嘿伙计们我在listview(自定义适配器)中面临问题。我已经使用播放音频的媒体播放器(mp3文件)实现了listview。我已将mp3文件包含在我的原始文件夹中。我知道这是一个循环视图。我的问题是在6-7行之后,相同的mp3文件与第0个位置的文件相同。 任何解决方案? 我的主要活动 公共类MainActivity扩展Activity实现OnItemClickListener {
ListView lview3;
ListViewCustomAdapter adapter;
private ArrayList<Object> itemList;
private ItemBean bean;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareArrayLits();
lview3 = (ListView) findViewById(R.id.listView1);
adapter = new ListViewCustomAdapter(this, itemList);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position1, long id) {
// TODO Auto-generated method stub
ItemBean bean = (ItemBean) adapter.getItem(position1);
Toast.makeText(this, "Title => "+bean.getTitle()+" \n Description => "+bean.getDescription(), Toast.LENGTH_SHORT).show();
}
/* Method used to prepare the ArrayList,
* Same way, you can also do looping and adding object into the ArrayList.
*/
public void prepareArrayLits()
{
itemList = new ArrayList<Object>();
AddObjectToList(R.drawable.ic_add, "Add", "Add desc",R.raw.mp1);
AddObjectToList(R.drawable.ic_delete, "Delete", "Delete desc",R.raw.mp2);
AddObjectToList(R.drawable.ic_down, "Down", "Down desc",R.raw.mp3);
AddObjectToList(R.drawable.ic_info, "Information", "Information desc",R.raw.mp4);
AddObjectToList(R.drawable.ic_help, "Help", "Help desc",R.raw.mp5);
AddObjectToList(R.drawable.ic_download, "Download", "Download desc",R.raw.mp6);
AddObjectToList(R.drawable.ic_mail, "Mail", "Mail desc",R.raw.mp7);
AddObjectToList(R.drawable.ic_search, "Search", "Search desc",R.raw.mp8);
AddObjectToList(R.drawable.ic_settings, "Settings", "Settings desc",R.raw.mp9);
}
// Add one item into the Array List
public void AddObjectToList(int image, String title, String desc,int audio)
{
bean = new ItemBean();
bean.setDescription(desc);
bean.setImage(image);
bean.setTitle(title);
bean.setAudio(audio);
itemList.add(bean);
}
}
我的listviewCustomadapter
public class ListViewCustomAdapter extends ArrayAdapter {
MediaPlayer mPlayer;
Context c;
ArrayList<Object> itemList;
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAdapter(Activity context, ArrayList<Object> itemList) {
super(context, R.layout.main);
this.context = context;
this.itemList = itemList;
this.c = context;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
ImageView imgViewLogo;
TextView txtViewTitle;
TextView txtViewDescription;
Button btn1, btn2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.items, null);
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
holder.btn1 = (Button) convertView.findViewById(R.id.button);
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == 0) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp1);
mPlayer.start();
}
if (position == 1) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp2);
mPlayer.start();
}
if (position == 2) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp3);
mPlayer.start();
}
if (position == 3) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp4);
mPlayer.start();
}
if (position == 4) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp5);
mPlayer.start();
}
if (position == 5) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp6);
mPlayer.start();
}
if (position == 6) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp7);
mPlayer.start();
}
if (position == 7) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp8);
mPlayer.start();
}
if (position == 8) {
mPlayer = MediaPlayer.create(getContext(), R.raw.mp9);
mPlayer.start();
}
}
});
holder.btn2 = (Button) convertView.findViewById(R.id.button2);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
ItemBean bean = (ItemBean) itemList.get(position);
holder.imgViewLogo.setImageResource(bean.getImage());
holder.txtViewTitle.setText(bean.getTitle());
holder.txtViewDescription.setText(bean.getDescription());
return convertView;
}
}
答案 0 :(得分:0)
这只是因为convertView被重用了。
即使holder.btn1.setOnClickListener(new View.OnClickListener() { ...
设置(convertView != null)
。
同样,对于所有视图项使用单个MediaPlayer可能会比一次又一次地创建它更好。