I have pojo with name and id and pojo is set to arraylist now i want to displace only name from the array list.
How to implement this???
I need to store both name and id in arraylist but show only name in spinner
Try this
Create a Pojo class like this
public class POJO
{
String id,name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
than add data name in to spinner like this
ArrayList<POJO>pojoArrayList= new ArrayList<>();
for (int i=0;i<20;i++){
POJO pojo= new POJO();
pojo.setId(i+"");
pojo.setName("name "+i);
pojoArrayList.add(pojo);
}
String []SpnName=new String[pojoArrayList.size()];
String []SpnID=new String[pojoArrayList.size()];
for (int i=0;i<pojoArrayList.size();i++){
SpnName[i]=pojoArrayList.get(i).getName();
SpnID[i]=pojoArrayList.get(i).getId();
}
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, SpnName);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Cliked Id :"+SpnID[i], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Try this
ArrayList<SecretQuestion> secretQuestionsArrayList = new ArrayList<>();
ArrayList<String> secretQueName = new ArrayList<>();
for (int i = 0; i < secretQuestionsArrayList.size(); i++) {
Log.i("Secrete Name"," : "+ secretQuestionsArrayList.get(i).getName());
secretQueName.add(secretQuestionsArrayList.get(i).getName());
}
arrayAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, secretQueName);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
questionSpinner.setAdapter(arrayAdapter);
EDIT
questionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String Questionname = questionSpinner.getSelectedItem().toString();
String Id = secretQuestionsArrayList.get(position).getId();
Log.i("Name"," : "+ Questionname);
Log.i("Id"," : "+ Id);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I will advise you to check android developers website or search google before asking.. anyway your code is not far from correct, and maybe it will work, but in your code the secretQuestionsArrayList is empty and has no data. Please populate your arrayList first, and then pass it to the adapter. In that case you will see the items in the spinner
Edit:
If you want to get the ex. String property from the secretQuestionsArrayList then you will need to get the property and add it to a new list that will contain only the String properties from secretQuestionsArrayList and then pass that list to the adapter
Like this is your pojo class of SecretQuestion
public class SecretQuestion{
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Get only name to the adapter :
secretQuestionsArrayList.getName()
Full code :
ArrayList<SecretQuestion> secretQuestionsArrayList = new ArrayList<>();
arrayAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, secretQuestionsArrayList.getName());
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
questionSpinner.setAdapter(arrayAdapter);