设置在recycleview中的视图中单击并在android中更改另一个图标查看recycleview

时间:2017-08-29 09:33:17

标签: android android-recyclerview recycler-adapter

我希望当用户,例如点击回收者视图内第一个视图中的播放按钮时,另一个视图说再循环视图中的第二个视图更改为播放按钮图像。但android studio出错并说

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.ImageButton.setImageResource(int)'在空对象引用上

如果用户在按钮1中单击,则按钮2仅在循环视图列表中更改图像按钮 或用户点击所有人按钮更改按钮[2] .image

我的代码在这里请帮帮我

public class DataAdapter33 extends RecyclerView.Adapter<DataAdapter33.MyViewHolder> {
public MediaPlayer mediaPlayer=new MediaPlayer();
public int i=0;
Context context;
Handler Myhandler;
public byte[] Image = null;
private List<Data> DataList = new ArrayList<>();
public DataAdapter33(Context context) {
    this.context = context;
}
public void setData(List<Data> DataList) {
    this.DataList = DataList;
    notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
    public TextView id, setter, singer, musicName, arrengment;
    public ImageButton btn_ring, btn_loop, btn_download;
  public   ImageButton btnplay_stop[] = new ImageButton[9];
    public String url = null;
    public SeekBar seekBarProgress;
    public int mediaFileLengthInMilliseconds; // gets the song length in milliseconds from URL
    public final Handler handler = new Handler();
    public ImageView image;
    public MyViewHolder(View view) {
        super(view);
        i++;
        musicName = (TextView) view.findViewById(R.id.TitleMusic);
        arrengment = (TextView) view.findViewById(R.id.arangement);
        image = (ImageView) view.findViewById(R.id.artistimage);
      btnplay_stop[i] = (ImageButton) view.findViewById(R.id.btnPlay_Puase);
        btn_download = (ImageButton) view.findViewById(R.id.btnDownload);
        btn_loop = (ImageButton) view.findViewById(R.id.btnLoop);
        btn_ring = (ImageButton) view.findViewById(R.id.btnring);
        seekBarProgress = (SeekBar) view.findViewById(R.id.SeekBarTestPlay);
        seekBarProgress.setMax(99); // It means 100% .0-99
        seekBarProgress.setOnTouchListener(this);
           btnplay_stop[i].setTag(i);
        btnplay_stop[i].setOnClickListener(this);
        btn_download.setOnClickListener(this);
        btn_loop.setOnClickListener(this);
        btn_ring.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnPlay_Puase: {
                int index = (Integer) v.getTag();
                Toast.makeText(context, ""+index, Toast.LENGTH_SHORT).show();
                btnplay_stop[2].setImageResource(R.drawable.disconenct);
            }
            break;
            case R.id.btnDownload:
                break;
            case R.id.btnLoop:
                break;
            case R.id.btnring:
                break;
        }
    }
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_musics_view, parent, false);
    MyViewHolder holder = new MyViewHolder(itemView);
    Log.d("DataAdapter", "On Create View Holder Called ");
    return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Data data = DataList.get(position);
    Log.d("DataAdapter", "On BindView Holder " + position);
    holder.musicName.setText(data.getMusicName());
    holder.arrengment.setText(data.getArrengment());
    holder.url = data.getUrl();
    MyrunUI m=new MyrunUI();
    m.runin(context,holder,data);
}
@Override
public int getItemCount() {
    return DataList.size();
}
}

我不请英语请帮忙 如果用户单击button1,则按钮2仅在循环视图列表中更改图像按钮 要么 或用户点击所有人按钮更改按钮[2] .image 感谢

我的抽象代码的图像 我在Android工作室的错误 enter image description here

1 个答案:

答案 0 :(得分:0)

查看您的代码,您能告诉我您的数据大小吗?每次调用viewHolder时,都会增加i的值,并根据该值初始化图像按钮。但在onclick中,您只指定图像按钮数组的第二个索引。可能会发生您的数据大小小于2或由于任何其他原因无法初始化,这就是为什么它在那里给你NullPointer。