我在Android应用程序中创建了一个自定义Spinner。
当我点击我的自定义微调器时,我的应用程序停止了。 在这里,我把我的错误日志和自定义适配器代码。 这里 Custom_Spinner_info 是我的类,我存储了Custom_Spinner Row数据。
public class MyCustomAdapter extends ArrayAdapter<Custom_Spinner_info> {
private Activity context;
ArrayList<Custom_Spinner_info> data = null;
Custom_Spinner_info csi;
public MyCustomAdapter(Activity context, int resource,ArrayList<Custom_Spinner_info> data) {
super(context, resource,data);
this.context=context;
this.data=data;
}
@Override
public View getView(int pos,View convertView,ViewGroup vg)
{
return super.getView(pos, convertView, vg);
}
@Override
public View getDropDownView(int pos,View convertView,ViewGroup vg)
{
View view=convertView;
if(view==null){
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.customrow, vg, false);
}
csi= data.get(pos);
if(csi!=null)
{
TextView tv=(TextView)view.findViewById(R.id.textView1);
tv.setText(csi.getName());
}
return view;
}
public ArrayList<Custom_Spinner_info> populateList() { ArrayList<Custom_Spinner_info> mySpinner = new ArrayList<Custom_Spinner_info>(); mySpinner.add(new Custom_Spinner_info("USA", 308745538)); // Image stored in /drawable mySpinner.add(new Custom_Spinner_info("Sweden", 9482855)); mySpinner.add(new Custom_Spinner_info("Canada", 34018000)); return mySpinner; }
更多细节。
ArrayList<Custom_Spinner_info> mySpinner=populateList();
public ArrayList<Custom_Spinner_info> populateList() {
ArrayList<Custom_Spinner_info> mySpinner = new ArrayList<Custom_Spinner_info>();
mySpinner.add(new Custom_Spinner_info("USA", 308745538)); // Image stored in /drawable
mySpinner.add(new Custom_Spinner_info("Sweden", 9482855));
mySpinner.add(new Custom_Spinner_info("Canada", 34018000));
return mySpinner;
}
MyCustomAdapter adapter= new MyCustomAdapter(this, android.R.layout.simple_spinner_dropdown_item, mySpinner);
spi.setAdapter(adapter);
这是我在xml
中声明的微调器<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="52dp" />
链接到完整代码
答案 0 :(得分:0)
尝试执行以下操作:\
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.customrow, null, false);
请给我一些反馈。
希望有所帮助。
答案 1 :(得分:0)
您只需在使用前添加此行,
ArrayList<CustomSpinnerRow_Info> spinnerInfo= new ArrayList<CustomSpinnerRow_Info>();