问题描述
- MainActivity中的setListAdapter
-
嘿,MainActivity中onPostExecute(string result)方法爆出了一个错误:"The method setListAdapter(CustomAdapter) is undefined for the type MainActivity.getTweets"
MainActivity.java
public class Main Activity extends Activity { ArrayList<TweetDetailClass> tweets = new ArrayList<TweetDetailClass>(); protected void onCreate(Bundle savedInstanceState) { .. } public boolean onCreateOptionsMenu(Menu menu) { .. } public void searchTwitter(View view){ ... new GetTweets().execute(searchURL); } public class GetTweets extends AsyncTask<String, Void, String>{ protected String doInBackground(String... twitterURL){ .. } protected void onPostExecute(String result) { setListAdapter(new CustomAdapter(MainActivity.this, R.layout.listview, tweets)); } } }
CustomAdapter Constructor 中的代码如下:
CustomAdapter(Context c, int textView, ArrayList<TweetDetailClass> data){ //_data = data; //_c = c; super(c, textView, data); this._data = data; }
解决方案
setListAdapter()
只有在Activity 继承ListActivity时才能用。你的xml布局文件肯定包含ListView 对象,id是 "@android:id/list"
。
如果满足继承条件的话,可以这样
yourListView.setAdapter(new CustomAdapter(MainActivity.this, R.layout.listview, tweets));
时间: 2025-01-05 19:28:08