如何获得使用ArrayAdapter微调项的ID

如何获得使用ArrayAdapter微调项的ID

本文介绍了Android的:如何获得使用ArrayAdapter微调项的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的微调载有一些信息,每条都有伊茨自己的ID,

 公共类信息{
    私人字符串ID;
    私人字符串信息;
    公共字符串的getId(){
        返回ID;
    }
    公共无效SETID(字符串ID){
        this.id = ID;
    }
    公共字符串的getMessage(){
        返回消息;
    }
    公共无效setMessage(字符串消息){
        this.message =消息;
    }

     @覆盖
     公共字符串的toString(){
         返回消息;
     }
}
 

我通过这个的消息来Arrayadapter,

 保护名单,其中;消息>消息=无效;
    ArrayAdapter<消息> arrayadapter =新的ArrayAdapter<消息>(活动,android.R.layout.simple_spinner_item,消息);
                        arrayadapter.setDropDownViewResource(R.layout.textview);
 spinner1.setAdapter(arrayadapter);
 

现在如何,我可以得到该特定消息的ID选择?我能做到这一点与arrayadapter。任何帮助pciated AP $ P $

解决方案

  spinner1.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
        公共无效onItemSelected(适配器视图<>母公司视图中查看,
                INT POS,长I​​D){
            消息mSelected =(消息)parent.getItemAtPosition(POS);
            Log.i(ID:,mSelected.getId());

        }

        @覆盖
        公共无效onNothingSelected(适配器视图<>为arg0){
            // TODO自动生成方法存根
            Log.i(消息,没有选择);

        }


    });
 

My spinner is loaded with some messages ,each message has itz own id,

public class Message {
    private String id;
    private String message;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

     @Override
     public String toString() {
         return  message;
     }
}

I am passing this message to Arrayadapter,

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

Now how can i get the id of that particular message selected?can i do it with arrayadapter .Any help is appreciated

解决方案
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            Message mSelected = (Message) parent.getItemAtPosition(pos);
            Log.i("Id:", mSelected.getId());

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            Log.i("Message", "Nothing is selected");

        }


    });

这篇关于Android的:如何获得使用ArrayAdapter微调项的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:35