0

getView function is called many times hence the childview is being shown multiple times.I don't know why this is happening,the childrows are shown by 5 times as I have 5 textview in a row.here is my code getChildView() and getChild() of expandablelistview:

    public View getChildView(int groupPosition,int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {

    final List<String> childText = getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.childrow, null);
    }

    final TextView address = (TextView)convertView.findViewById(R.id.address);
    final TextView timings = (TextView)convertView.findViewById(R.id.timings);
    final TextView telephone = (TextView)convertView.findViewById(R.id.telephone);
    final TextView citcoun = (TextView)convertView.findViewById(R.id.citcoun);

    address.setText(childText.get(0));
    timings.setText(childText.get(1));
    telephone.setText(childText.get(2));
    String citcounT = childText.get(3)+","+childText.get(4);
    citcoun.setText(citcounT);

    return convertView;
}

  public List<String> getChild(int groupPosition, int childPosition) {

    return childnames.get(parentNames.get(groupPosition));
}

And its the xml of expnadablelist

<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

       >

   <ExpandableListView
       android:id="@+id/storelist"
       android:layout_width="match_parent"
       android:layout_height="0dip"
       android:layout_weight="1" >

   </ExpandableListView>

   </LinearLayout>

this is childrow.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="79-A HBFC,Faisla Town,Lahore" />

<TextView
    android:id="@+id/citcoun"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="20dp"
    android:layout_below="@+id/address"
    android:layout_marginTop="10dp"
    android:text="Lahore" />

<TextView
    android:id="@+id/timings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/citcoun"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="Mon-Sat: 9 am-10 pm" />

<TextView
    android:id="@+id/telephone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/timings"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:text="0346-4678570" />

Kindly help me if u know what's wrong with this

Szymon
  • 42,577
  • 16
  • 96
  • 114
Hamza Khalil
  • 481
  • 1
  • 7
  • 18

2 Answers2

0

2 things look strange :

  • convertView = infalInflater.inflate(R.layout.childrow, null); should inflate the view in the parent view group : convertView = infalInflater.inflate(R.layout.childrow, parent);
  • you data structures seems confusing, why don't you use childPosition parameter in getChild() ? Didn't you miss something ?
Szymon
  • 42,577
  • 16
  • 96
  • 114
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Thanx for commenting @snicolas.i did not miss it i just write it here. & by sending parameter parent in place of null i got this "UnSupportedOperationException" having detail msg of "addView(View, LayoutParams) is not supported in AdapterView" – Hamza Khalil Nov 22 '13 at 07:29
  • Ok for parent view group. But still, are you sure children should not be a `List>` so that each child in a given parent group can be different ? – Snicolas Nov 22 '13 at 07:56
  • exactly i think i am doing something wrong in adding the child in parent – Hamza Khalil Nov 22 '13 at 10:45
  • here is my updated code and question http://stackoverflow.com/questions/20143104/adding-children-to-parent-in-expandablelistadapter – Hamza Khalil Nov 22 '13 at 10:54
0

you should replace this

if (convertView == null) {
    LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.childrow, null);
}

with

LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.childrow, null);