getView函数被多次调用

时间:2013-11-22 06:41:19

标签: android android-listview expandablelistview expandablelistadapter

多次调用

getView函数,因此多次显示子视图。我不知道为什么会发生这种情况,因为我连续5个textview,所以显示5次。我的expandablelistview的代码getChildView()getChild()

    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));
}

它是expnadablelist的xml

<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>

这是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" />

如果你知道这个错误的话,请帮助我

2 个答案:

答案 0 :(得分:0)

2件事看起来很奇怪:

  • convertView = infalInflater.inflate(R.layout.childrow, null);应该对父视图组中的视图进行充气:convertView = infalInflater.inflate(R.layout.childrow, parent);
  • 您的数据结构似乎令人困惑,为什么不在childPosition中使用getChild()参数?你有没有错过什么?

答案 1 :(得分:0)

你应该替换这个

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

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