2015-11-02 40 views
0

我想在android中實現可擴展列表視圖。子元素包含兩個圖像視圖等等。通過點擊這些我更新每個子元素上顯示的數字。 問題我面對的是,點擊某個特定的孩子也更新其他隨機孩子的價值在同一以及不同的父母。Android:ExpandableListView子項點擊在多個子項上工作

我的適配器:

public class MenuAdapter extends BaseExpandableListAdapter { 
    private Context _context; 
    private ArrayList<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, ArrayList<FoodItem>> _listDataChild; 

    public MenuAdapter(Context context, ArrayList<String> listDataHeader, 
         HashMap<String, ArrayList<FoodItem>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

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

     final FoodItem child = (FoodItem) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.content_menu, null); 
     } 

     TextView nameTextView = (TextView) convertView 
       .findViewById(R.id.nameTextView); 
     TextView priceTextView = (TextView) convertView 
       .findViewById(R.id.priceTextView); 
     final TextView quantityTextView = (TextView) convertView 
       .findViewById(R.id.quantityTextView); 
     ImageView decreaseImageView = (ImageView) convertView 
       .findViewById(R.id.decreaseImageView); 
     ImageView increaseImageView = (ImageView) convertView 
       .findViewById(R.id.increaseImageView); 

     nameTextView.setText(child.getName()); 
     priceTextView.setText(child.getPrice()); 

     decreaseImageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (child.getCount() > 0) { 
        quantityTextView.setText(Integer.toString(child.getCount() - 1)); 
        child.setCount(child.getCount() - 1); 
       } 
      } 
     }); 
     increaseImageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       quantityTextView.setText(Integer.toString(child.getCount() + 1)); 
       child.setCount(child.getCount() + 1); 
      } 
     }); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(final int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     final String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.header_menu, null); 
     } 

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

     return convertView; 
    } 

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

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

回答

0

你忘了設置quantityTextView 初始值添加此您getChildView()

quantityTextView.setText(Integer.toString(child.getCount())); 

編輯: 爲什麼它似乎是隨機發生的原因是,意見正在得到重用,這意味着convertView不是null。它將顯示在該convertView中顯示的任何內容(如果不重置/設置初始值,可能是任何視圖從屏幕上以不特定的順序滾動)。

P.S.你應該看看持有人的模式link

+0

工作就像一個魅力。謝謝一堆! – user3883991