0

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

0

4 Answers 4

1

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) {

        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

need only name in spinner
@Hemavathi check the code you will only get name in spinner
@NileshRathod Jay dwarkadhish
0

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) {

            }
        });

5 Comments

@Hemavathi Happy to help you.
on selecting a question i need to get Id from arraylist and store it in string
@Hemavathi you selecting a question you get both ID and Name. please check my edit answer.
@Hemavathi if you are solve your problem then select ans as correct.
@Hemavathi Thanks, Happy to help you.
0

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

Comments

0

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

2 Comments

secretQuestionsArrayList.getName()); not avaiable
Can you update your Question with SecretQuestion class, So i can able to understand @Hemavathi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.