我想显示所有短信..所以我运行 loadIntialMessage() 来显示 20 条短信..
在这种方法上,我将运行线程背景以显示下一条短信。这是在 loadIntialmessage() 方法的列表滚动方法上运行的 AsyncTask 类。
public void loadInitialMessages() {
String[] columns = new String[] { "_id", "thread_id", "date", "body",
"address", "type", "read" };
String orderby;
Log.d(TAG, "sory index" + sort_index);
// sort_index =1;
if (sort_index == 0) {
orderby = "date DESC";
} else {
orderby = "date ASC";
}
for (int i = 0; i < ((IdSMS.size() > NO_OF_SMS_TO_LOAD) ? NO_OF_SMS_TO_LOAD
: IdSMS.size()); i++) {
Cursor c = getContentResolver().query(Uri.parse(SMS_URI_ALL),
columns, " _id =" + IdSMS.get(i), null, null);
while (c.moveToNext()) {
String contactname = getContactDisplayNameByNumber((c
.getString(c.getColumnIndexOrThrow("address"))));
// Log.d(TAG, "contact number without replace space" +
// contactname);
if (contactname.contains(" ")) {
contactname.replace(" ", "");
// Log.d(TAG, "contact number replace space" +
// contactname);
}
if (!NumberListContact.contains(contactname)) {
NumberListContact.add(contactname);
IdSMS.add(c.getString(c.getColumnIndexOrThrow("_id"))
.toString());
ThreadSMS.add(c.getString(
c.getColumnIndexOrThrow("thread_id")).toString());
SMSDate.add(c.getString(c.getColumnIndexOrThrow("date"))
.toString());
MessageList.add(c
.getString(c.getColumnIndexOrThrow("body"))
.toString());
NumberList.add(c.getString(
c.getColumnIndexOrThrow("address")).toString());
SMSType.add(c.getString(c.getColumnIndexOrThrow("type"))
.toString());
SMSRead.add(c.getString(c.getColumnIndexOrThrow("read"))
.toString());
Log.d(TAG,
"read and unread"
+ c.getString(
c.getColumnIndexOrThrow("read"))
.toString());
}
Log.d(TAG, "Counter Value " + counter);
Log.d(TAG,
" thread_Id "
+ c.getString(
c.getColumnIndexOrThrow("thread_id"))
.toString());
counter++;
}
c.close();
}
// refreshAdapter();
inboxAdaptor = new inboxAdaptor(getApplicationContext(), IdSMS,
ThreadSMS, NumberListContact, NumberList, MessageList, SMSDate,
SMSType, SMSRead);
lvmsg.setAdapter(inboxAdaptor);
inboxAdaptor.notifyDataSetChanged();
lvmsg.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if (MessageList.size()
- (int) (firstVisibleItem + visibleItemCount) <= 1) {
Log.d("onScroll", " firstVisibleItem " + firstVisibleItem);
Log.d("onScroll", " visibleItemCount " + visibleItemCount);
Log.d("onScroll", " totalItemCount " + totalItemCount);
Log.d(TAG,
"Adding more data to listview ---------------------->>");
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
LoadNewMessage myTask = new LoadNewMessage(
MainActivity.this);
myTask.execute();
}
});
// if(!isLoadingNewMessages){
// mLoadNewMessage.post(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// isLoadingNewMessages=true;
// addToListview();
//
// }
// });
// }
}
}
});
}
class LoadNewMessage extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
Context context;
public LoadNewMessage(Context context) {
this.context = context;
pd = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
addToListview();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void... params) {
inboxAdaptor.notifyDataSetChanged();
}
}
`
在 addToListview() 我显示下一条短信..
public void addToListview() {
String[] columns = new String[] { "_id", "thread_id", "date", "body",
"address", "type", "read" };
String orderby = "date DESC limit 10";
if (sort_index == 1) {
orderby = "date ASC";
} else {
orderby = "date DESC";
}
// Log.d(TAG, " Adding new values Counter "+counter);
for (int i = 0; i < (((IdSMS.size() - counter) > NO_OF_SMS_TO_LOAD) ? NO_OF_SMS_TO_LOAD
: (IdSMS.size() - counter)); i++) {
Cursor c = getContentResolver().query(Uri.parse(SMS_URI_ALL),
columns, " _id =" + IdSMS.get(counter), null, null);
// Log.d(TAG, " Adding new values cursor count "+c.getCount());
while (c.moveToNext()) {
String contactname = getContactDisplayNameByNumber((c
.getString(c.getColumnIndexOrThrow("address"))));
if (!NumberListContact.contains(contactname)) {
NumberListContact.add(contactname);
IdSMS.add(c.getString(c.getColumnIndexOrThrow("_id"))
.toString());
ThreadSMS.add(c.getString(
c.getColumnIndexOrThrow("thread_id")).toString());
SMSDate.add(c.getString(c.getColumnIndexOrThrow("date"))
.toString());
MessageList.add(c
.getString(c.getColumnIndexOrThrow("body"))
.toString());
NumberList.add(c.getString(
c.getColumnIndexOrThrow("address")).toString());
SMSType.add(c.getString(c.getColumnIndexOrThrow("type"))
.toString());
SMSRead.add(c.getString(c.getColumnIndexOrThrow("read"))
.toString());
}
}
c.close();
counter++;
}
isLoadingNewMessages = false;
// refreshAdapter();
}
但是当我在消息加载时单击任何其他按钮时出现错误..
03-15 13:01:35.334: W/System.err(283): java.lang.NullPointerException
03-15 13:01:35.334: W/System.err(283): at com.example.smsmanager.MainActivity.addToListview(MainActivity.java:325)
03-15 13:01:35.344: W/System.err(283): at com.example.smsmanager.MainActivity$LoadNewMessage.doInBackground(MainActivity.java:139)
03-15 13:01:35.344: W/System.err(283): at com.example.smsmanager.MainActivity$LoadNewMessage.doInBackground(MainActivity.java:1)
03-15 13:01:35.344: W/System.err(283): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-15 13:01:35.344: W/System.err(283): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-15 13:01:35.344: W/System.err(283): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-15 13:01:35.344: W/System.err(283): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-15 13:01:35.344: W/System.err(283): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-15 13:01:35.344: W/System.err(283): at java.lang.Thread.run(Thread.java:1096)