Sophie
Sophie

Reputation: 2634

How to extends Class using Activity instead of ListActivity

In my program one of the java class extends ListActivity but in place of extending ListActivity i am trying to extend simple Activity, but getting few errors like:

  1. The method setListAdapter(ListAdapter) is undefined for the type PlayListActivity

  2. The method getListView() is undefined for the type PlayListActivity

code:

public class PlayListActivity extends Activity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_playlist);

    ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    SongsManager plm = new SongsManager();
    // get all songs from sdcard
    this.songsList = plm.getPlayList();

    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);

        // adding HashList to ArrayList
        songsListData.add(song);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, songsListData,
            R.layout.activity_playlist_item, new String[] { "songTitle" }, new int[] {
                    R.id.songTitle });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });
}

activity_playlist_item.xml;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <TextView
        android:id="@+id/songTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/imageView1"
        android:layout_toRightOf="@+id/thumbnail"
        android:maxLines="1"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:typeface="sans" />

</RelativeLayout>

activity_playlist.xml;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

       <ListView 
         android:id="@android:id/list" 
         android:layout_height="match_parent" 
         android:layout_width="match_parent" />

</RelativeLayout>

I am placing solution for learners [like: me], please see below:

// declare listview globally
ListView lv ;
// reference to ListView
lv = (ListView) findViewById(R.id.list);
// set list adapter to ListView
lv.setAdapter(adapter);

and also change

<ListView 
         android:id="@+id/list" .... />

Thanks to @Raghunandan & @SimplePlan for the guidance !

Upvotes: 1

Views: 676

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

The method setListAdapter(ListAdapter) is undefined for the type PlayListActivity

setListAdapter is method of ListActivity.

http://developer.android.com/reference/android/app/ListActivity.html#setListAdapter(android.widget.ListAdapter)

The method getListView() is undefined for the type PlayListActivity

getListView() is method of ListActivity.

http://developer.android.com/reference/android/app/ListActivity.html#getListView()

Instead of

setListAdapter(adapter);

// selecting single ListView item
ListView lv = getListView();

Change to

 ListView lv = (ListView) findViewById(R.id.list);
 lv.setAdapter(adapter);

Change to

 <ListView 
     android:id="@+idlist" 

Upvotes: 3

M D
M D

Reputation: 47817

If you extends ListActivity then defined your ListView in XML like below and implement getListView() method of ListActivity.

 <ListView 
     android:id="@android:id/list" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" />

and if you extend Activity then defined your ListView in XML like below and no need to implement getListView() for this.

<ListView 
     android:id="@+id/list" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" />

and also you initialized your ListView like

 ListView lv = (ListView) findViewById(R.id.list);
 lv.setAdapter(adapter);

Upvotes: 3

Related Questions