baseadapter上的onclick图像更改

时间:2013-06-07 08:32:29

标签: android baseadapter

我想从onclicklistener更改imageview源(当然还有音乐)。这是变化,但是每次点击都会改变第一个图像。我该如何解决?

(抱歉我的英语不好)

enter code here
public View getView(final int p, View v, ViewGroup arg2)
            {
                    Log.w("BaseAdapter", "p: "+p+", preP: "+preP );

                    if (v == null)
                        v = li.inflate(R.layout.listview_item, null);

                    ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
                    TextView baslik = (TextView) v.findViewById(R.id.textView1);
                    baslik.setText( isimler[p] );

                    if ( p == preP )
                        iv.setImageResource(R.drawable.stop);

                    v.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            if( preP != p  )
                            {
                                System.out.println("müzik çalışılacak.");
                                mPlay = MediaPlayer.create( getApplicationContext(), sarkilar[p] );
                                mPlay.start();
                                preP=p;
                            }
                            else
                            {
                                mPlay.stop();
                                preP=-1;
                            }

                            //getView(p, v, arg2);
                            initList();                             
                        }
                    });

                    return v;
                }

1 个答案:

答案 0 :(得分:3)

您必须在onItemClickListener上使用ListView,因为这是正确的方法。

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapterView, View v, int pos,
                                long arg3) {
            Item i = (Item) adapterView.getItemAtPosition(pos);
            ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
            iv.setImageResource(R.drawable.your_image);

            mPlay = MediaPlayer.create( getApplicationContext(), sarkilar[pos] );
                            mPlay.start();
        }
    });

如果我必须修改你的代码

    v.setTag(p); //attach the position to your view
    v.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            //How you get the correct ImageView
            ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
            iv.setImageResource(R.drawable.your_image);

            //The position is attached to the Clicked view
            int position = (int) v.getTag(); 

            if( preP != position  )
            {
                System.out.println("müzik çalışılacak.");
                mPlay = MediaPlayer.create( getApplicationContext(), sarkilar[position] );
                mPlay.start();
                preP=position;
            }
            else
            {
                mPlay.stop();
                preP=-1;
            }

            //getView(p, v, arg2);
            initList();                             
        }
    });

但我个人不会使用第二种方法