我实现了一个自定义列表视图,其中包含自定义列表项,其中包含微调框和文本框。我想在列表视图的列表项中使用onItemSelected()
保存微调器的选定值,但我不知道找到一个包含用户正在处理的微调器的列表项。我怎么能得到这个,有人可以帮忙吗?
以下是列表视图适配器中的代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = inflater.inflate(R.layout.listitem, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.view1 = (Spinner) listItemView.findViewById(R.id.view1);
viewHolder.view2 = (TextView) listItemView.findViewById(R.id.view2);
viewHolder.view1.setFocusable(false);
viewHolder.view2.setFocusable(false);
viewHolder.view1.setAdapter(view1Adapter);
viewHolder.view1.setPrompt("Select");
viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedId = spinnerItemList.get(position).id;
// todo : need to find a way to get list item of list view, which has this spinner, like the position of getView().
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
listItemView.setTag(viewHolder);
}
CustomListItem item = listViewItemList.get(position);
ViewHolder viewHolder = listItemView.getTag();
viewHolder.view1.setSelection(item.view1Value);
viewHolder.view2.setText(String.valueOf(item.view2Value));
return listItemView;
}
答案 0 :(得分:0)
如果要处理适配器内微调器的onItemSelected,请尝试:
//将activity / fragment的上下文传递给适配器
context.saveSelectedValue(selectedId);
//将所选内容保存在activity / fragment
中public void saveSelectedValue(String id){
//code to save
}
答案 1 :(得分:0)
我发布了我已经找到的答案给那些与我有同样问题的人。实际上我从Loc的回答中看到了这个想法,但他以某种方式删除了他的回答。也许这个解决方案可能存在严重问题。无论如何,这对我来说很有用。
我使用setTag()和getTag()来获取CustomListItem。请参阅以下代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = inflater.inflate(R.layout.listitem, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.view1 = (Spinner) listItemView.findViewById(R.id.view1);
viewHolder.view2 = (TextView) listItemView.findViewById(R.id.view2);
viewHolder.view1.setFocusable(false);
viewHolder.view2.setFocusable(false);
viewHolder.view1.setAdapter(view1Adapter);
viewHolder.view1.setPrompt("Select");
viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedId = spinnerItemList.get(position).id;
// todo : need to find a way to get list item of list view, which has this spinner, like the position of getView().
CustomListItem item = (CustomListItem)parent.getTag();
item.view1Value = selectedId;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
listItemView.setTag(viewHolder);
}
CustomListItem item = listViewItemList.get(position);
ViewHolder viewHolder = listItemView.getTag();
viewHolder.view1.setSelection(item.view1Value);
viewHolder.view.setTag(item);
viewHolder.view2.setText(String.valueOf(item.view2Value));
return listItemView;
}