为什么ExpandableListView的孩子在扩展和折叠任何组后都会被洗牌

时间:2014-03-27 09:39:36

标签: android android-fragments expandablelistview

我的问题是,当我第一次点击任何组项时,它会扩展并正常工作。但是,当我点击其他组时,它会显示错误的子项。错误的是我的意思是儿童用品被洗牌并相互替换。

我使用导航类型“操作栏标签(带有查看寻呼机)”

创建了活动

适配器代码:

package pointofsale.adapters;

import java.util.List;
import java.util.Map;

import com.example.pointofsale.R;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class RestaurantMenuExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
private List<String> _headers;
private Map<String, List<String[]>> _childs;

public RestaurantMenuExpandableListAdapter(Context context, List<String> headers, Map<String, List<String[]>> childs)
{
    this._context = context;
    this._headers = headers;
    this._childs = childs;
}

@Override
public int getGroupCount() {
    Log.i("adapter" , "Header Size : " + this._headers.size());
    return this._headers.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    Log.i("adapter", "Children Count : " + this._childs.get(this._headers.get(groupPosition)).size());
    return this._childs.get(this._headers.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    Log.i("adapter", "Group : " + this._headers.get(groupPosition));
    return this._headers.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    String[] temp = this._childs.get(this._headers.get(groupPosition)).get(childPosition);
    Log.i("adapter", "Child : " + temp[0] + " -- " + temp[1]);
    return this._childs.get(this._headers.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    Log.i("adapter", "Group ID : " + groupPosition);
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    Log.i("adapter", "Child ID : " + childPosition);
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String headerTitle = (String) getGroup(groupPosition);
    if(convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_list_restaurant_menu_header, null);
    }

    TextView item_category = (TextView) convertView.findViewById(R.id.lblListHeader);
    item_category.setTypeface(null, Typeface.BOLD);
    item_category.setText(headerTitle);


    return convertView;
}

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

    final String[] childText =  (String[]) getChild(groupPosition, childPosition);

    if(convertView == null)
    {
        LayoutInflater infalIntlater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalIntlater.inflate(R.layout.expandable_list_restaurant_menu_child_item, null);

        TextView item_text  = (TextView) convertView.findViewById(R.id.textView_item_name);
        TextView item_price = (TextView) convertView.findViewById(R.id.textView_item_price);

        item_text.setText(childText[0]);
        item_price.setText(childText[1]);

    }

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}


}

片段类:

package com.example.pointofsale.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import modle.Item;
import pointofsale.adapters.RestaurantMenuExpandableListAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.example.pointofsale.R;

import db.MyDataSource;

public class HomeScreen_Restaurant_Menu extends Fragment {

RestaurantMenuExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String[]>> listDataChild;
MyDataSource database;


public HomeScreen_Restaurant_Menu(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_restaurant_menu, container, false);

    database = new MyDataSource(getActivity());
    database.open();
    prepareListData();

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    listAdapter = new RestaurantMenuExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
    expListView = (ExpandableListView) getActivity().findViewById(R.id.expandableListView_restaurant_menu);
    expListView.setAdapter(listAdapter);

}


@Override
public void onDestroy() {
    super.onDestroy();
    database.close();
}

private void prepareListData()
{
    listDataChild = new HashMap<String, List<String[]>>();  
    listDataHeader = database.getCategories();


    if(listDataHeader != null)
    {
        for(String header : listDataHeader)
        {
             ArrayList<Item> items = database.getItemsbyCategories(header);

             List<String[]> item_name = new ArrayList<String[]>();

             for(Item i : items)
             {
                 String[] res_item_price = new String[2];

                 res_item_price[0] = i.getName();
                 res_item_price[1] = "Rs. " + i.getSalrate();

                 item_name.add(res_item_price);
                // Log.i("POS", header + ": " + res_item_price[0] + " -- " + res_item_price[1]);

             }
            listDataChild.put(header, item_name);
        }
    }

}


}

1 个答案:

答案 0 :(得分:0)

返回true以获取稳定的ID。

@Override
public boolean hasStableIds() {
    return true;
}