teekib
teekib

Reputation: 2821

Android: passing string into ArrayAdapter

I have a spinner and i am dynamically loading the spinner with messages i am doing it as below

protected List<Message> messages = null;
ArrayAdapter <Message> arrayadapter = new ArrayAdapter<Message>( activity,android.R.layout.simple_spinner_item,messages);               

                       spinner1.setAdapter(arrayadapter); 

i am getting the message as below

public List<Message> getMessages(String type) throws Exception{
    HttpClient client = new HttpClient();
    GetMethod get = null;
    Message message = null;
    List<Message> messages = new ArrayList<Message>();
    try {
        get = new GetMethod("http://" + Constants.CLOUD_SERVER_URL + Constants.getMessages + "/"+type);
        int statusCode = client.executeMethod(get);
        if(statusCode == 200){
            Log.d(TAG, "Successfully posted the comments");
            Utils utils = new Utils();
            Document doc = utils.formatInputStream(get.getResponseBodyAsStream());
            NodeList listMessages = doc.getElementsByTagName("message");
            int totalMessage = listMessages.getLength();
            for (int s = 0; s < totalMessage; s++) {
                Node messageNode = listMessages.item(s);
                if (messageNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element messageElement = (Element) messageNode;
                    message = new Message();
                    message.setId(utils.getTagValue(messageElement, "id"));
                    message.setMessage(utils.getTagValue(messageElement, "question"));
                    messages.add(message);

                    Log.d(TAG, "Id - " + message.getId() + " Message - " + message.getMessage());
                }
            }
        }else{
            Log.d(TAG, "Either server has problem or not reachable at this time");
            throw new Exception("Server Issue Status code return by server is " + statusCode);
        }
        Log.d(TAG, "recommend ----------> Step 15");
        Log.d(TAG, "recommend ----------> Step 20");
    }finally{
        get.releaseConnection();
        get = null;
    }
    return messages;
}

my issue is i am getting id of the message in spinner not the message text how to convert that message into string and pass it to adapter or any other way that finally i get the text in the spinner.

Upvotes: 1

Views: 241

Answers (1)

Waqas
Waqas

Reputation: 6802

One way is to override toString() method of your Message class and return the message value like this:

@Override
public String toString() {
    return message; //where message is member variable holding message text
}

Upvotes: 3

Related Questions