0

I have a ArrayList that contain a HashMap. One HashMap Object have 3 key, Value pair.

So it is like this....

Category : A
Name     : Name 1
Price    : 100

I want to do group by Category and make a list view like this.....

A
===============
Name 1     100
Name 2     100
Name 3     100

B
===============
Name 1     100
Name 2     100
Name 3     100

C
===============
Name 1     100
Name 2     100
Name 3     100

but my listview.xml is like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content""
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/card_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/card_type_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

My question is how do I group this arraylist according to Category and have a list view. How I dynamically change the behavior of this list view ?? Because my list view contain all the time these 3 fields.

What is the option that I have ??

Dev513
  • 51
  • 1
  • 10
  • so what you want is to sort the array list based on the category and make all category A items shows first in the listview and B second and C...like this? – Surely Jun 05 '15 at 09:46
  • You can create object type arraylist for this.. – gunjan luthra Jun 05 '15 at 09:51
  • @xwhyLikeThis Yes of cause . – Dev513 Jun 05 '15 at 09:53
  • So you just need to sort the array...for how to sort a list of hashmap, refer to this post: http://stackoverflow.com/questions/5369573/how-sort-an-arraylist-of-hashmaps-holding-several-key-value-pairs-each – Surely Jun 05 '15 at 11:01
  • more than sort I want to group under the category name – Dev513 Jun 05 '15 at 11:14

1 Answers1

0

I'll just talk generally on some points

  1. For the list view item layout you might need an extra view to display the category and possible one for the divider.
  2. Concerning the sorting. You can implement a comparator like @xwhyLikeThis mentioned. Check the link for details on how to do that. Now what to do with the sorted array list? When retrieving the items from your list adapter to display in your list view you just check the category of the items first so on this case position 1 on the listview adapter corresponds to the items with category A and so on. This would be much easier if there were only 3 items per category as your shown because you could just go straight to indexes at multiples of 3 in your array list and pick three consecutive items from there to display eg 0,1,2 will be category A and 3,4,5 will be B. All of this rides on whether the array list is actually sorted or not.

I apologize for all of the text as I can't write code for it right now (answering from my smart phone). Cheers!

PS: The question title is a lot misleading. But if you're interested in knowing you can change a textviews visibility with

mTextView.setVisibility(View.VISIBLE OR View.INVISIBLE OR View.GONE)

EDIT

I see you have asked this same question here also. The answer and code example that was given there should do the trick. Please try to be specific about your issues.

Community
  • 1
  • 1
efemoney
  • 3,616
  • 1
  • 14
  • 16