GeekedOut
GeekedOut

Reputation: 17185

Android - adapter code does not compile

I have some code which I think should compile, but doesn't:

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

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

import com.problemio.ViewSolutionsActivity.DownloadWebPageTask;
import com.problemio.data.Discussion;
import com.problemio.data.DiscussionMessage;
import com.problemio.data.SuggestedSolution;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TopicActivity extends Activity 
{
    ArrayAdapter<Discussion> adapter;       

    ArrayList<Discussion> discussion = new ArrayList <Discussion>( );   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discussion);

        // Have to display the topic, and the existing discussion, and the form field.



        Discussion d = new Discussion ();
        d.setDiscussionTopicName( "Please wait while the discussion comments load" );

        discussion.add(d);
        adapter = new ArrayAdapter<Discussion>( this,R.layout.discussion_comments, discussion);

        setListAdapter(adapter);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

There is more code below but the line with setListAdapter(adapter); gives this error:

The method setListAdapter(ArrayAdapter<Discussion>) is undefined for the type TopicActivity

Any idea why? I actually copied this code from another class and it worked well there.

Thanks!

Upvotes: 0

Views: 143

Answers (4)

Arpit Garg
Arpit Garg

Reputation: 8546

   setListAdapter(adapter) works with

ListActivity or

if the layout has atleast one list view with id android.R.id.list

Upvotes: 1

flo
flo

Reputation: 2018

You can only user setListAdapter() in a ListActivity

Upvotes: 2

Hamza Waqas
Hamza Waqas

Reputation: 629

You need to extend your activity with the ListActivity. Right now you have

public class TopicActivity extends Activity 

Change it to

public class TopicActivity extends ListActivity

Upvotes: 2

Sunny
Sunny

Reputation: 14818

I think this should work extends your class to ListActivity.

Upvotes: 2

Related Questions