方法onItemSelected
不起作用,我不知道原因。
以下是我的代码。
数组适配器:
public class MyListAdapter extends ArrayAdapter {
Spinner spinner;
ListView listView;
/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
System.out.println("ciao1");
// spinner.setOnItemSelectedListener(this);
return row;
}
/* @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) spinner.getSelectedItem();
System.out.println(selState);
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
}
片段:
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
// listView.setOnItemClickListener(this);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
listView.setSelection(position);
String selState = (String) listView.getSelectedItem();
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
在ListViews上使用onItemClicked。 OnItemSelected适用于Spinners。这是Android麻烦之一...
答案 1 :(得分:-1)
在OnItemSelectedListener(this)
中设置ArrayAdapter
的方法中,我认为问题在于,因为列表中的每个微调器都引用了MyListAdapter
的同一个实例,所以OnItemSelected
} method无法区分列表中的哪个微调器实际被选中 - 它只引用最后设置为MyListAdapter.spinner
引用的微调器对象,该引用应该是列表中的最后一个。
要解决此问题,请不要使用spinner.getSelectedItem()。而是使用((Spinner) parent).getSelectedItem()
,因为父对象将是从中选择的实际微调器。此时,您应该将spinner变量设置为getView方法的本地变量。
在片段中调用listView.setOnItemSelectedListener(...)
的方法中,我希望你的意思是listView.setOnItemClickListener(...)
,但这会听取列表中某一行的点击,而不是来自微调器的选择。
要检测其中一个微调器的选择,请进行上面的第一次修改。要检测listView中某一行的点击,请更改为listView.setOnItemClickListener
。如果没有更多的目标背景或实际发生的错误,很难判断是否需要这两者或只是第一个。
编辑: 根据您的评论,我认为您应该在MyListAdapter中使用它:
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Spinner spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
parent.getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) ((Spinner) parent).getSelectedItem();
System.out.println(selState);
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
这是你的片段(没有听众):
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);