Question

I have created a custom Spinner in my Android application.

When i click on my custom spinner my application gets stop Unfortunately. Here i had put my error Log and Custom Adapter Code. Here Custom_Spinner_info is my class where i stored my Custom_Spinner Row data.

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; } 

enter image description here

More details.

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); 

This is my declaration of spinner in 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" />

link to full code

http://pastebin.com/3PP0dFv7

Was it helpful?

Solution

You just need to add this line before using ,

ArrayList<CustomSpinnerRow_Info> spinnerInfo= new ArrayList<CustomSpinnerRow_Info>();

OTHER TIPS

try to do something like the following : \

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.customrow, null, false);

and please give me some feedback .

Hope that helps .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top