在我的程序在Java 類的一個擴展ListActivity但在地方擴展ListActivity的擴展類我是想擴展簡單的活動,但得到幾個錯誤,如:如何使用活動,而不是ListActivity
方法setListAdapter(ListAdapter)是未定義針對類型PlayListActivity
方法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他指導!
但仍然得到:該方法setListAdapter(ListAdapter)是未定義的類型PlayListActivity – Sophie
@Sophie刪除這段代碼形成你activtiy – Raghunandan
做的感謝! 。將盡快接受 – Sophie