我正在开发一个需要自定义列表视图的 android 应用程序。
我需要一个自定义的列表视图,如下所示:
每当我单击扬声器按钮时,它都会播放它所属项目的内容。
我不知道 listview 会有多少项目,所以我想知道如何命名所有扬声器按钮以及如何获取任何按钮的相应内容!
任何想法?
谢谢!
我正在开发一个需要自定义列表视图的 android 应用程序。
我需要一个自定义的列表视图,如下所示:
每当我单击扬声器按钮时,它都会播放它所属项目的内容。
我不知道 listview 会有多少项目,所以我想知道如何命名所有扬声器按钮以及如何获取任何按钮的相应内容!
任何想法?
谢谢!
您也可以使用以下方法。
首先定义列表视图和您想要的音乐数组列表
List<HashMap<String, String>> MusicArray;
ListView MusicList;
现在同时初始化 MusicArray 和 MusicList,并将歌曲列表的值赋给 MusicArray。
现在使用 xml 文件创建自定义单元格布局
然后创建自定义类 ViewHolder,它将布局 xml 文件。
class ViewHolder {
TextView SongName, SongDescription;
int id;
}
然后现在创建将扩展 BaseAdapter 的 Custom_Music_List,如下所示。
public class Custom_Music_List extends BaseAdapter {
Context contex;
ViewHolder holder;
List<HashMap<String, String>> MusicItems;
TextView SongName, SongDescription;
public Custom_Music_List(Context context,
List<HashMap<String, String>> MusicArray) {
contex = context;
MusicItems = MusicArray;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return MusicItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) LayoutInflater
.from(contex);
convertView = inflater.inflate(R.layout.custom_music_cell, parent,
false);
holder.SongName = (TextView) convertView.findViewById(R.id.SongName);
holder.SongName.setText(MusicItems.get(position).get("SongName"));
holder.SongDescription = (TextView) convertView.findViewById(R.id.SongDescription);
holder.SongDescription.setText( + MusicItems.get(position).get("SongDescription"));
}
return convertView;
}
}
然后通过以下方式将适配器设置为 MusicList,您将能够获得被点击的适当列表项。
MusicList.setAdapter(new Custom_contact(ClassName.this, MusicArray));
MusicList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
// You will get position of row clicked from here using position, then try to access particular item from list using position
Log.i("Song Clicked", MusicArray.get(position).get("SongName"))
// Perform Action based upon position of song
}
});
您需要通过扩展或类来创建自定义适配器类。BaseAdapter
ArrayAdapter
创建自定义适配器类后,覆盖getView()
方法。
您可以查看以下链接:
或者您可以查看我的博客中的 ListView 类别。