RecyclerView Element Shows text with only one background

I have just created recycler view contains text and checkbox I assumed I created an adapter for this item Recycler adapter class

   package abtech.waiteriano.com.waitrer.adapters;

/**
 * Created by dell on 7/13/2017.
 */

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import abtech.waiteriano.com.waitrer.R;
import cn.refactor.library.SmoothCheckBox;

public class ItemModifierAdapter extends RecyclerView.Adapter<ItemModifierAdapter.ItemModifierVierHolder> {
    private Context mContext;
    static ArrayList<ItemModifierModel> itemModifierModels;
    static ArrayList<Boolean> positionArray;


    public ItemModifierAdapter(Context context, ArrayList<ItemModifierModel> itemModifierModels) {
        mContext = context;
        this.itemModifierModels = itemModifierModels;
        positionArray = new ArrayList<Boolean>(itemModifierModels.size());
        for (int i = 0; i < itemModifierModels.size(); i++) {
            positionArray.add(false);
        }

    }

    public static class ItemModifierVierHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CardView cv;
        Context c;
        TextView itemName, modifierName;
        SmoothCheckBox Iv1;
        private SparseBooleanArray selectedItems;
        RelativeLayout RV1;
        private Context mContext;

        ItemModifierVierHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            Iv1 = (SmoothCheckBox) itemView.findViewById(R.id.iv1);
            modifierName = (TextView) itemView.findViewById(R.id.modifierItemNameTv);
            RV1 = (RelativeLayout) itemView.findViewById(R.id.rv1);
            //set OnclickListener
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Iv1.toggle();
            Iv1.mTickDrawing = false;
            Iv1.mDrewDistance = 0;
            if (Iv1.isChecked()) {
//                RV1.setTag(true);
//              `  Iv1.setVisibility(View.VISIBLE);
//                Iv1.setChecked(false);
                positionArray.set(position, true);
                Iv1.startCheckedAnimation();


            } else {
//                Iv1.setVisibility(View.INVISIBLE);
//                Iv1.setChecked(true);
                positionArray.set(position, false);
                Iv1.startUnCheckedAnimation();
//                RV1.setTag(null);

            }
            Toast.makeText(itemView.getContext(), "Position: " + itemModifierModels.get(Integer.parseInt(getAdapterPosition() + "")).getSer(), Toast.LENGTH_LONG).show();

        }
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public ItemModifierVierHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_modifier, viewGroup, false);
        ItemModifierVierHolder imvh = new ItemModifierVierHolder(v);
        return imvh;
    }

    @Override
    public void onBindViewHolder(ItemModifierVierHolder itemModifierVierHolder, final int position) {
        for (int i = 0; i < itemModifierModels.get(position).modifierName.length; i++) {
            if (i == 0)
                itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[i]);
            else
                itemModifierVierHolder.modifierName.setText(itemModifierVierHolder.modifierName.getText().toString() + "\n" + itemModifierModels.get(position).modifierName[i]);

        }
//        itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[0]);
        itemModifierVierHolder.RV1.setFocusable(false);
        itemModifierVierHolder.Iv1.setChecked(positionArray.get(position));

    }

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

      

and added this element to line []

Modifiers = new ArrayList<>();
Modifiers.add(new ItemModifierModel(1,(new String []{"Roomy Roll","Kiri Roll","Roomy Roll","Eggs Roll","Modifier 5","Modifier 6"}) ));
Modifiers.add(new ItemModifierModel(16, (new String []{"Eggs Roll"})));
Modifiers.add(new ItemModifierModel(23,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(43,(new String []{"Roomy Roll"})));
Modifiers.add(new ItemModifierModel(50,(new String []{"Roomy Roll"})));
Modifiers.add(new ItemModifierModel(56,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(63,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(73,(new String []{"Eggs Roll"})));
Modifiers.add(new ItemModifierModel(80,(new String []{"Eggs Roll"})));

      

and this is my model

package abtech.waiteriano.com.waitrer.adapters;

/**
 * Created by dell on 7/13/2017.
 */

public class ItemModifierModel {

    String[] modifierName;
    int ser;

    ItemModifierModel(int ser, String[] modifierName) {

        this.modifierName = modifierName;
        this.ser = ser;
    }

    public String[] getModifierName() {
        return modifierName;
    }




    public int getSer() {
        return ser;
    }


}

      

but it only shows one text like this enter image description here all i need, i want to show other texts and i also need these texts that show one after another and under each other should i make another adapter and model for this text?

I want to create something like this repeating text with the same text view, but with a different name, enter image description here hope this is clear.

+3


source to share


3 answers


Please check my solution, hope this helps you:

package com.app.practiceapp;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

    String[] namesArray1 = new String[]{"Cupcake", "Donut", "Eclair", "Froyo"};

    Model model1 = new Model();
    model1.setNamesArray(namesArray1);

    String[] namesArray2 = new String[]{"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean"};
    Model model2 = new Model();
    model2.setNamesArray(namesArray2);

    String[] namesArray3 = new String[]{"Kitkat", "Lollipop", "Marshmallow", "Nougat"};
    Model model3 = new Model();
    model3.setNamesArray(namesArray3);

    String[] namesArray4 = new String[]{"Prashant", "Android", "Developer"};
    Model model4 = new Model();
    model4.setNamesArray(namesArray4);

    ArrayList<Model> modelList = new ArrayList<>();

    modelList.add(model1);
    modelList.add(model2);
    modelList.add(model3);
    modelList.add(model4);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.setAdapter(new MyAdapter(modelList));

}


class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private ArrayList<Model> modelList;

    MyAdapter(ArrayList<Model> myList) {

        this.modelList = myList;

    }

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

        return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview, parent, false));

    }

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

        for (int i = 0; i < modelList.get(position).getNamesArray().length; i++) {

            TextView textView = new TextView(MainActivity.this);
            textView.setGravity(Gravity.CENTER);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 10, 10, 10);
            textView.setLayoutParams(layoutParams);

            textView.setPadding(10, 10, 10, 10);
            textView.setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.holo_blue_light));

            textView.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.white));
            textView.setText(modelList.get(position).namesArray[i]);

            holder.itemContainerLayout.addView(textView);

        }


    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final LinearLayout itemContainerLayout;

        public MyViewHolder(View itemView) {
            super(itemView);

            itemContainerLayout = (LinearLayout) itemView.findViewById(R.id.containerLayout);
        }
    }
}

class Model {

    private String[] namesArray;

    public void setNamesArray(String[] namesArray) {
        this.namesArray = namesArray;
    }

    public String[] getNamesArray() {

        return namesArray;
    }


}
}

      

Here's the main layout of the xml operation:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.practiceapp.MainActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

      



and this is the layout of the Recycler View xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">

</LinearLayout>

      

Please let me know if this is helpful for your requirement.

enter image description here

+1


source


onBindViewHolder

you put it

itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[0]);

      



Since you are explicitly setting one modifier by doing modifierName[0]

this, it shows a single entry.

If you want to add more TextViews dynamically, wrap the modifierName

TextView with a different layout. After that, you can dynamically add TextViews to it with each item modifierName

.

+1


source


I have an ugly solution, try ItemModifierVierHolder to create a helper recyclerView and then add checkBox + textView for each of the helper data items

0


source







All Articles