Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
[Java] Custom SpinnerAdapter Example
#1
Merhaba arkadaşlar,

Custom Spinner Adapter Örnek kodlarını sizlerle paylaşmak istiyorum.

Java kısmı kod olarak paylaştım xml kısmına proje dosyasından bakabilirsiniz.

URL: https://disk.yandex.com.tr/d/RzOykwqh3lIZ4w

[Resim: 3kznezy.png] [Resim: d0f6oar.png]

MainActivity.java
Kod:
package com.aturan.customspinnerexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Spinner spinner;
    private TextView textView;
    private CustomSpinnerAdapter adapter;
    private ListItem listItem;

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

        spinner = findViewById(R.id.main_aktivity_spinner);
        textView = findViewById(R.id.main_aktivity_textview);

        adapter = new CustomSpinnerAdapter(this, ListItem.getData(this));
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                listItem = (ListItem) parent.getItemAtPosition(position);
                textView.setText(listItem.getStrBayrak());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                listItem = (ListItem) parent.getItemAtPosition(0);
                textView.setText(listItem.getStrBayrak());
            }
        });


    }
}

ListItem.java


Kod:
package com.aturan.customspinnerexample;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.ArrayList;

public class ListItem {
    private Bitmap imgBayrak;
    private String strBayrak;

    public ListItem(Bitmap imgBayrak, String strBayrak) {
        this.imgBayrak = imgBayrak;
        this.strBayrak = strBayrak;
    }

    public Bitmap getImgBayrak() {
        return imgBayrak;
    }

    public String getStrBayrak() {
        return strBayrak;
    }

    public static ArrayList<ListItem> getData(Context context) {
        ArrayList<ListItem> listItems = new ArrayList<>();

        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.turkiye), "Türkiye"));
        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.almanya), "Almanya"));
        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.ingiltere), "İngiltere"));
        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.fransa), "Fransa"));
        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.cin), "Çin"));
        listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.abd), "ABD"));

        return listItems;
    }
}

CustomSpinnerAdapter.java

Kod:
package com.aturan.customspinnerexample;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomSpinnerAdapter implements SpinnerAdapter {
    private Context context;
    private ArrayList<ListItem> listItems;
    private ImageView imgBayrak;
    private TextView textBayrak;

    public CustomSpinnerAdapter(Context context, ArrayList<ListItem> listItems) {
        this.context = context;
        this.listItems = listItems;
    }

    @Override
    public View getDropDownView(int position, View view, ViewGroup parent) {
        view = LayoutInflater.from(context).inflate(R.layout.spinner_item_dropdown, null);

        imgBayrak = view.findViewById(R.id.spinner_item_dropDown_imgBayrak);
        textBayrak = view.findViewById(R.id.spinner_item_dropDown_textBayrak);

        imgBayrak.setImageBitmap(listItems.get(position).getImgBayrak());
        textBayrak.setText(listItems.get(position).getStrBayrak());


        return view;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

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

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

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        view = LayoutInflater.from(context).inflate(R.layout.spinner_item, null);

        imgBayrak = view.findViewById(R.id.spinner_item_imgBayrak);
        textBayrak = view.findViewById(R.id.spinner_item_textBayrak);

        imgBayrak.setImageBitmap(listItems.get(position).getImgBayrak());
        textBayrak.setText(listItems.get(position).getStrBayrak());

        return view;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {

        if (listItems.size() > 0)
            return false;
        else
            return true;

    }
}
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  [Andoid Java] MyBB User Login Example App root 0 8,036 02-02-2022, 00:27
Son Yorum: root
  [Android Java] Galeriden Resim Seçme root 0 1,204 18-01-2022, 11:03
Son Yorum: root
  Android Custom Alert Dialog root 0 1,272 03-12-2021, 12:12
Son Yorum: root
  Custom GridView Adaptor Örnek Kod Şablonu root 0 1,044 01-12-2021, 16:02
Son Yorum: root
  RecycleListView Custom Adaptor Örnek Kod Şablonu root 0 1,060 01-12-2021, 15:05
Son Yorum: root
  Android Custom ListView ve setSelection Administrator 0 4,293 01-04-2016, 19:48
Son Yorum: Administrator

Hızlı Menü:


Konuyu Okuyanlar: 1 Ziyaretçi