【问题标题】:How can i get the ID selected from my spinner with data from mysql如何使用来自 mysql 的数据获取从微调器中选择的 ID
【发布时间】:2017-10-24 02:06:35
【问题描述】:

大家好,我是 android 新手,我在从 mysql 加载的微调器中获取“id”时遇到问题。 问题是我需要一个特定的“id”,具体取决于我选择的选项,它的信息加载良好,但我不知道如何获取该 id。例如,我选择了名为“Joseph”的客户,它的 id 是“24”,我怎样才能在 toast 或 textview 上获得“24”。我把我的代码留在这里,希望你能帮助我,

创建时 -->

    spinnercliente = (Spinner) findViewById(R.id.vencliente);
    clienteList = new ArrayList<ListarClientes>();
    // seleccionar las frutas del spinner
    spinnercliente.setOnItemSelectedListener(this);
    new Getcliente().execute();
}
private void populateSpinner() {
    List<String> lables = new ArrayList<String>();
    for (int i = 0; i < clienteList.size(); i++) {
        lables.add(clienteList.get(i).toString());
    }
    ArrayAdapter<ListarClientes> spinnerAdapter = new ArrayAdapter<ListarClientes>(this, android.R.layout.simple_spinner_item,clienteList);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercliente.setAdapter(spinnerAdapter);
}
private class Getcliente extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(RegFacturas.this);
        pDialog.setMessage("Obteniendo Clientes..");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_LISTA_CLIENTE+"?Usuario="+datoNombre, ServiceHandler.GET);
        Log.e("Response: ", "> " + json);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray cliente = jsonObj
                            .getJSONArray("clientes");

                    for (int i = 0; i < cliente.length(); i++) {
                        JSONObject catObj = (JSONObject) cliente.get(i);
                        ListarClientes cat = new ListarClientes(catObj.getInt("id"),
                                catObj.getString("nombre"));
                        clienteList.add(cat);
                    }
                }else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(RegFacturas.this);
                    builder.setMessage("Error al cargar los clientes").setNegativeButton("Aceptar", null).create().show();
                    finishActivity(1);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "¿No ha recibido ningún dato desde el servidor!");
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
    Toast.makeText(adapterView.getContext(), (String) adapterView.getItemAtPosition(pos), Toast.LENGTH_SHORT).show();
    ListarClientes client = (ListarClientes) adapterView.getItemAtPosition(pos);
    idcli = client.getId();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {    }
}

ListarClienteClass

public class ListarClientes {
private int id;
private String name;
public ListarClientes() {}
public ListarClientes(int id, String name) {
    this.setId(id);
    this.setName(name);
}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
//public String getName() {return name;}
@Override
public String toString(){return name;}
public void setName(String name) {this.name = name;}

}

【问题讨论】:

    标签: php android mysql json spinner


    【解决方案1】:

    不要使用ArrayAdapter&lt;String&gt;,而是使用ArrayAdapter&lt;ListarClientes&gt;,并在ListarClientes类中覆盖方法toString并返回名称。

    将此方法添加到您的类中:

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

    然后,在创建适配器时,直接传递clienteList,而不是传递labels

    之后,您可以在onItemSelected 方法中获取ListarClientes 类型的项目。

    ListarClientes client = (ListarClientes) adapterView.getItemAtPosition(pos);
    int id = client.getId(); 
    

    【讨论】:

    • 这是我的 ListarClientes 类,当你说“覆盖方法 toString 并返回名称”时,我需要更改什么?,我正在编辑我的代码以便你可以看到它
    • 作为一个问题,我是否也需要覆盖 id?,代码在问题上
    • 不,你不需要。你已经有了方法getId()。方法 toString() 应该被覆盖,以便名称将显示在微调器中。
    • 应用程序停止了,所以我觉得有些东西放错了地方,你能检查一下代码吗?拜托。我更新它
    • 删除吐司。或者不要将其转换为字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多