Listview 使用 CursorAdapter 子类的数据复制项目
Listview duplicates items with data from a CursorAdapter subclass
我已经阅读了关于列表视图复制项目的各种帖子,但其中 none 似乎对使用 CursorAdapter subclass.
的列表视图有一个很好的解决方案
我正在尝试使用 扩展 CursorAdapter.
的 class 使用数据库 table 中的数据填充列表视图
当我第一次启动具有列表视图 的 activity 时 。列表项不重复。
- 约瑟夫
- 金
但 对此 activity 的任何后续调用,以下显示在我的列表视图中
- 金
- 金
我已经阅读了有关使用 ViewHolder 的信息,但我希望在我的列表视图中从我的数据库中获得一份新的数据副本 每次我调用此 activity
下面是我的列表适配器实现
public class ChatAdapter extends CursorAdapter {
private LayoutInflater cursorInfrlater;
View view;
public ChatAdapter(Context context,Cursor cursor, int flags){
super(context,cursor,flags);
cursorInfrlater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view , Context context ,Cursor cursor){
cursor.moveToFirst();
while(!cursor.isAfterLast()){
TextView name =(TextView) view.findViewById(R.id.sender_name);
name.setText(cursor.getString(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_SENDER_NAME)));
TextView message =(TextView) view.findViewById(R.id.message);
message.setText(cursor.getString(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_MESSAGE)));
TextView unread =(TextView) view.findViewById(R.id.counter);
unread.setText(Integer.toString(cursor.getInt(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_UNREAD_MESSAGES))));
cursor.moveToNext();
}
}
@Override
public View newView(Context context,Cursor cursor, ViewGroup viewGroup){
view = cursorInfrlater.inflate(R.layout.chat_row_layout,viewGroup,false);
return view;
}
}
实现我的activity
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.chat_displayer);
final ChatMessages cmc = new ChatMessages();
cmc.deleteNotifications(this);
ChatAdapter chatAdapter = cmc.getChatHistory(this);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(chatAdapter);
我应该做什么来删除列表项的重复?
删除
cursor.moveToFirst();
while(!cursor.isAfterLast()){
cursor.moveToNext();
来自 bindView
。该方法至少被调用 cursor.getCount()
次,因此您不需要围绕游标本身循环
我已经阅读了关于列表视图复制项目的各种帖子,但其中 none 似乎对使用 CursorAdapter subclass.
的列表视图有一个很好的解决方案我正在尝试使用 扩展 CursorAdapter.
的 class 使用数据库 table 中的数据填充列表视图当我第一次启动具有列表视图 的 activity 时 。列表项不重复。
- 约瑟夫
- 金
但 对此 activity 的任何后续调用,以下显示在我的列表视图中
- 金
- 金
我已经阅读了有关使用 ViewHolder 的信息,但我希望在我的列表视图中从我的数据库中获得一份新的数据副本 每次我调用此 activity
下面是我的列表适配器实现
public class ChatAdapter extends CursorAdapter {
private LayoutInflater cursorInfrlater;
View view;
public ChatAdapter(Context context,Cursor cursor, int flags){
super(context,cursor,flags);
cursorInfrlater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view , Context context ,Cursor cursor){
cursor.moveToFirst();
while(!cursor.isAfterLast()){
TextView name =(TextView) view.findViewById(R.id.sender_name);
name.setText(cursor.getString(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_SENDER_NAME)));
TextView message =(TextView) view.findViewById(R.id.message);
message.setText(cursor.getString(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_MESSAGE)));
TextView unread =(TextView) view.findViewById(R.id.counter);
unread.setText(Integer.toString(cursor.getInt(cursor.getColumnIndex(ChatHistory.COLUMN_NAME_UNREAD_MESSAGES))));
cursor.moveToNext();
}
}
@Override
public View newView(Context context,Cursor cursor, ViewGroup viewGroup){
view = cursorInfrlater.inflate(R.layout.chat_row_layout,viewGroup,false);
return view;
}
}
实现我的activity
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.chat_displayer);
final ChatMessages cmc = new ChatMessages();
cmc.deleteNotifications(this);
ChatAdapter chatAdapter = cmc.getChatHistory(this);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(chatAdapter);
我应该做什么来删除列表项的重复?
删除
cursor.moveToFirst();
while(!cursor.isAfterLast()){
cursor.moveToNext();
来自 bindView
。该方法至少被调用 cursor.getCount()
次,因此您不需要围绕游标本身循环