我可以在expandablelistview中实现checkedtextview。但是当我检查某个子项时,其他父项的一些子项也被选中。我无法弄清楚为什么我会遇到这样的问题?任何人都面临类似的情况?提前感谢您的帮助。
编辑:
正如我所建议的那样,我在customadapter中创建了布尔多维数组:`
Boolean[][] categoryx = new Boolean` [5][5];
自定义适配器中有getchildView方法:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
System.out.print(groupPosition);
System.out.println(childPosition);
final String children = (String) getChild(groupPosition, childPosition);
CheckedTextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((CheckedTextView )v).toggle();
}
});
return convertView;
}
但我很困惑,我应该使用访问的子位置来切换我的值。
编辑2:我正在尝试保存状态的自定义适配器:
import android.app.Activity;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
public class MyCategoryAdapter extends BaseExpandableListAdapter {
private SparseArray<Groupx> groups;
public LayoutInflater inflater;
public Activity activity;
Boolean[][] categoryx = new Boolean [5][5];
public MyCategoryAdapter(Activity act, SparseArray<Groupx> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
System.out.print(groupPosition);
System.out.println(childPosition);
final String children = (String) getChild(groupPosition, childPosition);
CheckedTextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Integer lol = (Integer) v.getTag();
((CheckedTextView )v).toggle();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Groupx group = (Groupx) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.stringx.toString());
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
答案 0 :(得分:2)
由于对ListView行回收的误解,这是一个经常出现的问题。
问题是,您只在视图中保留checked属性。现在,ListView
重新使用从屏幕上消失以保存内存的单元格,因此,一旦检查了一行,一旦原始的一行被回收,另一个也将被检查。
为避免这种情况,您必须将已检查状态保存在ListView
数据源中,并在ListView
适配器中设置相应的选中值。
答案 1 :(得分:0)
在xml中设置singlechoicemode:true。
答案 2 :(得分:0)
使用以下覆盖BaseExpandableListAdapter
中的方法public int getChildType(int groupPosition, int childPosition) {
//define a condition here to differentiate between your child views.
}
public int getChildTypeCount() {
return 2; <-- this is the number of views for your child
}
希望这会有所帮助。