2014-02-14 72 views
0

海蘭所有,動態添加視圖兒童ExpandableListView

我triyng從一個ExpandableListView添加到ChildView的LinearLayout中一些動態生成的觀點。該代碼是:

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    if (convertView == null) 
     convertView = inflater.inflate(R.layout.child_fragment_item_menu_child_view, null); 

    TextView childName        = (TextView) convertView.findViewById(R.id.childName); 
    TextView childDetails       = (TextView) convertView.findViewById(R.id.childDetails); 
    LinearLayout childPriceWeightHolder_Single  = (LinearLayout) convertView.findViewById(R.id.childPriceWeightHolder_Single); 
    LinearLayout childPriceWeightHolder_Multiple = (LinearLayout) convertView.findViewById(R.id.childPriceWeightHolder_Multiple); 

    JSONObject menuItem = null; 
    try { 
     menuItem = new JSONObject(getChild(groupPosition, childPosition)); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if(menuItem != null){ 
     childName.setText(menuItem.optString("name")); 
     childDetails.setText(menuItem.optString("description")); 

     final TextView tv1 = new TextView(context), tv2 = new TextView(context); 

     if(menuItem.optJSONArray("advance_prices") == null || menuItem.optJSONArray("advance_prices").length() == 0){ 
      if(menuItem.optString("price") != null && menuItem.optString("price").compareToIgnoreCase("null") != 0){ 
       tv1.setText(menuItem.optString("price") + " " + menuItem.optString("currency_name")); 
       tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 
      } 

      if(menuItem.optString("weight") != null && menuItem.optString("weight").compareToIgnoreCase("null") != 0){ 
       tv2.setText(menuItem.optString("weight")); 
       tv2.setTextColor(context.getResources().getColor(R.color.gray)); 
       tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 
      } 

      childPriceWeightHolder_Single.addView(tv1); 
      childPriceWeightHolder_Single.addView(tv2); 
     }else{ 
      // COMPOSED VIEW(multiple prices and multiple weights, etc...) 
     } 
    } 

    return convertView; 
} 

的問題是,第一頁之後,什麼適配器生成新的孩子的它保持一個參考或者東西這些dinamycally添加textViews和它攪亂了一切。我試圖添加tv1.setId(..);但它不起作用。

任何解決方案?謝謝!

+0

完成它!只需添加childPriceWeightHolder_Single.removeAllViews();到你想膨脹的觀點(以及之前誇大的觀點),所以它只會增加當前的基本觀點! – jorjSB

回答

0

爲expandablelistview基本的例子是..這裏....

在活動實施OnChildClickListener和implemet此方法..

@Override 
public boolean onChildClick(ExpandableListView parent, View v, 
int groupPosition, int childPosition, long id) { 

    //Add new chid data in your list (ex ArrayLis or Array whatever you use) 
    adapter.notifyDataSetChanged() // BaseExpandableListAdapter's adapter... 


return true; 
} 

是它的工作對你的工作?