我正在设计一个基本的文本消息 UI,我有一个名为 converseView 的类。
public class converseView extends Activity {
private ArrayList<String> conversation = new ArrayList<String>();
public ArrayAdapter<String> adapter;
private String phoneNumber;
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converse_view);
this.adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
conversation);
adapter.setNotifyOnChange(true);
listView = (ListView) findViewById(R.id.conversation);
listView.setAdapter(adapter);
}
protected void onStart(Bundle savedInstanceState)
{
super.onStart();
this.adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
conversation);
this.adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
protected void onResume()
{
super.onResume();
this.adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
conversation);
this.adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
public void changeNumber(String phoneNumber2) {
this.phoneNumber = phoneNumber2;
}
public void addText(String message) {
if (this.adapter!=null)
{
this.adapter.add(message);
this.conversation.add(message);
this.adapter.notifyDataSetChanged();
}
}
@Override
public String toString() {
return phoneNumber;
}
public boolean equals(converseView other) {
return this.phoneNumber.equals(other.phoneNumber);
}
}
每次我向新电话号码发送消息时,它都会创建一个实例,并且 ArrayList/ArrayAdapter 应该跟踪所有发送到该号码的消息。但是当我尝试进入这个视图时,似乎 ArrayAdapter 没有更新,也没有显示任何内容。我能做些什么来解决这个问题。
我处理将东西存储到适配器的方式是这样的
if (phoneNumber.length() > 0 && message.length() > 0) {
sendSMS(phoneNumber, message);
converseView numberObject = new converseView();
numberObject.changeNumber(phoneNumber);
MainActivity.addThread(numberObject);
numberObject.addText(message);
/*
* if(MainActivity.contains(numberObject)) { int index =
* MainActivity.indexOf(numberObject); converseView numberObject2 =
* (converseView) MainActivity.getList().get(index);
* numberObject2.addText(message); }
*/
}