Displaying items in ListViews inside a RecyclerView

Máté Antal :

I have a List<CopyModel> copyData with all my data, I want to display.
The CopyModel class looks like this:

public class CopyModel {
  public String autoId;
  public List<String> fieldNames;
  public List<Object> values;
}

I wanna display items of fieldNames and values.
The exact data I get from FireStore:

enter image description here

I'm displaying them in a ListView, with simple_list_item_2.
Here's the code:

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_2, android.R.id.text1, copyData) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView t1 = view.findViewById(android.R.id.text1);
            TextView t2 = view.findViewById(android.R.id.text2);

            String tS1 = copyData.get(position).fieldNames.get(position);
            t1.setText(tS1);

            String tS2 = String.valueOf(copyData.get(position).values.get(position));
            t2.setText(tS2);

            return view;
        }
    };
    holder.copyList.setAdapter(adapter);
}

Current behavior: The recyclerView is showing 3 ListViews with:

  • copyData0.fieldNames0 => "default"
  • copyData0.values0 => "false"
  • copyData1.fieldNames1 => "Pont"
  • copyData1.fieldNames1 => "9"
  • copyData1.fieldNames1 => "entity_name"
  • copyData1.fieldNames1 => "Analízis pontok"

How I want it to work:
The RecyclerView would show as many lists as the size of copyData (in this case, 3), and each ListView would have as many rows as the size of its fieldNames (now it's 5, but can be any other)

EDIT:

    String tS1 = copyData.get(holder.getAdapterPosition()).fieldNames.get(position);
    t1.setText(tS1);

    String tS2 = String.valueOf(copyData.get(holder.getAdapterPosition()).values.get(position));
    t2.setText(tS2);

Now I'm getting the 3 seperate objects, in 3 seperate listview, but only the first 3 values from fieldNames and values

EDIT#2:

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

    for (int i = 0;i<copyData.size();i++) {
        final List<String> names = copyData.get(i).fieldNames;
        final List<Object> values = copyData.get(i).values;

        //noinspection unchecked
        final ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_2, android.R.id.text1, names) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView t1 = view.findViewById(android.R.id.text1);
                TextView t2 = view.findViewById(android.R.id.text2);

                String tS1 = names.get(position);
                t1.setText(tS1);
                String tS2 = String.valueOf(values.get(position));
                t2.setText(tS2);

                return view;
            }
        };
        holder.copyList.setAdapter(adapter);
    }
}

This way I can add every fieldname-value pair to the list, but it behaves like, it goes over these 2 lines
final List<String> names = copyData.get(i).fieldNames; final List<Object> values = copyData.get(i).values;
3 times, then displays all the fieldname-value pairs of the last object in 3 seperate ListViews

Tejas Pandya :

At a First Glance I think problem is

 String tS1 = copyData.get(position).fieldNames.get(position);

Here you are using position which is Listview's Adapters position . so your statement should

 String tS1 = copyData.get(Recycleview Adapter Position).fieldNames.get(Listview adapter position);

From Your Edit:

Now I'm getting the 3 seperate objects, in 3 seperate listview, but only the first 3 values

Reason:

Your First Adapter (Recyleview adapter) is running only three times.

If i understand your question . you need to do something like this:

1) set your Recycleview with list of copyData (which you are doing properly).

2) in onBindViewHolder of your recycleview. generate a full list of fieldNames and Values and Set your Listview Adapter

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
    YourCustomeListViewAdapter adapter = new YourCustomeListViewAdapter(younewlistwithfieldNamesandvalue)
        holder.copyList.setAdapter(adapter);
    }

EDIT 2

You are looping through copyData . which is wrong .

  for (int i = 0;i<copyData.size();i++) 

you need to loop through fieldname and values array.like this

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
 for (int i = 0;i<copyData.get(position).fieldNames.size();i++) {
  }
 for (int i = 0;i<copyData.get(position).values.size();i++) {
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105195&siteId=1