如何使用mysql中的数据从我的微调器中选择ID

时间:2017-10-24 02:06:36

标签: php android mysql json spinner

大家好我刚接触到android并且我在从mysql加载的微调器中获取“id”时遇到问题。 事情是我需要一个特定的“id”取决于我选择的选项,其加载的信息,但我不知道如何获得该ID。例如,我选择了名为“Joseph”的客户端,其ID为“24”,如何在吐司或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;}

}

1 个答案:

答案 0 :(得分:0)

不使用ArrayAdapter<String>,而是使用ArrayAdapter<ListarClientes>并在类ListarClientes中覆盖方法toString并返回名称。

将此方法添加到您的班级:

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

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

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

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