我有一个asynctask,通过在Asynctask的doInBackground()方法中使用JSON方法从数据库中收集注释,用户名和数字。然后在onPostExecute()方法中将这些数组转换为字符串。我希望将这些字符串发送到扩展BaseAdapter的类,如何将字符串发送到另一个类但仍然在后台运行BaseAdapter?这是我目前的代码,
class CreateCommentLists extends BaseAdapter{
Context ctx_invitation;
String[] listComments;
String[] listNumbers;
String[] listUsernames;
public CreateCommentLists(Context ctx_invitation, String[] test1, String[] test2, String[] test3)
{
super();
this.ctx_invitation = ctx_invitation;
listComments = test1;
listNumbers = test2;
listUsernames = test3;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listComments.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listComments[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
try
{
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
v = li.inflate(R.layout.list_item, null);
TextView commentView = (TextView)v.findViewById(R.id.listComment);
TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
commentView.setText(listComments[position]);
NumbersView.setText(listNumbers[position]);
usernamesView.setText(listUsernames[position]);
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
}
final CreateCommentLists mycmlist = new CreateCommentLists(this, test1, test2, test3);
class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
protected JSONObject doInBackground(JSONObject... params) {
JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
return json2;
}
@Override
protected void onPostExecute(JSONObject json2) {
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res2 = json2.getString(KEY_SUCCESS);
if(Integer.parseInt(res2) == 1){
JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
String test1[] = new String[commentArray.length()];
for ( int i=0; i<commentArray.length(); i++ ) {
comments[i] = commentArray.getString(i);
}
JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
String test2[] = new String[] numberArray.length()];
for ( int i=0; i<numberArray.length(); i++ ) {
numbers[i] = numberArray.getString(i);
}
JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
String test3[] = new String[usernameArray.length()];
for ( int i=0; i<usernameArray.length(); i++ ) {
usernames[i] = usernameArray.getString(i);
}
ListView lstComments = (ListView)findViewById(android.R.id.list);
lstComments.setAdapter(mycmlist);
}//end if key is == 1
else{
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}//end else
}//end if
} //end try
catch (JSONException e) {
e.printStackTrace();
}//end catch
}
}
new loadComments().execute();
这是我的logcat
07-16 15:21:15.719: W/dalvikvm(19691): threadid=1: thread exiting with uncaught exception (group=0x40ac9228)
07-16 15:21:15.739: E/AndroidRuntime(19691): FATAL EXCEPTION: main
07-16 15:21:15.739: E/AndroidRuntime(19691): java.lang.NullPointerException
07-16 15:21:15.739: E/AndroidRuntime(19691): at com.example.DashboardActivity$1CreateCommentLists.getCount(DashboardActivity.java:404)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.widget.ListView.setAdapter(ListView.java:460)
07-16 15:21:15.739: E/AndroidRuntime(19691): at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:539)
07-16 15:21:15.739: E/AndroidRuntime(19691): at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:1)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.os.Looper.loop(Looper.java:154)
07-16 15:21:15.739: E/AndroidRuntime(19691): at android.app.ActivityThread.main(ActivityThread.java:4945)
07-16 15:21:15.739: E/AndroidRuntime(19691): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 15:21:15.739: E/AndroidRuntime(19691): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 15:21:15.739: E/AndroidRuntime(19691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 15:21:15.739: E/AndroidRuntime(19691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 15:21:15.739: E/AndroidRuntime(19691): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
由于它们都是Activity
的内部类,因此您应该能够创建ListView
和Adapter
个引用成员变量,然后您可以调用setAdapter()
ListView
上的notifyDataSetChanged()
或Adapter
取决于您是在更新还是最初设置它。
如果你这样做
ListView lstComments
Activity
的成员变量然后内部类将能够看到它。所以当你打电话时
lstComments = (ListView)findViewById(android.R.id.list);
lstComments.setAdapter(mycmlist);
在AsyncTask
中,它应该更新您的列表