2014-03-27 49 views
0

我的問題是,當我第一次點擊任何組項時,它會展開並正常工作。但是,當我點擊其他組顯示錯誤的子項。通過錯誤的我的意思是孩子項目洗牌並互相取代。爲什麼在展開和摺疊任何組後展開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); 
     } 
    } 

} 


} 

回答

0

返回true穩定的ID。

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

仍然無法正常工作。 –