Home » Android » android – How to divide nested ArrayList to the every layout at TabLayout

android – How to divide nested ArrayList to the every layout at TabLayout

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using nested ArrayList for Tablayout. Each element of ArrayList populate each Tab. Tab count is change according to parent arraylist’s size. For example When parent ArrayList’s size 2 , tab count 2; when size 3 tab count 3..etc..In order to explain this situation is difficult, I prepared expalantion image..

Image 1

This image represents general apperance. I want to populate this TextViews with ArrayLists each element

Image 2

This image represents my data type;
keys and values ArrayList is nested arraylist and they contain arraylist in each index.

Image 3 – Image 4 – Image 5

This images represents how tabLayouts used to be.I want my data looks like this images.

So issue is PopUpDetailsAdapterPlanT Class;

When I use codes below each row populate with same item. For this example every row writes last element of ArrayList (Location – Minnesota) How can I handle this issue. Thanks for helpings

public class PopUpDetailsAdapterPlanT extends ArrayAdapter {
Context context;
private ArrayList<ArrayList<Object>> keys;
private ArrayList<ArrayList<String>> values;

    public PopUpDetailsAdapterPlanT(Context context, ArrayList<ArrayList<Object>> keys, ArrayList<ArrayList<String>> values) {
        super(context,R.layout.popupdetails_listview_simpleitem);
        this.keys = keys;
        this.values = values;
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.popupdetails_listview_simpleitem, null);
        }

        TextView tv1 = (TextView) v.findViewById(R.id.hashmapKeys);
        TextView tv2 = (TextView) v.findViewById(R.id.hashmapValues);

        for (int i = 0; i < keys.size(); i++) {
            ArrayList<Object> mKeys = new ArrayList<>(keys.get(i));
            ArrayList<String> mValues = new ArrayList<>(values.get(i));
            for (int j = 0; j <mKeys.size() ; j++) {
                tv1.setText(String.valueOf(mKeys.get(j)));
                tv2.setText(String.valueOf(mValues.get(j)));
            }

        }
        return v;
    }

    @Override
    public int getCount() {
        return keys.get(0).size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= keys.size())
            return values.get(position);
        return keys.get(position);
    }
}

enter image description here

How to&Answers:

I hope I understand your requirements and code correctly. I understand you only have 2 TextViews in the layout for 10 different texts. So…in this case, you need 10 TextViews for the ObjectID, row #, name, age, surname, location, etc. You cannot have only 2 TextViews.

Let’s start with this and hopefully the issues will get clarified for all of us.

Answer:

If you can pass relevent tab index to each fragment’s adapter. You can change the fragment’s adapter as below and try:

public class PopUpDetailsAdapterPlanT extends ArrayAdapter {
    Context context;
    private ArrayList<Object> keys;
    private ArrayList<String> values;

    public PopUpDetailsAdapterPlanT(Context context, ArrayList<ArrayList<Object>> keys, ArrayList<ArrayList<String>> values, int tabIndex) {
        super(context,R.layout.popupdetails_listview_simpleitem);
        this.keys = keys.get(tabIndex);
        this.values = values.get(tabIndex);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.popupdetails_listview_simpleitem, null);
        }

        TextView tv1 = (TextView) v.findViewById(R.id.hashmapKeys);
        TextView tv2 = (TextView) v.findViewById(R.id.hashmapValues);


        tv1.setText(String.valueOf(keys.get(position)));
        tv2.setText(String.valueOf(values.get(position)));

        return v;
    }

    @Override
    public int getCount() {
        return keys.size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= keys.size())
            return values.get(position);
        return keys.get(position);
    }
}

Answer:

I am suggesting a different data structure for you, based on your comments under the posted problem.
Sample code:

private ArrayList<Integer> keys;
private ArrayList<Person> values;

Notes:

  • Object keys contain the primary key IDs.
  • Object values contain a class, I call it Person. Person is a class containing name, Surname, age, and Location. The class has the flexibility of adding another attribute.

If this design seems right for you, then we can discuss it with more details.