listview显示分页数据重复
============问题描述============
是单条item数据重复,比如正常显示应该是
item数据1
item数据2
item数据3
item数据4...
这段代码运行
item数据1
item数据1
item数据1
item数据1...
(重复数量是网页链接的【pageLines 每页显示行数】)
=======================================================
代码
==========================
public class SchoolNews extends Activity {
private ListView view;
private ImageView imageView;
private SchoolAdapter adapter;
private int count=1;
private boolean is_divpage; //是否进行分页操作
String url = "http://dingtao2.gotoip2.com/publish-info.html?type=1&pageLines=1&pageNo=";
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schoolnews);
adapter=new SchoolAdapter(this);
new DownloadTask().execute(url, Integer.toString(count));
/* if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}*/
view = (ListView) findViewById(R.id.list);
//jsondata();
imageView = (ImageView) findViewById(R.id.btn_about_back);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
view.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(is_divpage && scrollState==OnScrollListener.SCROLL_STATE_IDLE){
Toast.makeText(SchoolNews.this, "获取数据", 1).show();
new DownloadTask().execute(url, Integer.toString(count));
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
is_divpage=(visibleItemCount+firstVisibleItem==totalItemCount);
if(visibleItemCount+firstVisibleItem==totalItemCount){
//jsondata();
}
}
});
/* SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.relative_school, new String[]{"operationTime","title"},
new int[]{R.id.ItemClass,R.id.ItemTitle});
view.setAdapter(adapter);
adapter.notifyDataSetChanged();*/
view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
ListView listView=(ListView) parent;
HashMap<String, Object>map=(HashMap<String,Object>) listView.getItemAtPosition(position);
// String school_url=(String) map.get("id");
int school_url= (Integer) map.get("id");
Intent intent_a=new Intent(SchoolNews.this,WebActivity.class);
try {
intent_a.putExtra("schID", school_url);
startActivity(intent_a);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
class DownloadTask extends AsyncTask<String, Void, List<Map<String, Object>>> {
@Override
protected List<Map<String, Object>> doInBackground(String... params) {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0] + params[1]);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("联网成功");
try {
String jsonData = EntityUtils.toString(response.getEntity(), "UTF-8");
//JSONObject jsonObject = new JSONObject(jsonData);
//JSONArray jsonArray = jsonObject.getJSONArray("list");
JSONArray jsonArray = new JSONArray(jsonData);
if (!jsonArray.isNull(0)) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Iterator iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object value = jsonObject2.get(key);
map.put(key, value);
/* int id =jsonObject2.getInt("id");
String title = jsonObject2.getString("title");
String path=jsonObject2.getString("url");
String operationTime = jsonObject2.getString("operationTime");
map.put("id", id + "");
map.put("title", title);
map.put("url",path);
map.put("operationTime", operationTime);*/
}
list.add(map);
}
//return list;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---1--->" + list.size());
return list;
}
@Override
protected void onPostExecute(List<Map<String, Object>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result.size() > 0) {
lists.addAll(result);
adapter.setData(lists);
if (count == 1) {
view.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
count++;
} else {
Toast.makeText(getApplicationContext(), "没有更多数据了....", 0)
.show();
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
============解决方案1============
接口调用了多次吧
============解决方案2============
一个for循环套一个while循环,好像出问题了,你在好好跟踪一下你的代码。
============解决方案3============
这应该就是逻辑问题了,慢慢检查一下吧,为啥会变成这样,好像知道了,list.add应该在循环里面
posted on 2014-10-17 10:33 android基础教程 阅读(512) 评论(0) 编辑 收藏 举报