问题。在android中的autocompleteTextView中获取位置arraylist

时间:2013-05-17 13:06:51

标签: android

我开始学习Android了。我有问题。我有autocompleteTextView,内容是arrayList。然后,当我点击autoCompleteTextView时,我想获得arraylist中元素的位置。你能告诉我这样做吗?

非常感谢你!

此代码!

/* ================= looping through All categories ==================== */
        for(int i=0;i<product.length();i++){
            JSONObject c = product.getJSONObject(i);
            // Storing each json item in variable
            pro_id = c.getString(TAG_ID);
            pro_name = c.getString(TAG_NAME);

            name_product.add(pro_name);
        } // end for products
    } catch (JSONException e) {
        e.printStackTrace();
    }   

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_product);
    AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.result_search);

    textView.setThreshold(1);
    textView.setAdapter(adapter);
    textView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

          //here position is your selected item id

          /*String selecteditem =name_product.get(position);
            Log.d("Test",selecteditem.toString());
            int pos = Integer.parseInt(selecteditem);
            Toast.makeText(getApplicationContext(), "cvxcvcv",pos).show();*/

            // I want to get position when i click element in autocompletetextView
        }
    });

1 个答案:

答案 0 :(得分:1)

尝试

    autocompletetextview.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

              //here position is your selected item id

              int selectedposition=position;


            }
        });