我需要用 AsyncTask 填充 ListView。此代码有效,TextView 更改为“开始”,然后更改为“结束”。但是 ListView 是空白的,没有显示。在日志中,我看到“array = 3”。
package a.test3;
import java.util.ArrayList;
import a.test3.MainActivity;
import a.test3.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
GrabURL mt;
TextView textView;
ListView lvMain;
ArrayList<String> result_arr = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = new TextView(this);
lvMain = (ListView) findViewById(R.id.listView1);
mt = new GrabURL();
mt.execute();
}
public class GrabURL extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
textView.setText("Begin");
setContentView(textView);
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
result_arr.add("a");
result_arr.add("b");
result_arr.add("c");
return result_arr;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
Log.d("info", "array = " + result.size());
textView.setText("End");
setContentView(textView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, result);
lvMain.setAdapter(adapter);
}
}
}
请告诉我,我做错了什么?