wake-up-neo.com

Wie erstelle ich eine Spinner-Liste mit Custom Adapter in android

in meiner App möchte ich SpinnerList mit der CustomAdapter-Klasse erstellen. Dazu habe ich den folgenden Code geschrieben, aber wenn ich auf die Spinner-Liste tippe, werden die Array-List-Bilder nicht in die Spinner-Liste geladen, wie auf meinem unteren Bildschirm. Warum tritt dieses Problem auf?

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>

Hauptaktivität:-

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>

Benutzerdefinierter Adapter: -

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

Zeilenelement: -

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

bildschirm: - --- enter image description here

23
AbhiRam

Senden Sie in Ihrem Adapterkonstruktor auch die ID von textview

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

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

nennen Sie es durch

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

EDIT Ihre Bilder werden nicht angezeigt, weil Sie die Methode getDropdownView () nicht überschrieben haben. Diese Methode legt das Layout des untergeordneten Elements fest, wenn das Dropdown-Menü angezeigt wird. Fügen Sie diese Methode Ihrem Adapter hinzu

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

Vorschlag Setzen Sie in Ihrem getView () ein Häkchen für (convertview == null). Dies hat möglicherweise keine Auswirkungen auf diesen kleinen Adapter. Wenn Ihr Adapter jedoch mehr Elemente enthält, wirkt sich dies auf die Leistung aus.

EDIT Um Ihr Spinner-Dropdown unter den Anker zu setzen, verwenden Sie

Android:overlapAnchor="false" in deinem 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