我把它变成onCreateView()中的片段:
ListView listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter();
listView.setAdapter(listAdapter);
MyListAdapter:
public class MyListAdapter extends ArrayAdapter {
/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/
/* public MyListAdapter(Context context, int resource, List<ClipData.Item> items) {
super(context, resource, items);
}*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Spinner 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);
return row;
}
}
ARRAY ADAPTER:
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener{
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);
return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Spinner spinner = null;
System.out.println("ciao");
listView.setSelection(position);
String selState = (String) listView.getSelectedItem();
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
现在我的问题是:我必须写入课程constructor
MyListAdapter
的内容?
感谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!
答案 0 :(得分:0)
ArrayAdapter类中有6个构造函数。您必须根据您的要求实施其中任何一个。
有关详细信息,请参阅此link
答案 1 :(得分:0)
每个扩展类必须至少有一个与父类匹配的构造函数。所以你的类必须有一行构造函数并调用super,不需要自己的代码。
public MyListAdapter(Context context, int resource ) {
super(context, resource);
//your code
}
但是当你创建你的适配器时,你正在使用构造函数,所以你可以创建任何构造函数,重要的是在其中使用super。
在构造函数中您可以实现代码来设置数据以从数据库列出。
您可以创建类似的构造函数:
public MyListAdapter(Context context) {
super(context, R.layout.row_view);
//your code
}
和
ListView listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);