4

I have Just created a recycler view contains a text and a check box I supposed that I created an adapter for this Recycler Item 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 item in a String[ ]

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 shows Only just 1 text like this enter image description here all I need I want to shows the other texts,and I need also these texts shows behind each others and under each others shall I make another adapter and model for these text?

I want to create something like this repeating texts with the same text view but with different name enter image description here hope this gonna be clear

  • What do you mean by other texts? and behind each other? Can you explain a bit – Kapil G Jul 25 '17 at 14:06
  • @kapsym I supposed that added more than one texts In String[ ] like I post I shows only just one text I want show all the texts I added but every text shows in another text view not in the same text view ,just wanna show them in a row –  Jul 25 '17 at 14:09
  • Inside onBindViewHolder(), you have created a dynamic TextView , and setting text in it. Why are you doing it ? – Prashant Jul 25 '17 at 14:12
  • @Prashant I set Texts for it as it shows some texts from the data base,I want repeat this texts with different name as getting from the data base –  Jul 25 '17 at 14:16
  • @Nativony But that TextView is not used anywhere. I mean its not added in the view anywhere. – Prashant Jul 25 '17 at 14:20
  • @Prashant please check edits –  Jul 25 '17 at 14:22

3 Answers3

1

onBindViewHolder you put this

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

Since you're explicitly setting a single modifier by executing modifierName[0] thus, it shows the single entry.

If you want to add more TextViews dynamically then wrap modifierName TextView with another layout. After that, you can dynamically add TextViews to it with each modifierName item.

fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • sorry you mean that create another layout and put in this layout this text view and create for this layout in tha same adapter another layout out inflator and create another view in the same adapter and setview for this new layout to the parent layout? –  Jul 25 '17 at 15:06
  • No, I meant instead of `modifierItemNameTv` declare a layout here and add those dynamic textviews you created in the loop to that. Take a look at this. https://stackoverflow.com/questions/4203506/how-can-i-add-a-textview-to-a-linearlayout-dynamically-in-android – fluffyBatman Jul 25 '17 at 15:11
  • by the way I don't need add this text dydnamically please check my adapter class I edited It –  Jul 25 '17 at 15:19
  • Your expected behavior needs styling. I don't think you can achieve that with your current adapter code. But if you add the textviews dynamically then you can apply a common style over them. – fluffyBatman Jul 25 '17 at 15:25
1

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

zhangle
  • 140
  • 13
1

Please check my solution, hope it 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 Activity layout xml:

<?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 Recycler View item layout 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 its helpful for your requirement.

enter image description here

Prashant
  • 1,046
  • 14
  • 21