简体   繁体   English

我如何从MySQL的数据中从微调器中选择ID

[英]How can i get the ID selected from my spinner with data from mysql

Hello everybody im new on android and i'm having a problem getting the "id" from my spinner loaded from mysql. 大家好,我是android新手,我在从mysql加载微调器中获取“ id”时遇到问题。 the thing is i need an specific "id" depending on which option i selected, the info its loading well but i dont know how to get that id. 事情是我需要一个特定的“ id”,具体取决于我选择的选项,该信息的加载良好,但我不知道如何获取该id。 For example, i selected my client named "Joseph" and its id is "24", how can i get that "24" on a toast or on a textview. 例如,我选择了一个名为“ Joseph”的客户,其ID为“ 24”,如何在烤面包或textview上获取“ 24”。 i leaving my code here so i hope you can helpme, 我将代码留在这里,希望对您有所帮助,

On Create --> 创建时->

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

} }

Instead of using ArrayAdapter<String> , use ArrayAdapter<ListarClientes> and in the class ListarClientes , override the method toString and return name. 代替使用ArrayAdapter<String> ,而使用ArrayAdapter<ListarClientes>并在类ListarClientes中覆盖方法toString并返回名称。

Add this method to your class: 将此方法添加到您的类中:

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

Then, instead of passing labels , directly pass clienteList while creating the adapter. 然后,在创建适配器时,直接传递clienteList而不是传递labels

After that you can get an item of type ListarClientes inside the onItemSelected method. 之后,你可以得到类型的项目ListarClientes内部onItemSelected方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用bootstrap和Ajax基于所选ID从MySQL数据库获取数据? - How can I get the data from MySQL database based on the selected ID using bootstrap and Ajax? 从 mysql 数据库中获取微调器选定的项目 ID - Get spinner selected item id from mysql database 如何将选定表行中的 ID 传递到我的 AJAX function 以获取数据 - How can I pass an ID from selected table row to my AJAX function that fetching data 如何获取我在表中选择的数据行的 ID? - How can I get the id of the datarow I selected in my table? 如何从 PHP 中的 mysql 数据库获取用户 ID? - How can I get the user ID from mysql database in PHP? 如何使用从使用 PHP、MySQL、Bootstrap 的数据库获取的数据? - How can I use the data I get from my database using PHP, MySQL, Bootstrap? 如何从Paypal取回数据,以便可以相应地更改MySQL数据库? - How do I get data back from Paypal so I can alter my MySQL database accordingly? 如何使用MySQL从一个微调器到另一个微调器获取值? - How to get values from one spinner to another spinner using MySQL? 如何从mysql中获取数据区分大小写? 我的语言是php - How can I get case sensitive from getting data in mysql? my language is php 如何在Xcode应用程序中使用PHP从MySql获取数据? - How can I get data from MySql using PHP in my Xcode app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM