2014-05-23 73 views
1

在我的程序在Java 類的一個擴展ListActivity但在地方擴展ListActivity的擴展類我是想擴展簡單的活動,但得到幾個錯誤,如:如何使用活動,而不是ListActivity

  1. 方法setListAdapter(ListAdapter)是未定義針對類型PlayListActivity

  2. 方法getListView()是該類型PlayListActivity

代碼未定義

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> 

我把學習者 [一樣:我] 解決方案,請參閱以下內容:

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

,改變

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

感謝到@ Raghunandan & @SimplePlan for t他指導

回答

3

方法setListAdapter(ListAdapter)是未定義的類型 PlayListActivity

setListAdapterListActivity方法。

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

方法getListView()是未定義的類型PlayListActivity

getListView()ListActivity方法。

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

而不是

setListAdapter(adapter); 

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

更改爲

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

更改爲

<ListView 
    android:id="@+idlist" 
+0

但仍然得到:該方法setListAdapter(ListAdapter)是未定義的類型PlayListActivity – Sophie

+0

@Sophie刪除這段代碼形成你activtiy – Raghunandan

+0

做的感謝! 。將盡快接受 – Sophie

3

如果擴展ListActivity然後定義您的ListView如下所示的XML,並執行getListView()方法ListActivity

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

,如果你在XML擴展Activity然後定義你的ListView像下面並沒有必要實施getListView()這一點。

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

,你也初始化你ListView

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

我打勾你的解決方案是有用的 – Sophie

相關問題