每次下载数据时,我都会使用自定义适配器填充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;
}
答案 0 :(得分:1)
我认为你必须声明全局适配器变量,而不是每次设置listview适配器时都创建新变量。
CommentAdapter commentAdapter;
然后在updateList()方法
commentAdapter = new CommentAdapter(PostComments.this, listData);
listView.setAdapter(commentAdapter );
使用此代码,您不会丢失对适配器的引用,希望此帮助
答案 1 :(得分:0)
我不认为它是一个全局变量,即直接创建一个类的实例,并且由于它是一个listadapter,它直接分配给listview,你认为应该做的是使适配器成为一个全局变量然后在设置任何数据之前调用.notifyDataSetChanged(),然后设置适配器并为其提供所需的正确数据。
adapter.notifyDataSetChanged();
adapter = new CommentAdapter(this,listData);
listView.setAdapter(adapter);
答案 2 :(得分:-1)
尝试使用listView.setAdapter(null);
清空ListView
,然后重新设置适配器。