Spinner getselecteditem来自地图

时间:2014-03-10 18:56:34

标签: android map hashmap spinner

public void QuerySQL_Process(String SQLCMD){

ResultSet rs;

    try{

        Statement statement = connect.createStatement();
        rs = statement.executeQuery(SQLCMD);

        //CONFIGURAR O SIMPLE ADAPTER
        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String,String>>();

        //RESULTSET, TODOS OS REGISTROS
        while(rs.next()){
            Map<String, String> datanum = new HashMap<String, String>();
            datanum.put("A", rs.getString("id_process"));
            datanum.put("B", rs.getString("name"));
            data.add(datanum);
        }

        String[] from = {"A", "B"};
        int[] cb_process = {R.id.tId_Process, R.id.tProcessName};

        //MODELO LAYOUT
        AD = new SimpleAdapter(this, data, R.layout.model_process, from, cb_process);

        //CARREGAR SPINNER PROCESSOS COM OS DADOS DO BANCO
        PROCESS.setAdapter(AD); //PROCESS SPINNER

    }catch (Exception e){
        Log.e("ERROR: ",e.getMessage());
    }

}

EXECUTE.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //QUANTIDADE DE ATENDIMENTOS
            Toast toast = Toast.makeText(getApplicationContext(),"Process: "+PROCESS.getSelectedItem()+"\nQuantidade de Atendimentos: "+ quantidade, Toast.LENGTH_LONG);
            toast.show();

        }
    });

尝试使用此代码“onItemSelected”,但选中的字符串返回“ null ”。

PROCESS.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            selected = (String) data.get(PROCESS.getSelectedItemPosition()).get("name");
            Toast toast = Toast.makeText(getApplicationContext(),"Process: "+selected, Toast.LENGTH_LONG);
            toast.show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

嗨,我想知道如何从Toast(PROCESS.getSelectedItem())获取“id_process”或“name”,使用上面的代码返回:Process:{A = 1,B = SOLICITADOS}它返回map postition。我想知道如何恢复id_process或名称。

由于

1 个答案:

答案 0 :(得分:0)

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> tmp = (Map<String, String>) PROCESS.getSelectedItem();
                Toast toast = Toast.makeText(getApplicationContext(),"Process: "+tmp.get("A"), Toast.LENGTH_LONG);
                toast.show();
}

但是您上面的代码是正确的,除了您应该使用“ A”代替“ name”

@Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub
        selected = (String) data.get(PROCESS.getSelectedItemPosition()).get("A");
        Toast toast = Toast.makeText(getApplicationContext(),"Process: "+selected, Toast.LENGTH_LONG);
        toast.show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }