1

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.

2
  • 1
    What are you passing to "messages"? Commented Feb 19, 2013 at 10:00
  • @AmeerMoaaviah updated my question please look at it Commented Feb 19, 2013 at 10:11

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

6 Comments

itz not allowing me to do...i edited my question ..please look at it
@njzk2 in my question i have a method which is returning messages how can i convert into string?
you have to override Message to include the toString method given by Waqas. Otherwise how do you expect the ArrayAdapter to know what you want it to put in the textview ? (alternate solution : put strings in your array)
@Waqas hi thank you now itz working i override toString() in my message pojo class where my getters and setters arethere and itz working thxxx
@teekib nice that it helped :)
|

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.