【发布时间】:2015-07-23 00:01:44
【问题描述】:
每次下载数据时,我都会使用自定义适配器填充 ListView。问题是每次 onResume 调用下载方法时,视图都是重复的,而不是删除旧视图并替换为新视图。
如何解决停止重复?下面是我的代码,我尝试制作一种删除视图的方法,但我认为它的工作正常或更好但尚未完成。
public class CommentAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
ViewHolder holder;
public CommentAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.singlecomment, null);
holder = new ViewHolder();
holder.username = (TextView) convertView.findViewById(R.id.usernme);
holder.message = (TextView) convertView.findViewById(R.id.postCommentBox);
holder.timeStamp = (TextView) convertView.findViewById(R.id.timeAgo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
holder.username.setText(newsItem.getUsername());
holder.message.setText(newsItem.getmComment());
holder.timeStamp.setText(newsItem.getTimeStamp());
return convertView;
}
//method to delete old views i tried implementing
//from each adapter
public void deleteOldViews(){
listData.clear();
this.notifyDataSetChanged();
}
static class ViewHolder {
TextView username;
TextView message;
TextView timeStamp;
}
} //CustomAdapter的实现
//ListAdapter
private void updateList() {
// Locate the listview in listview_main.xml
ArrayList<ListItem> listData = getListData();
final ListView listView = getListView();
listView.deferNotifyDataSetChanged();
listView.setAdapter(new CommentAdapter(PostComments.this, listData));
AsyncTask
类 LoadAuto 扩展 AsyncTask {
@Override
protected String doInBackground(String... params) {
int success;
try {
// Building Parameters
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("postID", postID));
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
LOAD_COMMENT_URL, "POST", param);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
//Updating Json
mCommentList = new ArrayList<HashMap<String, String>>();
try {
//check connection before search for posts
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag;
// gets the content of each tag;
String time = c.getString(TAG_TIMESTAMP);
content = c.getString(TAG_COMMENT);
username = c.getString(TAG_USERNAME);
//getting comments
mUsername.add("By " + username);
TimeStamp.add(time);
Comment.add(content);
}
} catch (JSONException e) {
e.printStackTrace();
}
return json.getString(TAG_MESSAGE);
} else {
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException npe) {
//Handle Exception
String s = "NullPointerException";
return s;
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected void onPostExecute(String file_url) {
//Updating List
updateList();
;
}
}
getlistdata 方法
private ArrayList<ListItem> getListData() {
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
String[] username = mUsername.toArray(new String[mUsername.size()]);
String[] comments = Comment.toArray(new String[Comment.size()]);
String[] time = TimeStamp.toArray(new String[TimeStamp.size()]);
for (int i = 0; i < username.length; i++) {
ListItem newsData = new ListItem();
newsData.setUsername(username[i]);
newsData.setmComment(comments[i]);
newsData.setTimeStamp(time[i]);
listMockData.add(newsData);
System.gc();
}
return listMockData;
}
【问题讨论】:
-
您可以清除列表或在 onResume() 中重新启动它。你能发布 getListData();方法代码?
-
我添加了getlistdata方法
标签: java android listview android-asynctask