Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

android - Add different number of arguments in the recyclerview

I have this Recyclerview and populated it with CardView. I want to add different numbers of arguments in each CardView, how can I do this? Is it possible? It's working if I have the same number of arguments but that's not what I wanted.

Edit: I added the following HelperClass and Adapter

Example

main.add(new HelperClass(R.drawable.image, "item1", "item2", "item3"));
main.add(new HelperClass(R.drawable.image2, "item1", "item2", "item3"));

I only want to add 2 items in image 2 like this

main.add(new HelperClass(R.drawable.image2, "item1", "item2"));

I'm getting this error

Expected 4 arguments but found 3

This is the HelperClass

public class HelperClass {

int image;
String item1, item2, item3;

public HelperClass (int image, String item1, String item2, String item3) {
    this.image = image;
    this.item1 = item1;
    this.item2 = item2;
    this.item3 = item3;
}

public int getImage() {
    return image;
}

public String getItem1() {
    return item1;
}

public String getItem2() {
    return item2;
}

public String getItem3() {
    return item3;
}

This is the Adapter

ArrayList<HelperClass> main;
Context context;
public MainAdapter(ArrayList<HelperClass> main, Context context) {
    this.main = main;
    this.context = context;
}


@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card_design,parent,false);
    MainViewHolder mainViewHolder = new MainViewHolder(view);
    return mainViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {

    final HelperClass helperClass = main.get(position);
    holder.image.setImageResource(helperClass.getImage());
    holder.item1.setText(helperClass.getItem1());
    holder.item2.setText(helperClass.getItem2());
    holder.item3.setText(helperClass.getItem3());
   
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, DetailsActivity.class);
            intent.putExtra("image", helperClass.getImage());
            intent.putExtra("item1", helperClass.getItem1());
            intent.putExtra("item2", helperClass.getItem2());
            intent.putExtra("item3", helperClass.getItem3());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return main.size();
}

public static class MainViewHolder extends RecyclerView.ViewHolder{

    ImageView image;
    TextView item1, item2, item3;

    public MainViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image);
        item1 = itemView.findViewById(R.id.item1);
        item2 = itemView.findViewById(R.id.item2);
        item3 = itemView.findViewById(R.id.item3);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can make Helper class accept varargs. Please change constructor like this.

public HelperClass(@DrawableRes int image, String... items) {
  this.image = image;
  if (items.length > 0) {
     this.item1 = items[0]; // first item
  }
  
  if (items.length > 1) {
     this.item2 = items[1]; // second item
  }

  if (items.length > 2) {
     this.item3 = items[2]; // third item
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...