如何从外部数据库加载的自动完成文本值中获取基于服务器的ID

时间:2014-07-09 13:49:16

标签: android sql autocompletetextview

我已经实现了AutoCompleteTextView并填充了外部数据库中的值。我想要做的是当点击一个值时,我希望获得与服务器中相同值的Id

因此,如果Injection Id在外部数据库中为72,当我在我的Android应用程序中单击它时,我得到72作为id。

到目前为止,我已经尝试过以下代码,但是它给我的位置不是id。

@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), list2.get(position).toString(),
                Toast.LENGTH_SHORT).show();




                Object data = parent.getAdapter().getItem(position);
                int realPosition = list2.indexOf(data);
                String str_realPositionMode = String.valueOf(realPosition);
                 Log.d("Chek id for autocomplete",str_realPositionMode);

            }

使用JSON重播数据:

try
    {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://datanetbeta.multi-trade.co.uk/tablet/appModelNames.php");
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("Pass 1", "connection success ");
    }
    catch(Exception e)
    {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
    }     

    try
    {
        BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();

        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }

        is.close();
        result = sb.toString();
        Log.e("Pass 2", "connection success ");
    }
    catch(Exception e)
    {
        Log.e("Fail 2", e.toString());
    }     

   try{
        JSONArray JA=new JSONArray(result);
        JSONObject json= null;
        final String[] str2 = new String[JA.length()]; 

        for(int i=0;i<JA.length();i++)
        {
            json=JA.getJSONObject(i);
            str2[i] = json.getString("appliances_models_id");     // retrieving id
            str2[i]=json.getString("appliances_models_name");
        }
        final List<String> list2 = new ArrayList<String>();

        for(int i=0;i<str2.length;i++)
        {
            list2.add(str2[i]);
        }
        Collections.sort(list2);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.app_mod_auto_com , list2);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        auto_complete_app_model.setThreshold(1);
        auto_complete_app_model.setAdapter(dataAdapter);

1 个答案:

答案 0 :(得分:0)

解析服务器结果时,您似乎对id和名称使用相同的数组str2。一个简单的解决方案(如果您不修改数据)将使用另一个List来保存将复制当前名称List的ID:

final List<Integer> idList = new ArrayList<Integer>();
final List<String> list2 = new ArrayList<String>(); // why aren't you using a List directly?
for(int i=0 ; i < JA.length(); i++) {
     json=JA.getJSONObject(i);
     idList.add(json.getInt("appliances_models_id")); // you said this is a int
     list2.add(json.getString("appliances_models_name"));
}

然后在OnItemClickListener你可以做到:

int theId = idList.get(position);

正确的解决方案是创建一个自定义类来保存两个数据

class Appliance {
    int id;
    String name;    

    @Override
    public String toString() {
        // this is the method that will be called by the adapter
        return name;
    }
}
你可以使用

final List<Appliance> list2 = new ArrayList<Appliance>(); 
for(int i=0 ; i < JA.length(); i++) {
     Appliance item = new Appliance();
     json=JA.getJSONObject(i);
     item.id = json.getInt("appliances_models_id")); // you said this is a int
     item.name = json.getString("appliances_models_name");  
     list2.add(item);
}
ArrayAdapter<Appliance> dataAdapter = new ArrayAdapter<Appliance>(getApplicationContext(), R.layout.app_mod_auto_com , list2);

OnItemClickListener中你有:

Appliance item = (Appliance) parent.getAdapter().getItem(position);
// use the item.id to do what you want.