如何获取 autocompletetextview 的 id?

How to get id of autocomplettextview?

我有两个自动完成文本视图,在我的第一个自动完成中,我收到了服务器的响应,

    {"status":"success","clientlist":[{"cid":"1","name":"margi"},{"cid":"2","name":"steven"}],"productboxtype":[{"pbxid":"1","pbxname":"1 Dozen","qtyperbox":"12"},{"pbxid":"2","pbxname":"2 Dozens","qtyperbox":"24"},{"pbxid":"3","pbxname":"3 Dozens","qtyperbox":"36"}]}

我能够在我的第一个自动完成中获取名字并且它工作正常,

现在的问题是假设用户 select 项目 "margi" 并且它的 cid 是 1,所以我再次向服务器发送请求并尝试获取 'margi' 的产品名称及其响应是

{"status":"success","clientproduct":[{"pid":"4","name":"kangan pair","unitprice":"1500","boxqty":"1"}]}

但在史蒂文 selection 之后它仍然在第二次自动完成中显示 kangan pari

     @Override
    protected String doInBackground(String...args) {
        //Check for success tag
        //int success;
        Looper.prepare();



         try {

             JsonParseClientList jp=new JsonParseClientList();
                List<NameValuePair> params = new ArrayList<NameValuePair>();
             List<SuggestGetSetClientList> list =jp.getParseJsonWCF(acTextView.getText().toString());

                 for(int i = 0;i<list.size();i++)
                 {
                   if(list.get(i).getName().equals(acTextView.getText().toString()))
                    params.add(new BasicNameValuePair("cid",list.get(i).getId()));

                     //  catid=list.get(i).getId();



                 }

                 for(int b=0;b<list.size();b++)
                 {
                     catidtemp=list.get(b).id.toString();

                     System.out.println("cattttttiiiiddd????"+catidtemp);
                     break;
                 }

                //catidtemp=String.valueOf(selected_cid);

             System.out.println("cattttttiiiiddd????"+catidtemp);
             params.add(new BasicNameValuePair("action", "clientproduct"));

             System.out.println("su gayu server ma????"+params);

             Log.d("request!", "starting");
             // getting product details by making HTTP request
             JSONObject json = jsonParser.makeHttpRequest (
                 FEEDBACK_URL, "POST", params);
             //check your log for json response
             Log.d("Login attempt", json.toString());





             return json.getString(FEEDBACK_SUCCESS);

         }catch (JSONException e) {
             e.printStackTrace();
         }
         return null;
    }

    // After completing background task Dismiss the progress dialog

    protected void onPostExecute(String file_url) {
        //dismiss the dialog once product deleted
        pDialog.dismiss();

        //parentcat.getText().clear();
}}

如果你想比较 'boxqty' 和 'minimum_qty',在你的 if 条件下,你不应该使用它 .length() for boxqty,因为它只会计算字符串的长度,用户将输入,而不是它的值。

我认为当您 select "steven" 时,您会得到 clientproduct 数组 null 作为响应。这就是为什么你的 catch 块被调用并且你的秒数自动完成数组不清楚的原因。这就是为什么您在第二个自动完成中获得先前值的原因。

希望你能理解:) 快乐编码:)

JSONObject resultObject = new JsonObject(result); //where result is {"status":"success","clientlist":[{"cid":"1","name":"margi"},{"cid":"2","name":"steven"}],"productboxtype":[{"pbxid":"1","pbxname":"1 Dozen","qtyperbox":"12"},{"pbxid":"2","pbxname":"2 Dozens","qtyperbox":"24"},{"pbxid":"3","pbxname":"3 Dozens","qtyperbox":"36"}]}
JSONArray clientListArray = resultObject.getJSONArray("clientlist");
for (int i=0; i<clientListArray.length(); i++){
     JSONObject client = clientListArray.getJSONObject(i);
     int id = client.getInt("cid");
     ...
     Client clientInfo = new Client(id, name);
     clientList.add(clientInfo)
     ....
}

然后您可以将 clientList 用于适配器或其他东西。

您可以创建一个 class 来保存您的信息

public class Client{
    int id;
    String name;

    //getter and setter for this + constructor;
}

为了不使您的代码检查复杂化:https://code.google.com/p/google-gson/ 并查找一些可以更好地解释用法的教程

此外,如果您更改适配器信息,请不要忘记致电:

adapter.notifyDataSetChanged();