为什么OnItemSelected不能使用array adapter
?
spinner
的每一行都有list view
。
这是我的代码:
package info.androidhive.materialtabs.activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import info.androidhive.materialtabs.R;
/**
* Created by marco on 24/07/16.
*/
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {
Spinner spinner;
/*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;
System.out.println("ciao");
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);
spinner.setOnItemSelectedListener(this);
/*
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
*/
/* @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Spinner spinner = null;
// String selState = (String) ((Spinner) parent).getSelectedItem();
spinner.setSelection(position);
String selState = (String) ( parent).getSelectedItem();
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});*/
return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setSelection(position);
String selState = (String) ( parent).getSelectedItem();
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/* @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) {
}*/
}
FRAGMENT:
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
答案 0 :(得分:0)
只需在OnItemSelected方法中尝试这个....
更改
txtThird = (TextView) convertView.findViewById(R.id.age);
到
String selState = (String) ( parent).getSelectedItem();
答案 1 :(得分:0)
尝试在getView方法中使用它,而不是onItemSelected方法。你的onItemSelected方法适用于listview。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selState = spinner.getSelectedItem().toString();
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});