Hy all,
我正在尝试从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 :(得分:0)
expandablelistview的基本示例是.. here ....
在您的Activity中实现OnChildClickListener并实现此方法..
@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;
}
这对你有用吗?