1

我想从 onclicklistener 更改 imageview 源(当然还有音乐)。它已更改,但每次单击时第一张图像都会更改。我该如何解决?

这是我的代码:

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;
                }
                
4

1 回答 1

4

您必须使用 a onItemClickListeneron your 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();                             
        }
    });

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

于 2013-06-07T10:19:08.613 回答