Android:将字符串传递给ArrayAdapter

时间:2013-02-19 09:58:19

标签: android android-arrayadapter

我有一个微调器,我正在动态加载微调器和消息 我这样做如下

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

                       spinner1.setAdapter(arrayadapter); 

我收到如下信息

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

我的问题是我在微调器中获取消息的id而不是消息文本 如何将该消息转换为字符串并将其传递给适配器或 最后我在旋转器中得到文本的任何其他方式。

1 个答案:

答案 0 :(得分:3)

一种方法是覆盖toString()类的Message方法并返回消息值,如下所示:

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