I tried to create to custom spinner and almost succeed. Spinner shows weird values in dropdown scenario but when item is selected i get correct value...the problem is why i am getting weird values?
spinnerActivity.java
public class customSpinner extends AppCompatActivity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_spinner);
spinner=(Spinner)findViewById(R.id.spinner_id);
ArrayList<spinner_info> arrayList1 = new ArrayList<>();
arrayList1.add(new spinner_info("a"));
arrayList1.add(new spinner_info("b"));
arrayList1.add(new spinner_info("c"));
arrayList1.add(new spinner_info("d"));
spinner_adapter sAdapter = new spinner_adapter(this,arrayList1);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(sAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
spinner_Adapter.java
public class spinner_adapter extends ArrayAdapter<spinner_info> {
public spinner_adapter(Context context,ArrayList<spinner_info> arrayList1) {
super(context,0,arrayList1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View viewItem = convertView;
if(viewItem==null)
{
viewItem= LayoutInflater.from(getContext()).inflate(spinner_item,parent,false);
}
spinner_info currentinfo=getItem(position);
TextView name = (TextView) viewItem.findViewById(R.id.item_spinner);
name.setText(currentinfo.getSub());
return viewItem;
}
}
spinner_Info.java
public class spinner_info{
String subject1;
public spinner_info(String subject){
subject1=subject;
}
public String getSub() {return subject1;}
}
spinner_Item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:textSize="18sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_spinner" />
</LinearLayout>
The ArrayAdapter
basically just calls toString()
on the value it finds in the array. You're using a custom POJO ( spinner_info
) with one field that's a String, so it's taking your class and calling toString()
which gives you the fully-qualified class name and the memory address.
Given that your POJO has just the String field, you can just get rid of that POJO and pass the ArrayAdapter
a list of Strings and your Spinner
should display them properly. Then when one is selected, you can check the value of the String.
Implementation
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("a");
arrayList1.add("b");
arrayList1.add("c");
arrayList1.add("d");
spinner_adapter sAdapter = new SpinnerAdapter(this, arrayList1);
spinner.setAdapter(sAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = arrayList1.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerAdapter class
public class SpinnerAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
public List<String> strings;
public SpinnerAdapter(Context context, List<String> strings) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.strings = strings;
}
@Override
public int getCount() {
return strings != null ? strings.size() : 0;
}
@Override
public String getItem(int position) {
return strings != null ? strings.get(position) : null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.spinner_item, parent, false);
}
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.spinner_dropdown_item, parent, false);
}
return convertView;
}
}
You'll need to set the text of the view in the appropriate methods inside the adapter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.