单击任何列表视图项时,将该项文本显示为EditText

时间:2014-12-22 04:16:39

标签: java android listview android-listview android-edittext

我从String数组中获取ListItem的值并将它们显示为ListView,我也添加了EditText字段,这样当用户输入一些文本时,listview只显示匹配的项目。我担心的是,当用户点击任何listview项目时,必须在edittext字段中显示此项目中的文本,我尝试过很多方法,例如onitemclicklistener,viewgroup但是没有成功,每次都会出错。以下是我的代码,请帮助我。 MainActivity.java

private ListView lv;
    // Listview Adapter
    ArrayAdapter<String> adapter;
    // Search EditText
    EditText inputSearch;
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Listview Data
        final String products[] = getResources().getStringArray(R.array.Countreis);
        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
        lv.setVisibility(View.GONE);
        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);
                if(cs.length()== 0){
                    lv.setVisibility(View.GONE);
                }else{
                lv.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="textVisiblePassword"/>
    <!-- List View -->

    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 </LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- Single ListItem -->
 <!-- Product Name -->
    <TextView android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold"/>   
 </LinearLayout>

String Array:

    <string-array name="Countreis">
        <item >India</item>
        <item >Pakistan</item>
        <item >Bnagladesh</item>
        <item >Nepal</item>
        <item >China</item>
        <item >Russia</item>
        <item >America</item>
        <item >Ingland</item>
        <item >Japan</item>
        <item >Arab emirat</item>
        <item >Saudi Arabia</item>
        <item >Austrelia</item>
        <item> Kuwiat</item>
        <item >Israil</item>
   </string-array>

2 个答案:

答案 0 :(得分:2)

实现ListView项目单击侦听器:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         inputSearch.setText(adapter.getItem(position));
    }
});

答案 1 :(得分:1)

试试这个:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        inputSearch.setText((String) lv.getAdapter().getItem(position));
    }
});