Dark.Rider
Dark.Rider

Reputation: 391

Custom ArrayAdapter for a Spinner: drop down view not working properly

I created a custom ArrayAdapter for a Spinner. The difference is, that it shows images from an ArrayList of a complex class instead of plain text. It works so far. The Images and the radio buttons are displayed as desired. The problem is, that the drop down view doesn't behave correctly: it doesn't close on a click and only the radio buttons are clickable instead of the whole view.

Does anybody has an idea what's wrong? Do I have to implement some kind of listener in the adapter??

Here's the code of the getDropDownView method:

@Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {

        LayoutInflater inflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view=(LinearLayout)inflater.inflate(R.layout.spinnerimageitem, null);

        ImageView iv=(ImageView)view.getChildAt(0);
        RadioButton rb=(RadioButton)view.getChildAt(1);

        int iImageID=ctx.getResources().getIdentifier(
                "f_"+funcs.get(position).getBitmapSetup(), 
                "drawable", ctx.getPackageName());  
        if(loco.getFunction(iIndex).equals(funcs.get(position)))
            rb.setChecked(true);
        iv.setImageResource(iImageID);
        return(view);
    }

Upvotes: 1

Views: 7316

Answers (3)

lub0v
lub0v

Reputation: 949

I had the same issue. For those who will get this problem later, I found one of solution.

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner spinner = (Spinner)findViewById(R.id.spinner);
    ArrayList<String> items = new ArrayList<String>();
    for (int i=1; i<6; i++) items.add("Spinner item "+i);
    spinner.setAdapter(new SpinnerAdapter(this,R.layout.spinner_item_list,items));
}

public class SpinnerAdapter extends ArrayAdapter<String> {
    private ArrayList<Boolean> mChecked;
    private ArrayList<String> mValues;
    private Context mContext;
    public SpinnerAdapter(Context context, int resourceId, ArrayList<String> values) {
        super(context, resourceId, values);
        mValues = values;
        mContext = context;
        mChecked = new ArrayList<Boolean>();
        for (int i=0; i<mValues.size(); i++){
            mChecked.add(false);
        }
    }
    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
            View row= View.inflate(mContext,R.layout.spinner_item_list, null);
            TextView label=(TextView)row.findViewById(R.id.textView);
            label.setText(mValues.get(position));
            RadioButton rb = (RadioButton)row.findViewById(R.id.radioButton);
            rb.setFocusable(false);
            rb.setClickable(false);
            rb.setChecked(mChecked.get(position));
            return row;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = View.inflate(mContext,R.layout.spinner_item_top, null);
        TextView label=(TextView)row.findViewById(R.id.textView);
        label.setText(mValues.get(position));
        for(int i=0; i<mChecked.size(); i++){
            mChecked.set(i,(i==position));
        }
        return row;
    }
}
}

spinner_item_list.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"                     xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView" android:layout_centerVertical="true"/>
<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton" android:layout_alignParentRight="true"     android:checked="false"/>
 </RelativeLayout>

spinner_item_top.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/textView"></TextView>

Upvotes: 1

Shoshi
Shoshi

Reputation: 2254

have you tried like this:

@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    View view = convertView;
    if(view == null){
        LayoutInflater inflater=(LayoutInflater)
                   tx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=(LinearLayout)inflater.inflate(R.layout.spinnerimageitem, null);
    }
     /// your code ....
     return view;
 }

Upvotes: 0

Pragnani
Pragnani

Reputation: 20155

set android:focusable="false" in your layout for Radio button.

Upvotes: 7

Related Questions