简体   繁体   中英

setListAdapter within MainActivity

Hi I'm trying to get my listview to work with json and in my MainActivity. I have an error on my onPostExecute(string result) method . It gives me the following error: "The method setListAdapter(CustomAdapter) is undefined for the type MainActivity.getTweets". I have the following:

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;
}

Thanks for your help! I'm a beginner so any help would be greatly appreciated!

setListAdapter() is a method on ListActivity . Your activity (whose name apparently is the invalid Main Activity ) appears to inherit from Activity , not ListActivity .

setListAdapter() can only be used if your Activity extends ListActivity and I think your xml layout must contain a ListView object with the id "@android:id/list".

Alternativelly, if your Activity extends Activity you should set the adapter like:

yourListView.setAdapter(new CustomAdapter(MainActivity.this, R.layout.listview, tweets));

Let me know if it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM