Android 避免重复收件箱的Listview项目
我正在开发一个包含收件箱活动和对话活动的消息传递应用程序。为此,我使用SQL数据库保存所有接收和发送的消息,并将它们附加到收件箱活动的列表视图中。我正在从数据库中获取要追加的数据。我不想附加所有具有相同号码但具有最新号码的邮件,因为其他邮件将进入对话。帮我做这个。我已经搜索过了,但在这里找不到,还有一件事,我只是一个学习者。任何意见都将不胜感激 我想要这个 收件箱活动的代码是Android 避免重复收件箱的Listview项目,android,eclipse,sqlite,listview,Android,Eclipse,Sqlite,Listview,我正在开发一个包含收件箱活动和对话活动的消息传递应用程序。为此,我使用SQL数据库保存所有接收和发送的消息,并将它们附加到收件箱活动的列表视图中。我正在从数据库中获取要追加的数据。我不想附加所有具有相同号码但具有最新号码的邮件,因为其他邮件将进入对话。帮我做这个。我已经搜索过了,但在这里找不到,还有一件事,我只是一个学习者。任何意见都将不胜感激 我想要这个 收件箱活动的代码是 public class ReceiveSMSActivity extends Activity { DBAdapte
public class ReceiveSMSActivity extends Activity {
DBAdapter myDb;
private List<Message> MyMessages = new ArrayList<Message>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openDB();
getResultsAndAppend();
populateListView();
}
private void getResultsAndAppend() {
Cursor cursor = myDb.getAllRows();
if (cursor.moveToFirst()) {
do {
// Process the data:
// String name = cursor.getString(DBAdapter.COL_NAME);
String number = cursor.getString(DBAdapter.COL_NUMBER);
String message = cursor.getString(DBAdapter.COL_MESSAGE);
MyMessages.add(new Message(message, number, true));
} while (cursor.moveToNext());
}
}
private void populateListView() {
ArrayAdapter<Message> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.listViewInbox);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Message> {
public MyListAdapter() {
super(ReceiveSMSActivity.this, R.layout.inbox_list_item_layout,
MyMessages);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(
R.layout.inbox_list_item_layout, parent, false);
}
// Find the Message to work with.
Message currentMessage = MyMessages.get(position);
// Message:
TextView message = (TextView) itemView.findViewById(R.id.tvMessage);
message.setText(currentMessage.getMessage());
// Number:
TextView number = (TextView) itemView.findViewById(R.id.tvNumber);
number.setText(currentMessage.getNumber());
return itemView;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
}一种解决方案是在一个hashmap中解析您的所有消息,该hashmap将电话号码作为键,并有一个“PeopleText”模型,该模型将有一个来自同一个人的消息列表(号码)、文本数量等 在解析它之后,您只需迭代hashmap而不是db行 这就是逻辑。下面是一些代码片段
public class PeopleTexts {
private String phoneNumber;
private String name;
private ArrayList<Message> peopleTexts;
// add setters and getters...
public int textCount() {
return peopleTexts.size();
}
public void addMessage(Message message) {
this.peopleTexts.add(message);
}
}
我让你做剩下的。谢谢Mathieu的回复。我使用了你的帮助和MyMessages.haskey(),但我收到一个错误“类型列表的方法haskey(字符串)未定义”我无法使用此方法。看看这个令人愉快的故事,它是
containsKey(对象键)
它只有contains(布尔值),我也试过了,但没有成功。仍然显示所有的条目。你在使用Hashmap吗?
public class PeopleTexts {
private String phoneNumber;
private String name;
private ArrayList<Message> peopleTexts;
// add setters and getters...
public int textCount() {
return peopleTexts.size();
}
public void addMessage(Message message) {
this.peopleTexts.add(message);
}
}
if (cursor.moveToFirst()) {
do {
// Process the data:
// String name = cursor.getString(DBAdapter.COL_NAME);
String number = cursor.getString(DBAdapter.COL_NUMBER);
String message = cursor.getString(DBAdapter.COL_MESSAGE);
if(MyMessages.containsKey(number)) {
MyMessages.put(number, new PeopleTexts(name, number);
}
MyMessages.get(number).addMessage(new Message(number, message);
} while (cursor.moveToNext());
}