我是Android新手,已经谷歌搜索了几个星期来解决这个问题。我在主体中有一个微调器,在扩展列表的头部有一个文本字段,它位于片段的滚动视图中。标题中的文本正确显示,但旋转器值会随着显示/隐藏子视图而更改。
我不知道自己做错了什么。我很感激这里的任何帮助。
这是适配器:
public class ExtendedListAdapter extends BaseExpandableListAdapter {
private Context ctx;
private LinkedHashMap<String, List<List<LinkedHashMap<String, String>>>> extendedListData;
private List<String> extendedListHeaders;
class Viewholder {
View rowView;
Spinner spnr;
TextView item1;
}
public ExtendedListAdapter(Context ctx, LinkedHashMap<String, List<List<LinkedHashMap<String, String>>>> extendedListData, List<String> extendedListHeaders) {
this.ctx = ctx;
this.extendedListData = extendedListData;
this.extendedListHeaders = extendedListHeaders;
}
@Override
public Object getChild(int parent, int child) {
return extendedListData.get(extendedListHeaders.get(parent)).get(child);
}
@Override
public long getChildId(int parent, int child) {
// TODO Auto-generated method stub
return child;
}
@Override
public View getChildView(int parentId, int childId, boolean lastChild, View convertView,
ViewGroup parentview) {
Viewholder v;
if (convertView == null) {
v = new Viewholder();
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.child_layout, parentview, false);
v.rowView = convertView;
v.rowView.setTag(v);
v.item1 = (TextView) convertView.findViewById(R.id.textLabel);
v.spnr = (Spinner) convertView.findViewById(R.id.spinner1);
}
else {
v = (Viewholder) convertView.getTag();
}
List<LinkedHashMap<String, String>> childListLinkedHashMap = (List<LinkedHashMap<String, String>>) getChild(parentId, childId);
LinkedHashMap<String, String> label = childListLinkedHashMap.get(0);
String[] items = new String[childListLinkedHashMap.size() - 1];
v.item1.setText(label.get("name"));
for (int i = 1; i < childListLinkedHashMap.size(); i++) {
items[i - 1] = childListLinkedHashMap.get(i).get("name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_dropdown_item, items);
v.spnr.setAdapter(adapter);
if (v.spnr.getTag() != null) {
v.spnr.setSelection((int) v.spnr.getTag());
}
v.spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
parent.setTag(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return convertView;
}
@Override
public int getChildrenCount(int arg0) {
return extendedListData.get(extendedListHeaders.get(arg0)).size();
}
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return extendedListHeaders.get(arg0);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return extendedListHeaders.size();
}
@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentview) {
// TODO Auto-generated method stub
String group_title = (String) getGroup(parent);
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.parent_layout, parentview, false);
}
TextView parent_textview = (TextView) convertView.findViewById(R.id.parent_txt);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
parent_textview.setTypeface(null, Typeface.BOLD);
parent_textview.setText(group_title);
if (isExpanded) {
imageView.setImageResource(R.drawable.group_down);
} else {
imageView.setImageResource(R.drawable.group_up);
}
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:0)
对于微调器选择的值,您尝试从微调器访问标记,但是您要在ExpandedListView
(parent
),而不是微调器上设置标记。但即使你解决了这个问题,它仍然无法发挥作用。
当视图被回收时,您将从循环视图中获取微调器值,这将导致您的微调器在滚动列表时具有错误的值。
基本上,您试图在视图上存储模型数据(微调器选择),而不是在应该存在的模型中。
为了完成这项工作,你需要一些类似于int列表的列表 - 组的第一个列表和子组的第二个列表。
因此,让我们添加一个属性来存储选择:
private int[][] selected;
你应该可以在这里使用数组,因为你的列表/地图似乎没有变化。
在构造函数中设置:
selected = new int[extendedListHeaders.size()][];
for (int i = 0; i < selected.length; i++) {
int childSize = extendedListData.get(extendedListHeaders.get(i)).size();
selected[i] = new int[childSize];
}
现在getChildView
中的微调器逻辑将如下所示:
v.spnr.setSelection(selected[parentId][childId]);
v.spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
selected[parentId][childId] = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
});
您需要制作parentId
和childId
参数final
才能在事件回调中使用它们。
通过事件回调使用模型数据设置模型数据和视图,微调器应该按照您期望的方式工作。