I want more required data from my Spinner but unable to get. This is my list ["id":"1","title":"test 1","tableid":"my_value"]
and my Spinner showing dropdown of table
to users and I want my_value
from list. But I want to show users to table
. How can I achieve this?
table
is properly showing on dropdown
but item is not selecting
and unable to get my_value
data.
This is my Model Class
public class myModel {
String id;
String title;
String tableid;
public String getTitle() {
return title;
}
public String getTableId() {
return tableid;
}
public String getId() {
return id;
}
@NonNull
@Override
public String toString() {
return getTitle();
}
}
This is spinner XML
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_weight="1" />
This is Spinner Code
ArrayList<myModel> dropList = new ArrayList<>();
//DropList data is like this = ["id":"1","title":"test 1","tableid":"my_value"]
ArrayAdapter<myModel> adapter = new ArrayAdapter<>(this, R.layout.my_spinner_textview, dropList); // my_spinner_textview is same as simple_spinner_item_view
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCategory.setAdapter(adapter);
spinCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
myModel modelcls = dropList.get(position);
String val= modelcls.getTableId();
String val2= modelcls.getTitle();
Toast.makeText(ShayariEditor.this, "value1: "+val, Toast.LENGTH_SHORT).show();
Toast.makeText(ShayariEditor.this, "value2: "+val2, Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I found the solution.
Just create a custom Array with different data and can easily access them using position
of onItemSelected.
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
It works very fine. You'll achieve 2 data with the same spinner.