使用Listview行中的按钮android

时间:2013-08-06 09:25:31

标签: android android-listview listadapter android-adapter

在我的Android应用程序中,我使用的是Listview,listview的每一行都有textview和play / pause按钮。在这里,我在每个列表行显示音频文件。当用户点击播放按钮时,音频文件将启动,播放按钮的图标将被更改为暂停。如果我再次单击相同的按钮,它将停止该音频文件和暂停图标按钮将播放。

问题是如果我正在访问第一行的播放按钮而我点击另一行的播放按钮,那么第一行的图标应该会改变。我不知道如何实现这个功能。请帮我解决这个问题。

customAdapter的getView()方法:

  public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem_ringtone rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ringtone_row, null);
            holder = new ViewHolder();

            holder.txtTitle = (TextView) convertView
                    .findViewById(R.id.ringtoneTitle);
            holder.btnPlay = (ImageButton) convertView
                    .findViewById(R.id.btnPlay);
            holder.btnSet = (ImageButton) convertView.findViewById(R.id.btnSet);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtTitle.setText(rowItem.getRingTitle());
        holder.btnPlay.setTag(rowItem.getRingId());
        holder.btnSet.setTag(rowItem.getRingId());

        holder.btnPlay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getContext(), v.getTag().toString(),
                        Toast.LENGTH_LONG).show();
                if (CustomListViewAdapter_ringtone.mp != null) {
                    if (CustomListViewAdapter_ringtone.mp.isPlaying()) {
                        CustomListViewAdapter_ringtone.mp.stop();
                        CustomListViewAdapter_ringtone.mp.release();
                    }
                }
                mp = MediaPlayer.create(getContext(),
                        Integer.parseInt(v.getTag().toString()));
                mp.start();
                v.setBackgroundResource(R.drawable.set_icon);
                btnId = Integer.parseInt(v.getTag().toString());
            }
        });
return convertView;
    }

4 个答案:

答案 0 :(得分:1)

只需存储对OnListItemClick方法中单击的最后一个按钮的引用。

public class Something {
    private CustomListItem previouslyClickedListItem;

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        CustomListItem item = (CustomListItem) view;
        if (item != previouslyClickedListItem) {
            // Set previouslyClickedListItem to not clicked
            prevouslyClickedListItem = item;
            // Set item to clicked
        } else {
            // Set prevouslyClickListItem to not clicked
        }
    }
}

答案 1 :(得分:0)

在列表视图项单击侦听器上,您正在更改图标吗?所以你也得到了行的id。因此,您可以将该id存储在变量中,作为当前播放的行或文件。当您点击另一行时,只需检查该ID并根据该行更改图标。

像这样使用

yourlistview.setOnItemClickListener(new OnItemClickListener()
{
    @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long         arg3)
{ 
    Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

此示例代码适用于我:

View view = null;

 private OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            if(view != null)
            {
                TextView count = (TextView) view.findViewById(R.id.downloadCount);
                count.setText("007");
            }
            view = arg1;

        }
    };

答案 2 :(得分:0)

您必须使用listview的自定义适配器,并且您的代码必须位于getView方法中。

在此方法中,在findviewbyid之后,只需为按钮分配标记。

例如,btn.setTag(position);

然后当您点击播放按钮时,只需获取该标签即可。  例如,int clickedposition =(Intger)btn.getTag();

您现在只能更改该特定行的图标。

答案 3 :(得分:0)

以下是您获得救赎的代码段。我用过切换按钮,例如你可以相应地改变

public class CustomListviewAdapter extends BaseAdapter {

  private View PreviousView=null;

  public View getView(final int position, View convertView, final ViewGroup parent) {

  togglebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        View CurrentView = v;
          if(PreviousView != null && PreviousView != CurrentView){
             ToggleButton tb=(ToggleButton)PreviousView.findViewById(R.id.togglebutton);
             tb.setChecked(false);
            }

          PreviousView=CurrentView;
         }
    });
  }
}