我正在制作一个短信应用程序,问题是,当新的短信收到活动不刷新列表视图时,我尝试了数据集,但没有发生任何事情。 我把代码放在了onresume方法上 但是当新的短信收到时,我挂起我的应用程序。当我把代码放入asynctask时,它会给出错误。
void update() {
getAll.clear();
Uri SMS_INBOX = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date DESC");
startManagingCursor(c);
count = new String[c.getCount()];
snippet = new String[c.getCount()];
thread_id = new String[c.getCount()];
String[] num=new String[c.getCount()];
date=new String[c.getCount()];
address=new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount()-1; i++) {
count[i] = c.getString(c.getColumnIndexOrThrow("message_count"))
.toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("_id"))
.toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"));
date[i]=c.getString(c.getColumnIndexOrThrow("date"));
Cursor cur = getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id = " + thread_id[i], null, null);
cur.moveToFirst();
startManagingCursor(cur);
num[i] = getContactName(this,cur.getString(cur.getColumnIndexOrThrow("address")).toString());
address[i] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
getAll.add(count[i]+","+num[i]+","+snippet[i]);
Log.d("help",num[i]);
c.moveToNext();
}
adapter = new HomeAdapter(this, R.layout.sms, getAll,date);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
2.In OnResume
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
update();
}
答案 0 :(得分:0)
这不是阅读新短信的正确方法
请参阅以下示例
希望它会帮助你
答案 1 :(得分:0)
试试这个mMyListView.invalidate();
或mMyListView.invalidateViews();
如果不起作用,请参阅此
((BaseAdapter) mMyListView.getAdapter()).notifyDataSetChanged();
阅读刷新ListView link 。
答案 2 :(得分:0)
将它包装在一个线程中,这样的事情应该有效: 然而,其他人说有更好的方法可以让你看看他们的链接,看看他们的方式是否更好...
void update() {
Thread thread = new Thread() {
@Override
public void run() {
getAll.clear();
Uri SMS_INBOX = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date DESC");
startManagingCursor(c);
count = new String[c.getCount()];
snippet = new String[c.getCount()];
thread_id = new String[c.getCount()];
String[] num = new String[c.getCount()];
date = new String[c.getCount()];
address = new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount() - 1; i++) {
if (i ==35){
i = 35;
}
count[i] = c.getString(c.getColumnIndexOrThrow("message_count")).toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("_id")).toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"));
date[i] = c.getString(c.getColumnIndexOrThrow("date"));
Cursor cur = getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id = " + thread_id[i], null, null);
cur.moveToFirst();
startManagingCursor(cur);
try {
num[i] = getContactName(Home.this, cur.getString(cur.getColumnIndexOrThrow("address")).toString());
} catch (Exception e) {
e.printStackTrace();
}
try {
address[i] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
} catch (Exception e) {
e.printStackTrace();
}
getAll.add(count[i] + "," + num[i] + "," + snippet[i]);
//Log.d("help", num[i]);
c.moveToNext();
}
Home.this.runOnUiThread(new Runnable() {
public void run() {
adapter = new HomeAdapter(Home.this, R.layout.sms, getAll, date);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
});
}
};
thread.start();
}