0

I have a Arraylist that contain a hashmap. hashmap contain a Topic name, Name and value. For example hashmap object like this.(I did not add the key for the example.)

{"Topic A", "Name 1" , "100"}  
{"Topic A", "Name 2" , "100"}  
{"Topic A", "Name 3" , "100"}  
{"Topic A", "Name 4" , "100"}  
{"Topic B", "Name 1" , "100"}  
{"Topic B", "Name 2" , "100"}  
{"Topic B", "Name 3" , "100"}  
{"Topic B", "Name 4" , "100"}  
{"Topic C", "Name 1" , "100"}  
{"Topic C", "Name 2" , "100"}  
{"Topic C", "Name 3" , "100"}  
{"Topic C", "Name 4" , "100"}  
{"Topic D", "Name 1" , "100"}  
{"Topic D", "Name 2" , "100"}  

I want to make a list view like this

Topic A
=========================
name 1                100
name 2                100
name 3                100
name 4                100

Topic B
==========================
name 1                100
name 2                100
name 3                100
name 4                100


Topic C
==========================
name 1                100
name 2                100
name 3                100
name 4                100

Topic D
==========================
name 1                100
name 2                100
name 3                100
name 4                100

Here is my getView(int position, View convertView, ViewGroup parent) method................

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

    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_view_card_type, null);

    mTopic = (TextView) v.findViewById(R.id.topic);
    mItemname = (TextView) v.findViewById(R.id.name_of_item);
    mValue = (TextView)v.findViewById(R.id.value_of_item); 

    mTopic.setText(myArrayList.get(position).get("Topic"));

    mItemname.setText(myArrayList.get(position).get(
            "Name"));
    mValue.setText(myArrayList.get(position).get("Value"));
    return v;

}

Here is my list view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#ffffff"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/topic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />

    <TextView
        android:id="@+id/name_of_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />

    <TextView
        android:id="@+id/value_of_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />


</LinearLayout>

I try to do that with add a condition inside of the getView method using position value. But it confused me. Can please someone suggest me what to do..............

HpTerm
  • 8,151
  • 12
  • 51
  • 67
Dev513
  • 51
  • 1
  • 10
  • add some code of array list.. – Praveen B. Bhati May 20 '15 at 05:28
  • Without key how are you able to check this `myArrayList.get(position).get("Topic")?` Where did you set `Topic` as a key into hashmap ? – Piyush May 20 '15 at 05:32
  • create your item file with Topic and 1 row for name and value and just check if Topic value is same then set visibility of topic gone else visible – Jignesh Jain May 20 '15 at 05:32
  • items in listview are recycled every time the view changes. You need to hold view objects in ViewHolder. For example: http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html – Shahzeb May 20 '15 at 05:32
  • that 'myArrayList' has values. I checked with debug option. It is ok. I want to know how to add such a condition in 'getView' method. – Dev513 May 20 '15 at 05:36

2 Answers2

1

Do some thing like this, You can make changes, i am just adding a sample

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_view_card_type, null);

    mTopic = (TextView) v.findViewById(R.id.topic);
    mItemname = (TextView) v.findViewById(R.id.name_of_item);
    mValue = (TextView)v.findViewById(R.id.value_of_item); 

      String topic = "";

if(!(myArrayList.get(position).get("Topic").equals(topic )))
{
     mTopic.setText(myArrayList.get(position).get("Topic"));
      topic = myArrayList.get(position).get("Topic");
}else{
mTopic.setText("");}


    mItemname.setText(myArrayList.get(position).get(
            "Name"));
    mValue.setText(myArrayList.get(position).get("Value"));
    return v;

}
Sree
  • 3,136
  • 2
  • 31
  • 39
  • I try in your method. But it do not work. I feel that your method must work. I can't find what went wrong.. – Dev513 May 20 '15 at 08:25
  • What you are getting?? i have updated answer try that too and you will not get the exact as in question, you need to do some thing more – Sree May 20 '15 at 08:26
  • 1
    finally I have made it. Still it has some issues. But I think I can manage that. Thank you @Sree – Dev513 May 20 '15 at 11:45
0

Try this-

// define this as global
String lastTopic = "";

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_item, null);

    TextValue tv_topic = (TextView) v.findViewById(R.id.tv_topic);
    TextValue tv_item_name = (TextView) v.findViewById(R.id.tv_item_name);
    TextValue tv_item_value = (TextView) v.findViewById(R.id.tv_item_value);

    if (!(myArrayList.get(position).get("Topic").equals(lastTopic))) {

        tvTopic.setText(myArrayList.get(position).get("Topic"));
        lastTopic = myArrayList.get(position).get("Topic");

    } else {

        tvTopic.setVisibility(View.GONE);

    }

    tv_item_name.setText(myArrayList.get(position).get("Name"));
    tv_item_value.setText(myArrayList.get(position).get("Value"));

    return v;

}
NehaK
  • 2,639
  • 1
  • 15
  • 31