web-dev-qa-db-fra.com

Comment créer Spinner-list en utilisant Custom Adapter dans android

dans mon application, je veux créer SpinnerList à l'aide de la classe CustomAdapter. Pour cela, j'ai écrit le code ci-dessous, mais lorsque j'appuie sur spinner list, les images array-List ne se chargent pas dans spinner-list, comme dans l'écran ci-dessous, pourquoi ce problème se produit-il?

main_layout.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="wrap_content">

    <TextView
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:layout_marginTop="10dip"
        Android:text="Category:"
        Android:layout_marginBottom="5dp"/>

    <Spinner
        Android:id="@+id/spinner"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"/>

</LinearLayout>

Activité principale:-

public class MainActivity extends AppCompatActivity  {

    public static final String[] titles = new String[] { "Strawberry",
            "Banana", "Orange", "Hello" };

    public static final Integer[] images = { R.drawable.image1,
            R.drawable.image2, R.drawable.image3, R.drawable.image4 };

    Spinner spinner;
    List<RowItem> rowItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++) {

            RowItem item = new RowItem(titles[i],images[i]);
            rowItems.add(item);
        }

        spinner = (Spinner)findViewById(R.id.spinner);
       CustomAdapter adapter = new CustomAdapter(MainActivity.this,
            R.layout.listitems_layout, R.id.title, rowItems);
        spinner.setAdapter(adapter);
    }
}

listitems_layout.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="horizontal">

    <ImageView
        Android:id="@+id/icon"
        Android:layout_width="80dp"
        Android:layout_height="80dp"
        Android:src="@drawable/image1"
        Android:padding="10dp"
        />

    <TextView
        Android:layout_marginTop="30dp"
        Android:id="@+id/title"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_toRightOf="@+id/icon"
        Android:textColor="#CC0033"
        Android:text="Strawberry"
        Android:textSize="16dp" />

</LinearLayout>

CustomAdapter: -

package com.example.venkat.spinnerexample1;


import Android.app.Activity;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.ArrayAdapter;
import Android.view.LayoutInflater;
import Android.widget.ImageView;
import Android.widget.TextView;

import Java.util.List;

public class CustomAdapter extends ArrayAdapter<RowItem> {

    LayoutInflater flater;

     public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){

    super(context,resouceId,textviewId, list);
    flater = context.getLayoutInflater();
}

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

        RowItem rowItem = getItem(position);

        View rowview = flater.inflate(R.layout.listitems_layout,null,true);

        TextView txtTitle = (TextView) rowview.findViewById(R.id.title);
        txtTitle.setText(rowItem.getTitle());

        ImageView imageView = (ImageView) rowview.findViewById(R.id.icon);
        imageView.setImageResource(rowItem.getImageId());

        return rowview;
    }
}

RowItem: -

package com.example.venkat.spinnerexample1;

public class RowItem {

    private int ImageId;
    private String Title;

    public RowItem(String Title,int ImageId){

        this.Title = Title;
        this.ImageId = ImageId;
    }

    public String getTitle(){

        return Title;
    }

    public void setTitle(String Title){

        this.Title = Title;
    }

    public int getImageId(){

        return ImageId;
    }

    public void setImageId(int ImageId){

        this.ImageId = ImageId;
    }

    @Override
    public String toString() {
        return Title ;
    }
}

écran:- --- enter image description here

23
AbhiRam

Dans le constructeur de votre adaptateur, envoyez également l'id de textview

public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){

        super(context,resouceId,textviewId, list);
        flater = context.getLayoutInflater();
    }

appelle par

CustomAdapter adapter = new CustomAdapter(MainActivity.this,
                R.layout.listitems_layout, R.id.title, rowItems);

EDIT Vos images ne s'affichent pas car vous n'avez pas remplacé la méthode getDropdownView (). Cette méthode décide de la disposition de l'enfant lorsque la liste déroulante est visible. Alors ajoutez cette méthode à votre adaptateur

@Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = flater.inflate(R.layout.list_itemslayout,parent, false);
            }
            RowItem rowItem = getItem(position);
            TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
            txtTitle.setText(rowItem.getTitle());
            ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
            imageView.setImageResource(rowItem.getImageId());
            return convertView;
        }

Suggestion dans votre getView (), cochez (convertview == null). Cela n’aura peut-être aucun impact sur ce petit adaptateur, mais au cas où votre adaptateur contiendrait plus d’éléments, les performances en seraient affectées.

EDIT Pour placer votre liste déroulante de spinner sous l’ancre, utilisez

Android:overlapAnchor="false" à l'intérieur de votre spinner

21
Ankit Aggarwal
Change your adapter like this

public class CustomAdapter extends ArrayAdapter<RowItem> {

    LayoutInflater flater;

    public CustomAdapter(Activity context, int resouceId, int textviewId, List<RowItem> list){

        super(context,resouceId,textviewId, list);
//        flater = context.getLayoutInflater();
    }

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

        return rowview(convertView,position);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return rowview(convertView,position);
    }

    private View rowview(View convertView , int position){

        RowItem rowItem = getItem(position);

        viewHolder holder ;
        View rowview = convertView;
        if (rowview==null) {

            holder = new viewHolder();
            flater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowview = flater.inflate(R.layout.listitems_layout, null, false);

            holder.txtTitle = (TextView) rowview.findViewById(R.id.title);
            holder.imageView = (ImageView) rowview.findViewById(R.id.icon);
            rowview.setTag(holder);
        }else{
            holder = (viewHolder) rowview.getTag();
        }
        holder.imageView.setImageResource(rowItem.getImageId());
        holder.txtTitle.setText(rowItem.getTitle());

        return rowview;
    }

    private class viewHolder{
        TextView txtTitle;
        ImageView imageView;
    }
}
26
shubham goyal