Mustafa Max
Mustafa Max

Reputation: 69

Retrieve data from RecyclerView

I use RecyclerView in the application using a custom adapter. I want to deal with the existing menu RecyclerView items. To access them like normal variables so that I can apply a specific condition or send them in a text message and so on outside of my Class adapter.

How to access RecyclerView items?

Code class matrix:

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);

        addItem();

        Button button = findViewById(R.id.get_item);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                    I want when I press the button
                    The items are called
                 */
            }
        });

    }

    public void addItem() {

        List<Shops> shops = new ArrayList<>();
        String[] cloth = {"Silk", "Cotton", "Linen"};
        String[] wood = {"Sandal", "Jawi", "Pine"};
        String[] metal = {"Window", "door", "roof"};

        RecyclerView recyclerView = findViewById(R.id.list_shop);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        for (int i = 0; i < cloth.length; i++) {

            Shops shopsList = new Shops(cloth[i], wood[i], metal[i]);

            shops.add(shopsList);
        }

        ShopsAdapter adapter = new ShopsAdapter(shops);
        recyclerView.setAdapter(adapter);
    }
}

code Class list

public class Shops {
    String cloth, wood, metal;

    public Shops(String cloth, String wood, String metal) {
        this.cloth = cloth;
        this.wood = wood;
        this.metal = metal;
    }
}

Code Class Adapter

public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.ShopsHolder> {

    private List<Shops> shopsList;

    public ShopsAdapter(List<Shops> shops) {
        this.shopsList = shops;
    }

    @Override
    public ShopsHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.template_recycler_view, viewGroup, false);
        ShopsHolder holder = new ShopsHolder(row);
        return holder;
    }

    @Override
    public void onBindViewHolder(ShopsHolder holder, int i) {

        Shops shops = shopsList.get(i);
        holder.cloth.setText(shops.cloth);
        holder.wood.setText(shops.wood);
        holder.metal.setText(shops.metal);

    }

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

    class ShopsHolder extends RecyclerView.ViewHolder {
        private TextView cloth, wood, metal;

        public ShopsHolder(View itemView) {
            super(itemView);
            cloth = itemView.findViewById(R.id.cloth);
            wood = itemView.findViewById(R.id.wood);
            metal = itemView.findViewById(R.id.metal);
        }
    }
}

If you need the XML code, I will put it in another reply.

Upvotes: 2

Views: 8282

Answers (3)

Satyajit
Satyajit

Reputation: 685

Create your model / POJO class with getters() and setters () like this

public class Shops {

    private String cloth;
    private String wood;
    private String metal;

    public Shops(String cloth, String wood, String metal) {
        this.cloth = cloth;
        this.wood = wood;
        this.metal = metal;
    }

    public String getCloth() {
        return cloth;
    }

    public void setCloth(String cloth) {
        this.cloth = cloth;
    }

    public String getWood() {
        return wood;
    }

    public void setWood(String wood) {
        this.wood = wood;
    }

    public String getMetal() {
        return metal;
    }

    public void setMetal(String metal) {
        this.metal = metal;
    }
}

Now declare the ArrayList globally and add items to it. When you click button simple access them with index number. You can run a for loop for that.

public class MainActivity extends AppCompatActivity {

private  List<Shops> shops;

protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    shops= new ArrayList<>();
    addItem();

    Button button = findViewById(R.id.get_item);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*
                I want when I press the button
                The items are called
             */
            shops.get(1).getCloth();
            shops.get(1).getWood();
            shops.get(1).getMetal();

            // do whatever you want with them

        }
    });

}

public void addItem() {


    String[] cloth = {"Silk", "Cotton", "Linen"};
    String[] wood = {"Sandal", "Jawi", "Pine"};
    String[] metal = {"Window", "door", "roof"};

    RecyclerView recyclerView = findViewById(R.id.list_shop);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    for (int i = 0; i < cloth.length; i++) {

        Shops shopsList = new Shops(cloth[i], wood[i], metal[i]);

        shops.add(shopsList);
    }

Upvotes: 1

Tăng Tấn T&#224;i
Tăng Tấn T&#224;i

Reputation: 351

Your code way seem meet the thing you need but i don't think it's a really good way. Instead of create a public list in some where in your project to cache the data you want, that's better to make this list private in your adapter and get the data you want through the adapter by adding a getItem(int pos) which return the item by the 'pos' param you pass to it, that's safer and more professional. There're many ways to solve a problem, but which is the best ...

Upvotes: 1

Mustafa Max
Mustafa Max

Reputation: 69

thank you. After repeated experimentation. I was able to reach a result. It is using For loob Inside you addItem So

  public void addItem() {

        String[] cloth = {"Silk", "Cotton", "Linen"};
        String[] wood = {"Sandal", "Jawi", "Pine"};
        String[] metal = {"Window", "door", "roof"};

        RecyclerView recyclerView = findViewById(R.id.list_shop);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        for (int i = 0; i < cloth.length; i++) {

            Shops shopsList = new Shops(cloth[i], wood[i], metal[i]);

            shops.add(shopsList);

        }

        ShopsAdapter adapter = new ShopsAdapter(shops);
        recyclerView.setAdapter(adapter);

    // From here begins the solution code
        for (int i = 0; i < cloth.length; i++) {

            String clothMy = shops.get(i).cloth;
            String woodMy = shops.get(i).wood;
            String metalMy = shops.get(i).metal;

            Log.i("TAG",clothMy);
            Log.i("TAG",woodMy);
            Log.i("TAG",metalMy);

        }
    // Here ends the solution code

    }

Upvotes: 0

Related Questions