Arif YILMAZ
Arif YILMAZ

Reputation: 5866

add unique images to spinner rows in android

I have managed to code to have images in each row of spinner but the images are all the same.I want to change each image. how do you do that. here is my xml and the code

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <ImageView
 android:id="@+id/icon"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/calendar"/>
 <TextView
 android:id="@+id/weekofday"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
</LinearLayout>

here is java

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.weekofday, list);
dataAdapter.setDropDownViewResource(R.layout.row);
spinner.setAdapter(dataAdapter);

it displays the same image because it is set in the row.xml file. how do I make it dynamic?

Upvotes: 0

Views: 179

Answers (2)

Prachi
Prachi

Reputation: 3672

         public class SavedSearchAdapter extends BaseAdapter {

          private Context mContext;
          private ArrayList<SavedSearch> mData;

          private boolean isEditMode = false;

          public SavedSearchAdapter(Context context , ArrayList<SavedSearch> data){
      mContext = context;
      mData = data;
           }


      @Override
         public int getCount() {
        return mData.size();
       }

         @Override
          public Object getItem(int position) {
        return mData.get(position);
        }

       @Override
          public long getItemId(int position) {
        return position;
      }

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

       View view;
       TextView tvTitle;
       ImageView ivDelete;

        if(convertView == null){
            view = View.inflate(mContext,          R.layout.screen_saved_search_item_row, null);
        }
         else{
        view = convertView;
         }




         tvTitle = (TextView)   view.findViewById(R.id.screen_saved_search_item_row_tv);
         tvTitle.setText(mData.get(position).name);

         ivDelete = (ImageView) view.findViewById(R.id.screen_saved_search_item_row_iv_delete);

         view.setTag(mData.get(position));

          return view;
           }


           }

Upvotes: 0

Brijesh Thakur
Brijesh Thakur

Reputation: 6788

Why don't you create Custom adapter and write the code accordingly. As far as I understand that , its showing same image because you have set it in row.xml. But how would adapter will know that the image should be changed on each row.

In Custom Adapter you can set the image as per the position (i.e. index) of Spinner Item

Upvotes: 1

Related Questions