控制单击可展开列表视图的结果



我需要为我的应用程序实现可扩展的列表视图。这个列表将用于为披萨添加一些配料,所以我需要在检查配料时增加奖品,在不检查配料时减少奖品。

我找到了自定义可扩展列表的代码,并根据我的需求进行了修改。

这是适配器的代码:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<customPizzaSendInfo>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<customPizzaSendInfo>> 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 customPizzaSendInfo childText = (customPizzaSendInfo) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expander_list_item, null);
}
CheckBox txtListChild = (CheckBox) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText.nameString);
txtListChild.setChecked(childText.choosen);
txtListChild.setOnCheckedChangeListener(new OnCheckedChangeListener() {             

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true)
{
prizeSmall += childText.priceSmallDouble;
prizeBig += childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
}else
{
prizeSmall -= childText.priceSmallDouble;
prizeBig -= childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
}
}
});
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(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.expander_list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
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;
}

}

这是包含数据customPizzaSendInfo的类的代码

public class customPizzaSendInfo {
public String nameString;
public boolean choosen;
public Double priceSmallDouble;
public Double priceBigDouble;
public customPizzaSendInfo(String nameString, boolean choosen, Double priceSmallDouble, Double priceBigDouble) {
super();
this.nameString = nameString;
this.choosen = choosen;
this.priceSmallDouble =  priceSmallDouble;
this.priceBigDouble = priceBigDouble;
}

}

有两个问题:

  • 如何更新_listDataChild,因为当我关闭扩展器然后打开它时,我得到的结果就像它被放入一样
  • 当我搜索列表时,我的价格会上涨,所以android会将我的触摸解释为滚动,就像我在检查对象一样,但复选框不会改变状态

如果我试图在任何点击按钮上监控孩子的状态,我没有得到任何反应,所以我决定在AdapterClass中制作。

如何解决

UPDATE我试图更新这个_listDataChild,它保存关于对象的数据,所以我修改了checkChanged

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true)
{
//
//Toast.makeText(getApplicationContext(), Integer.toString(childPosition) + " " + childText.nameString, Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).nameString, Toast.LENGTH_LONG).show();
_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = true;
prizeSmall += childText.priceSmallDouble;
prizeBig += childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
}else
{
_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = false;
prizeSmall -= childText.priceSmallDouble;
prizeBig -= childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
}

}
});

当我滚动对象时,该对象将变为未选中。。。

解决方案我是如何通过在ExpandableListAdapter中添加onclick监听器来解决这个问题的,我使用@Abd El Rahman El Tamawy的建议来更新_listDataChild,它就像一个魅力,下面是代码。

txtListChild.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), Integer.toString(childPosition), Toast.LENGTH_LONG).show();
if(_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen)
{
_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = false;
}
else
{
_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = true;
}
}
});

您应该将按钮的状态保存在HashMaponClickListener中,以使adapter能够在collapseexpand再次成为父级时从HashMap重新绘制最终状态,如下所示

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true)
{
childText.
prizeSmall += childText.priceSmallDouble;
prizeBig += childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
}else
{
prizeSmall -= childText.priceSmallDouble;
prizeBig -= childText.priceBigDouble;
normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
duzaButton.setText("Duża " + String.valueOf(prizeBig));
Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
}
//Get the HashMap Item of the current position and set all its value with the new values
this._listDataChild.get("yourCurrentKey").get(position).choosen = !this._listDataChild.get("yourCurrentKey").get(position)..choosen;
//Complete saving all your data

最新更新