0

我的应用程序在列表的一行中生成一个列表视图(即音频列表),其中包含 2 个按钮和 3 个文本视图。这两个按钮通过使用实现基本适配器的适配器变得可点击。其中一个按钮用于意图视图另一个播放音频。单击播放按钮时,我更改了按钮的背景(播放)。但问题是多个播放按钮的背景已更改,如位置 0、10、20、31、41 ......当我单击 0 该行,我还想知道如何更改所选列表视图的背景以区分正在播放的音频与其他音频...

请..向我建议解决方案..提前感谢以下我的适配器类

public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.lv_layout1, null);
            holder = new ViewHolder();
            holder.Title = (TextView) convertView.findViewById(R.id.Title);
            holder.Date = (TextView) convertView.findViewById(R.id.Date);
            holder.Time = (TextView) convertView.findViewById(R.id.Time);
            holder.Duration = (TextView) convertView.findViewById(R.id.Duration1);
            holder.view = (Button) convertView.findViewById(R.id.View);
            holder.play = (Button) convertView.findViewById(R.id.Play);
            holder.r_layout = (RelativeLayout) convertView.findViewById(R.id.r_layout);


            holder.Duration.setVisibility(View.INVISIBLE);

            holder.play.setBackgroundResource(0x7f020009);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.play.setOnClickListener(new View.OnClickListener() 
        {

            public void onClick(View convertView) throws IllegalStateException 
            {
                // TODO Auto-generated method stub

                //here is code for play/stop the audio
holder.play.setBackgroundResource(0x7f020009);





            }
        });
        holder.view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View convertView) {
                // TODO Auto-generated method stub
                Log.i("View","hai");
                Intent i=new Intent(context,audio_player.class);
                Bundle bd=new Bundle();

                String sel=itemDetailsrrayList.get(position).getTitle();
                String dur=itemDetailsrrayList.get(position).getDuration();


                bd.putString("audioID",sel);
                bd.putString("totalDuration",dur);

                i.putExtras(bd);

                System.out.println("After intent called");



                context.startActivity(i);
                ((Activity) context).finish();



            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView Title,Date,Time,Duration;
        ListView lv;
        Button view,play;
        RelativeLayout r_layout;
    }
}
4

4 回答 4

0

你可以试试这个:

if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.lv_layout1, null);
        holder = new ViewHolder();
        holder.Title = (TextView) convertView.findViewById(R.id.Title);
        holder.Date = (TextView) convertView.findViewById(R.id.Date);
        holder.Time = (TextView) convertView.findViewById(R.id.Time);
        holder.Duration = (TextView) convertView.findViewById(R.id.Duration1);
        holder.view = (Button) convertView.findViewById(R.id.View);
        holder.play = (Button) convertView.findViewById(R.id.Play);
        holder.r_layout = (RelativeLayout) convertView.findViewById(R.id.r_layout);


        holder.Duration.setVisibility(View.INVISIBLE);




        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
 holder.play.setBackgroundResource(0x7f020009);
于 2013-01-28T07:00:01.863 回答
0

好吧,我前段时间遇到过同样的问题,这里是解决方案:首先,将您的代码放在, 设置并重置每个按钮的背景的BindView部分,customAdapter因为只有没有重置的设置才会将背景更改应用于所有按钮滚动时列表中的按钮,请注意上下文更改。例如,要在单击时更改文本,请在单击之前获取文本视图,而不是在其中。检查下面的代码并评论以进行澄清。PS:不要复制/粘贴代码,它不是 100% 正确的,因为我使用了我的完整代码并删除了无用的部分。

class ViewHolder {
TextView PhoneTV;
ImageButton player;

}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
    ViewHolder holder;
    Log.i("myadapter", "bindView");
    if (view != null) {
    holder = new ViewHolder();
    holder.PhoneTV = (TextView) view.findViewById(R.id.one);
    holder.player = (ImageButton)view.findViewById(R.id.imageButton1);
    } else {
         holder = (ViewHolder) view.getTag();
        } 
                if(there is audio file){
        holder.player.setBackgroundResource(R.drawable.play_icon);
            holder.player.setVisibility(View.VISIBLE);
        holder.player.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
        final ImageButton playPause = (ImageButton) v.findViewById(R.id.imageButton1);
                Log.i("click", "Clicked");
                    try{
                        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                // TODO Auto-generated method stub
                                playPause.setBackgroundResource(R.drawable.play_icon);
                            }
                        });

                        if(mediaPlayer.isPlaying()){
                            Log.i("click", "Pausing");
                            mediaPlayer.pause();    
                            playPause.setBackgroundResource(R.drawable.play_icon);
                            Toast.makeText(localContext, "Paused ", Toast.LENGTH_SHORT).show();
                        }


                        else{


                            new AsyncTask<MediaPlayer, String, String>(){

                                ProgressDialog pd;
                                AsyncTask at = this;

                                protected void onPreExecute() {
                                    // do pre execute stuff...
                                    super.onPreExecute();
                                    try{
                                        this.pd = new ProgressDialog(localContext);
                                        this.pd.setMessage("Please Wait..");
                                        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                        this.pd.setOnCancelListener(new OnCancelListener() {

                                            @Override
                                            public void onCancel(DialogInterface dialog) {
                                                // TODO Auto-generated method stub
                                                at.cancel(true);
                                                playPause.setBackgroundResource(R.drawable.play_icon);
                                                Toast.makeText(localContext, "canceled", Toast.LENGTH_SHORT).show();

                                            }
                                        });
                                        this.pd.show();
                                    }catch(NullPointerException n){
                                        Log.i("play", "NPE pre");
                                    }
                                }

                                @Override
                                protected String doInBackground(MediaPlayer... params) {
                                    // TODO Auto-generated method stub
                                    try {
                                        mediaPlayer.reset();
                                        Log.i("click", "inside datasource and here it is: "+rec);
                                        mediaPlayer.setDataSource(recording_file);

                                        mediaPlayer.prepare();
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } 
                                    Log.i("click", "Playing");

                                    return null;
                                }
                                protected void onPostExecute(String result) {
                                    // do post execute stuff...
                                    try{
                                        mediaPlayer.start();
                                        this.pd.dismiss();
                                    }catch(NullPointerException n){
                                        Log.i("play", "NPE post");
                                    }

                                }


                            }.execute(mediaPlayer);
                            try{
                                playPause.setBackgroundResource(R.drawable.pause_icon);
                                //Toast.makeText(localContext, "Playing..", Toast.LENGTH_SHORT).show();
                            }catch(NullPointerException n){
                                Log.i("play", "NPE out");
                            }
                        }
                    }
                    catch(IllegalStateException il){
                        Log.i("exception", "Player Illegal State Exception");
                    }
                }

            }
        });
于 2013-05-21T06:23:58.750 回答
0

尝试这个,

我面临同样的问题。我在里面使用按钮getViewButton单击选项不起作用。而不是Button善意地使用textView. 然后写OnClickListener()

 holder.text.setOnClickListener(new View.OnClickListener() {

    public void onClick(View convertView) {
   }
  };

它会起作用的。

于 2013-01-28T07:10:39.847 回答
0

在很多情况下,您想知道单击项目的行位置,并且想知道该行中的哪个视图被点击。这在平板电脑 UI 中将变得更加重要。

您可以使用以下自定义适配器执行此操作:

private static class CustomCursorAdapter extends CursorAdapter {

    protected ListView mListView;

    protected static class RowViewHolder {
        public TextView mTitle;
        public TextView mText;
    }

    public CustomCursorAdapter(Activity activity) {
        super();
        mListView = activity.getListView();
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // do what you need to do
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = View.inflate(context, R.layout.row_layout, null);

        RowViewHolder holder = new RowViewHolder();
        holder.mTitle = (TextView) view.findViewById(R.id.Title);
        holder.mText = (TextView) view.findViewById(R.id.Text);

        holder.mTitle.setOnClickListener(mOnTitleClickListener);
        holder.mText.setOnClickListener(mOnTextClickListener);

        view.setTag(holder);

        return view;
    }

    private OnClickListener mOnTitleClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
            Log.v(TAG, "Title clicked, row %d", position);
        }
    };

    private OnClickListener mOnTextClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
            Log.v(TAG, "Text clicked, row %d", position);
        }
    };
}
于 2013-01-28T07:14:12.287 回答