0

我开始学习安卓了。我有问题。我有 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
        }
    });
4

1 回答 1

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;


            }
        });
于 2013-05-17T13:14:42.343 回答