在我的程序中,java 类之一扩展了ListActivity ,但是代替扩展ListActivity,我 尝试扩展简单的Activity ,但获取少数错误喜欢:
类型PlayListActivity
PlayListActivity类型的方法 getListView()未定义
码:
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&amp; @SimplePlan用于 指南 !
答案 0 :(得分:3)
方法setListAdapter(ListAdapter)未定义类型 PlayListActivity
setListAdapter
是ListActivity
的方法。
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"
答案 1 :(得分:3)
如果您延伸ListActivity
,则在XML中定义ListView
,如下所示,并实施getListView()
ListActivity
方法。
<ListView
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent" />
如果您扩展Activity
,则会在XML中定义您的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);