简体   繁体   中英

I need to implementation click listener class on item in recycle view when using adapter and array list

I want when user click on item in recycle move on second activity. I have the following adapter:

public class AdapterWassafat extends RecyclerView.Adapter<AdapterWassafat.ViewHolder> {
    private ArrayList<Item> itemsList;
    private Context mContext;

    public AdapterWassafat(ArrayList<Item> itemsList, Context mContext) {
        this.itemsList = itemsList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wasafat,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = itemsList.get(position);
        holder.text.setText(item.getText());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text_card_main);
        }
    }
}

and I have this class to add on item list:

public class Item {
    private String text;

    public Item(String text) {
        this.text = text;
    }

    public Item() {
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

and this class has recycle:

private AdapterWassafat adapter;
private RecyclerView recyclerView ;
private ArrayList<Item> myList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wasafat);

    recyclerView = findViewById(R.id.wasafat_recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));

    adapter = new AdapterWassafat(getMyitem(),getBaseContext());
    recyclerView.setAdapter(adapter);
}
    private ArrayList<Item> getMyitem() {
    ArrayList<Item> list = new ArrayList<>();
    Item tips = new Item();
    tips.setText("وصفة التمر ");
    list.add(tips);

    tips = new Item();
    tips.setText("وصفة اللبن ");
    list.add(tips);

    tips = new Item();
    tips.setText("وصفة الشاي الاخضر ");
    list.add(tips);
    return list;}

What do I need to make a recycle view when user click on item move on another activity? Like, as user clicks on item وصفة الشاي, to swap or move on into this activity and so on. I tried a lot of ways to make it both select or click, but that didn't succeed.

In the Adapter:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Item item = itemsList.get(position);
    Context context = holder.itemView.getContext();
    holder.text.setText(item.getText());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = Intent(context, YourActivity.class);
        intent.putExtra("item", item.getText())
        context.startActivity(intent);
     }
  });
}

In your Other Activity, get data as:

// onCreate()
String value = getIntent().getStringExtra("item", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM