Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Adapting Data for Display in a List Filling Our String Array and Creating the Adapter

Why does it say The method setListAdapter(ArrayAdapter<String>) is undefined for the type MainList?

package com.example.blogreader2;

import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

import android.app.Activity; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.Toast;

public class MainListActivity extends Activity {

protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
protected JSONObject mblogData;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);

    if(isNetWorkAvailable()){
    GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
    getBlogPostTask.execute();
    }
    else{
        Toast.makeText(this, "The network is down",  Toast.LENGTH_LONG).show();
    }

}

private boolean isNetWorkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if(networkInfo != null && networkInfo.isConnected()){
        isAvailable = true;
    }
    return isAvailable;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_list, menu);
    return true;
}


public void UpdateList() {
    if(mblogData == null){
        //TODO: Handle error
    }
    else{
        try {
            JSONArray jsonPosts = mblogData.getJSONArray("posts");
            mBlogPostTitles = new String[jsonPosts.length()];
            for(int i = 0; i < jsonPosts.length(); i++ ){
                JSONObject posts = jsonPosts.getJSONObject(i);
                String title = posts.getString("title");
                title = Html.fromHtml(title).toString();
                mBlogPostTitles[i] = title;
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mBlogPostTitles);
            setListAdapter(adapter);


        } catch (JSONException e) {
            Log.e(TAG, "Json Exception Found: " , e);
        }
    }

}

private class GetBlogPostTask extends AsyncTask<Object, Void, JSONObject>{

    @Override
    protected JSONObject doInBackground(Object... params) {

        int responseCode = -1;
        JSONObject jsonResponse = null;

        try {
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK);
            InputStream inputStream = connection.getInputStream();
            Reader reader = new InputStreamReader(inputStream);
            int content = connection.getContentLength();
            char[] charArray = new char [content];
            reader.read(charArray);
            String responseData = new String(charArray);

            jsonResponse = new JSONObject(responseData);



        } catch (MalformedURLException e) {
            Log.e(TAG, "Exception Found: " , e );
        } catch (IOException e) {
            Log.e(TAG, "Exception Found: " , e );
        }catch (Exception e){
            Log.e(TAG, "Exception Found: " , e );
        }
        return jsonResponse;
    }
    protected void onPostExecute (JSONObject result){
        mblogData = result;
        UpdateList();
    }

}





@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi John

Try extending from ListActivity instead of a regular Activity. You can do this by changing the following line right at the beginning of your code

public class MainListActivity extends Activity {

to

public class MainListActivity extends ListActivity {

Because ListActivity is derived from Activity it retains all the features of activity while supporting methods that help when using Lists, hopefully after arranging your imports (Ctrl + O) you should see the error go away. Please let me know if this is not the case.

Hope this helps

Thanks Daniel

Thanks a bunch!