2015-09-18 59 views
0

我遇到了一些實現expandableListView的問題。我已經實現我的ExpandableListAdapterexpandableListView子代之間的干擾

公共類ExpandableListAdapter擴展BaseExpandableListAdapter {

private static final int SUM = 1; 
private static final int SUB = 2; 

private Context context; 
private List<String> listDataHeader; //Header titles 
private HashMap<String, List<Article>> listDataChild; 
private HashMap<Integer, Bitmap> productImagesHashMap; 

public ExpandableListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<Article>> listDataChild, HashMap<Integer, Bitmap> productImagesHashMap) { 
    this.context = context; 
    this.listDataHeader = listDataHeader; 
    this.listDataChild = listDataChild; 
    this.productImagesHashMap = productImagesHashMap; 
} 

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

@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 Object getChild(int groupPosition, int childPosition) { 
    return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition); 
} 

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

@Override 
public long getChildId(int groupPosition, int 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 layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.user_expand_listview_header, null); 
    } 

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

    return convertView; 
} 

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

    final Article childValues = (Article) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.product_row, null); 
    } 

    Integer articleId = childValues.getId(); 

    final TextView articleIdTextView = (TextView) convertView.findViewById(R.id.product_id); 
    ImageView productImageTextView = (ImageView) convertView.findViewById(R.id.product_image); 
    TextView productNameTextView = (TextView) convertView.findViewById(R.id.product_name); 
    TextView productDescriptionTextView = (TextView) convertView.findViewById(R.id.product_description); 
    final TextView productPriceTextView = (TextView) convertView.findViewById(R.id.product_price); 

    articleIdTextView.setText(articleId.toString()); 
    productImageTextView.setImageBitmap(productImagesHashMap.get(articleId)); 
    productNameTextView.setText(childValues.getNome()); 
    productDescriptionTextView.setText(childValues.getDescrizione()); 
    productPriceTextView.setText(childValues.getPrezzo()); 

    ImageButton addButton = (ImageButton) convertView.findViewById(R.id.add_btn); 
    final TextView articleQty =(TextView) convertView.findViewById(R.id.product_qnty); 
    ImageButton removeButton = (ImageButton) convertView.findViewById(R.id.remove_btn); 

    addButton.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      if (context instanceof UserActivity) { 
       ((UserActivity) context).updateTotal(
         Integer.parseInt(articleIdTextView.getText().toString()), 
         productPriceTextView.getText().toString(), 
         SUM); 
       articleQty.setText(updateProductQty(articleQty, SUM)); 
      } 
     } 
    }); 

    removeButton.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      if (context instanceof UserActivity) { 
       ((UserActivity) context).updateTotal(
         Integer.parseInt(articleIdTextView.getText().toString()), 
         productPriceTextView.getText().toString(), 
         SUB); 
       articleQty.setText(updateProductQty(articleQty, SUB)); 
      } 
     } 
    }); 

    return convertView; 
} 

@Override 
public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 

private String updateProductQty(TextView productQty, int operation) { 
    Integer qty = Integer.parseInt(productQty.getText().toString()); 
    switch (operation) { 
     case 1: //SUM 
      qty += 1; 
      break; 
     case 2: //SUB 
      if (qty > 0) 
       qty -= 1; 
      break; 
    } 
    return qty.toString(); 
} 

}

但是也有一些從數據庫中按類型(其實鋼筆,筆記本電腦和閱讀並插入ExpandableListView一些產品紙)。然後有兩個按鈕可以選擇用戶想要購買的數量,數量由TextView跟蹤。現在,當我用普通的ListView實現了這一切時,所有的工作,但與ExpandableListView有數量的TextView的兒童之間的干擾。

例如,ExpandableListView的實際組成爲:

- 黑色筆

- 藍色筆

- 紅色筆

筆記本

- 黑色筆記本

- 藍筆記本

- 紅色筆記本

- A4紙

如果用戶添加一個黑色的筆記本,藍筆也被添加

如果用戶添加藍色筆記本,還會添加黑色筆

如果用戶添加紅色筆記本,也添加藍色筆

爲什麼會有這些干擾?

+0

你試過調試嗎? –

回答

0

您沒有正確跟蹤您的數量更新。

由於ExpandableListView - 像ListView - 循環利用的意見,你這種行爲:

  • 用戶按下按鈕添加黑色筆記本。
  • 用戶滾動/展開/摺疊列表。
  • 查看藍色筆是從黑色筆記本回收視圖創建的。
  • 查看藍色筆顯示數量從黑色筆記本。

這就是你需要做什麼:

  • 數量屬性添加到Article
  • 在創建子視圖,填充articleQtyTextView與包含在Article對象的數量。
  • 當用戶按下添加/刪除按鈕時,更新Article對象中的數量並致電notifyDataSetChanged()

規則是:視圖從模型填充,模型從監聽器更新。不要使用偵聽器來更新視圖,調用notifyDataSetcChanged()會照顧到這一點。

+0

Thaks,它的工作原理!這正是我想知道的:D – Fededark