爲什麼OnItemSelected不起作用到array adapter
?發佈onItemSelected
我在list view
的每一行有spinner
。 這是我的代碼:
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) {
}*/
}
片斷:
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
希望該列表是空的。越來越顯示? – sJy
列表視圖中的每一行中的列表視圖我看到飛旋,我可以選擇,但是當我選擇了一個對象,吐司沒有出現...所以方法不起作用 –
它是適配器的完整代碼嗎?它缺少一些重要的覆蓋方法,如getCount()。 – sJy