我在ListView中获得了重复的项目。向后和向下滚动有时会更改项目顺序。 我用谷歌搜索并发现许多线程报告了这个错误,但没有一个帮助我解决我的问题。
这是我的代码:
的活动:
package com.github.progval.SeenDroid;
import java.util.ArrayList;
import java.util.List;
import com.github.progval.SeenDroid.lib.Connection;
import com.github.progval.SeenDroid.lib.Message;
import com.github.progval.SeenDroid.lib.MessageFetcher;
import com.github.progval.SeenDroid.lib.Query.ParserException;
import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class ShowUserActivity extends ListActivity {
private Connection connection;
public ArrayList<Message> listMessages = new ArrayList<Message>();
public MessageAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
this.connection = new Connection();
this.setTitle(R.string.homefeed_title);
this.listMessages = new MessageFetcher(this.connection).fetchUser();
this.bindUi();
}
private void bindUi() {
this.adapter = new MessageAdapter(this, this.listMessages);
this.setListAdapter(adapter);
// TODO Bind buttons
}
}
MessageAdapter:
package com.github.progval.SeenDroid;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
import com.github.progval.SeenDroid.lib.Message;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MessageAdapter extends BaseAdapter {
private Context context;
private List<Message> items = new ArrayList<Message>();
private int lastPosition = 0;
public MessageAdapter(Context context, List<Message> items) {
super();
this.context = context;
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LinearLayout view;
view = (LinearLayout) LinearLayout.inflate(this.context, R.layout.message, null);
Log.d("SeenDroid", String.format("Get view %d", position));
TextView title = new TextView(view.getContext());
title.setText(this.items.get(position).getTitle());
view.addView(title);
return view;
} else {
return convertView;
}
}
@Override
public int getCount() {
return this.items.size();
}
@Override
public Object getItem(int location) {
return this.items.get(location);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
}
顺便说一下,输出是:
D/SeenDroid(30939): Get view 0
D/SeenDroid(30939): Get view 1
D/SeenDroid(30939): Get view 2
D/SeenDroid(30939): Get view 3
D/SeenDroid(30939): Get view 4
D/SeenDroid(30939): Get view 5
D/SeenDroid(30939): Get view 6
D/SeenDroid(30939): Get view 7
D/SeenDroid(30939): Get view 8
D/SeenDroid(30939): Get view 0
D/SeenDroid(30939): Get view 16
此致 ProgVal
答案 0 :(得分:32)
试试这个:
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LinearLayout view = (LinearLayout) LinearLayout.inflate(this.context,
R.layout.message, null);
Log.d("SeenDroid", String.format("Get view %d", position));
TextView title = new TextView(view.getContext());
title.setText(this.items.get(position).getTitle());
view.addView(title);
return view;
} else {
LinearLayout view = (LinearLayout) convertView;
TextView title = (TextView) view.getChildAt(0);
title.setText(this.items.get(position).getTitle());
return convertView;
}
}
解释:您获得了重复项,因为Android上的列表重用了UI对象。如果它不为null,则应该重用convertView
而不是创建新的{{1}}。当然,您有责任为正在重用的实例设置适当的值。否则,该值将保留在最后一次“使用”中。
答案 1 :(得分:4)
BOOL WINAPI WriteConsole(..., const VOID *lpBuffer, DWORD nNumberOfCharsToWrite, ..., ..);
答案 2 :(得分:2)
ListView不保证您添加的项目的唯一性。这是你的责任。您正在使用ArrayList
来存储项目,并且可以根据需要存储任意数量的重复项目。
如果要删除重复项,请将项目置于设置中,然后再次列入列表:
listMessages = new ArrayList<Messages>(new LinkedHashSet<Message>(listMessages))
LinkedHashSet
将删除重复项,保留项目的初始顺序,ArrayList
将允许按位置访问元素。
答案 3 :(得分:0)
建议 - 在title.setText之后添加一条日志语句,并写入从getTitle()获取的值。您可能会知道为什么会收到重复的条目。
答案 4 :(得分:0)
您应该在ListAdapter中使用ViewHolder范例
可以在这里找到一个很好的例子: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html