我尝试了AutoCompleteTextView并获取了值但我无法获取autocompletetextview的click事件中的值这是我的布局
<AutoCompleteTextView
android:id="@+id/atv_places"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/lin"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/et_chat"
android:drawableLeft="@drawable/searchblue"
android:drawablePadding="4dp"
android:dropDownVerticalOffset="5dp"
android:dropDownWidth="match_parent"
android:hint="Search contacts..."
android:scrollHorizontally="true"
android:textColor="@color/Black"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="16dp"
android:visibility="gone" />
和我的点击事件代码,以获取我通过适配器填充的值。我无法获得点击的值,或者无法使用吐司。
atvPlaces = (AutoCompleteTextView) v.findViewById(R.id.atv_places);
mTxtPhoneNo = (AutoCompleteTextView) v.findViewById(R.id.mmWhoNo);
mAdapternew = new SimpleAdapter(getActivity(), mPeopleList,
R.layout.row, new String[] { "Name", "Phone" }, new int[] {
R.id.textView1, R.id.textView2 });
atvPlaces.setAdapter(mAdapternew);
atvPlaces.setThreshold(1);
atvPlaces.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> av, View arg1, int index,
long arg3) {
Toast.makeText(getActivity(), "auto", Toast.LENGTH_LONG).show();
Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
String name = map.get("Name");
String number = map.get("Phone");
atvPlaces.setText(""+name+"<"+number+">");
}
});
答案 0 :(得分:1)
我没有得到您的代码中的问题,但是我在代码中生成的代码及其工作正常,请自行检查。
AutoCompleteTextView atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
ArrayList<HashMap<String,String>> mPeopleList=new ArrayList<>();
HashMap h1 = new HashMap<String, String>();
h1.put("Name","name1");
mPeopleList.add(h1);
HashMap h2 = new HashMap<String, String>();
h2.put("Name","name2");
mPeopleList.add(h2);
final SimpleAdapter myAdapter = new SimpleAdapter(this, mPeopleList,
android.R.layout.simple_spinner_dropdown_item, new String[] { "Name"}, new int[] {
android.R.id.text1});
atvPlaces.setAdapter(myAdapter);
atvPlaces.setThreshold(1);
atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View arg1, int index,
long arg3) {
Toast.makeText(MainActivity.this, "auto", Toast.LENGTH_LONG).show();
HashMap<String, String> map = (HashMap<String, String>) av.getItemAtPosition(index);
String name = map.get("Name");
String number = map.get("Phone");
}
});