How to divide nested ArrayList to the every layout at TabLayout
Asked Answered
A

3

11

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

Actinouranium answered 27/10, 2015 at 9:18 Comment(16)
By iterating over all of your lists of data you'll always end up with the data from the last list. Instead you should pass to your adapter a int value representing the tab indicator, along with the position from getView() you can retrieve the proper data, something like this: tv1.setText(keys.get(tabIndicator).get(position)); tv2.setText(values.get(tabIndicator).get(position));Silverstein
If I wrong, please correct me getView works more than tabIndicator size ? So how can I get tabIndicator ?Actinouranium
So how can I get tabIndicator - you should know this when you create the adapter for that tab, at that moment pass this information to the adapter.Silverstein
aren't you assign all the values to the same two textviews? tv1 and tv2?Signe
Another important note: please don't use for to loop on your ArrayLists. use iterator, it is much more efficient .Signe
@William Kinaan I want to assign different values to textviews, i explained with images in questionActinouranium
Anyway , how can i use iterator for nested array. Like this ? : Iterator it = arraylist.iterator(); --- it.iterator() again ?Actinouranium
@Actinouranium iterate like this: for (ArrayList<Object> mKeys : keys) {for (Object object : mKeys) {}}Signe
you need to add your views dynamically inside your for loop for each arraylistSigne
I have never used the TabLayout class. I believe the position paramter of getView() is the tab position. Correct me if I am wrong. If I am correct, then I don't see how the current user interface will work. How would a user select a certain TextView item?Granvillegranvillebarker
You're using 2 ArrayLists. I am guessing you're using them as a pair matched by their indexes. Have you ever thought of using HashTable/Hashmap instead?Granvillegranvillebarker
@The Original Android this adapter is fragment's adapter. So getView is row number of list viewActinouranium
Thanks Salih. I did not know the View is a ListView and there is no tag of it. Perhaps it's a good idea to make it clear on your post about it.Granvillegranvillebarker
getView itself is kinda like a for loop, and gives you the position, why are you using another for loop inside it?! just remove those two for loops and only write tv1.setText(String.valueOf(mKeys.get(position))); tv2.setText(String.valueOf(mValues.get(position)));Thithia
Thank you @M D P but how can I get mValues and mKeys with position ? Suppose position give me 4 but keys size is 3 ?Actinouranium
@salih, It seems your original issue is related to data structure consisting of keys mapped to values, or a key mapped to values.Granvillegranvillebarker
G
1

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.

Granvillegranvillebarker answered 29/10, 2015 at 17:14 Comment(0)
A
1

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);
    }
}
Antecede answered 8/11, 2015 at 4:6 Comment(2)
If this worked feel free to mark this as the answer ☺.Antecede
This didnt work. You may think why this didnt work. In order to work this code, I have to create adapter dynamicly but I could not afford to create adapter dynamically.Actinouranium
G
0

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.

Granvillegranvillebarker answered 8/11, 2015 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.