gpt4 book ai didi

android - 如何使用 Activity 而不是 ListActivity 扩展类

转载 作者:行者123 更新时间:2023-11-29 14:49:18 25 4
gpt4 key购买 nike

在我的程序中,其中一个 java class extends ListActivity 但是我试图扩展简单的 Activity 而不是扩展 ListActivity,但是得到很少有错误,例如:

  1. PlayListActivity 类型的setListAdapter(ListAdapter) 方法未定义

  2. 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>

我正在为学习者放置解决方案 [like: me],请看下面:

// 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 的指导!

最佳答案

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

setListAdapterListActivity 的方法。

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

The method getListView() is undefined for the type 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"

关于android - 如何使用 Activity 而不是 ListActivity 扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com