自学内容网 自学内容网

Android实现自定义下拉列表绑定数据

xml界面代码:

  <LinearLayout
                android:id="@+id/LinearLayout_warehouse7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView61"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/txt_return_warehosing"
                    android:textAlignment="viewEnd" />


                <AutoCompleteTextView
                    android:id="@+id/AutoCompleteTextView_house"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"

                    android:cacheColorHint="#00000000"
                    android:completionThreshold="1"
                    android:dropDownHorizontalOffset="5dp"
                    android:ems="10"
                    android:gravity="center_vertical"
                    android:hint="请输入或选择"
                    android:textSize="14sp">

<!--                    <requestFocus />-->
                </AutoCompleteTextView>

                <ImageButton
                    android:id="@+id/ImageButton_house"
                    android:layout_width="10dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:src="@android:drawable/btn_dropdown"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/LinearLayout_location7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView71"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_weight="1"
                    android:text="@string/txt_return_location_name"
                    android:textAlignment="viewEnd" />

                <AutoCompleteTextView
                    android:id="@+id/AutoCompleteTextView_location_name"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"

                    android:cacheColorHint="#00000000"
                    android:completionThreshold="1"
                    android:dropDownHorizontalOffset="5dp"
                    android:ems="10"
                    android:gravity="center_vertical"
                    android:hint="请输入或选择"
                    android:textSize="14sp">

<!--                    <requestFocus />-->
                </AutoCompleteTextView>

                <ImageButton
                    android:id="@+id/ImageButton_location_name"
                    android:layout_width="10dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:src="@android:drawable/btn_dropdown"
                    android:textSize="14sp" />
            </LinearLayout>

java实现自定义适配器代码:

    public class CustomAutoCompleteAdapter extends ArrayAdapter<KeyValueItem> {

        private Context context;
        private int resource;
        List<KeyValueItem> itemList;
        KeyValueItem KeyValueItem_select=null;
        public CustomAutoCompleteAdapter(Context context, int resource, List<KeyValueItem> objects) {
            super(context, resource, objects);
            this.context = context;
            this.resource = resource;
        }
//        @Override
//        public KeyValueItem getItem(int position) {
//            // TODO Auto-generated method stub
//            return itemList.get(position);
//        }
//        @Override
//        public long getItemId(int position) {
//            // TODO Auto-generated method stub
//            return position;
//        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 尝试重用convertView来优化性能
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(resource, parent, false);
            }

            TextView textView_Key = convertView.findViewById(R.id.TextView_Key);
            TextView textView_value = convertView.findViewById(R.id.TextView_value);

            textView_Key.setText(getItem(position).getKey());
            textView_value.setText(getItem(position).getValue());

            textView_Key.setVisibility(View.GONE);
            // 这里不需要再设置字体大小,因为它已经在布局文件中设置了

            textView_value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyValueItem_select=(KeyValueItem)getItem(position);
                    Log.d("data1", "5value:"+KeyValueItem_select.getValue()+"-"+KeyValueItem_select.getKey());
                    AutoCompleteTextView_house.setText(KeyValueItem_select.getValue());
                    AutoCompleteTextView_house.dismissDropDown();
                    AutoCompleteTextView_house.clearFocus();


                }
            });
            textView_Key.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyValueItem_select=(KeyValueItem)getItem(position);
                    Log.d("data1", "6value:"+KeyValueItem_select.getValue()+"-"+KeyValueItem_select.getKey());
                    AutoCompleteTextView_house.setText(KeyValueItem_select.getValue());
                    AutoCompleteTextView_house.dismissDropDown();
                    AutoCompleteTextView_house.clearFocus();
                }

            });
            return convertView;
        }
    }



    public class CustomAutoCompleteLocationAdapter extends ArrayAdapter<KeyValueItem> {

        private Context context;
        private int resource;
        List<KeyValueItem> itemList;
        KeyValueItem KeyValueItem_select=null;
        public CustomAutoCompleteLocationAdapter(Context context, int resource, List<KeyValueItem> objects) {
            super(context, resource, objects);
            this.context = context;
            this.resource = resource;
        }
        //        @Override
//        public KeyValueItem getItem(int position) {
//            // TODO Auto-generated method stub
//            return itemList.get(position);
//        }
//        @Override
//        public long getItemId(int position) {
//            // TODO Auto-generated method stub
//            return position;
//        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 尝试重用convertView来优化性能
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(resource, parent, false);
            }

            TextView textView_Key = convertView.findViewById(R.id.TextView_Key);
            TextView textView_value = convertView.findViewById(R.id.TextView_value);

            textView_Key.setText(getItem(position).getKey());
            textView_value.setText(getItem(position).getValue());

            textView_Key.setVisibility(View.GONE);
            // 这里不需要再设置字体大小,因为它已经在布局文件中设置了

            textView_value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyValueItem_select=(KeyValueItem)getItem(position);
                    Log.d("data1", "5value:"+KeyValueItem_select.getValue()+"-"+KeyValueItem_select.getKey());
                    AutoCompleteTextView_location_name.setText(KeyValueItem_select.getValue());
                    AutoCompleteTextView_location_name.dismissDropDown();
                    AutoCompleteTextView_location_name.clearFocus();


                }
            });
            textView_Key.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyValueItem_select=(KeyValueItem)getItem(position);
                    Log.d("data1", "6value:"+KeyValueItem_select.getValue()+"-"+KeyValueItem_select.getKey());
                    AutoCompleteTextView_location_name.setText(KeyValueItem_select.getValue());
                    AutoCompleteTextView_location_name.dismissDropDown();
                    AutoCompleteTextView_location_name.clearFocus();
                }

            });
            return convertView;
        }
    }

定义适配器初始化代码:

 CustomAutoCompleteAdapter adapter_house=null;
    CustomAutoCompleteLocationAdapter adapter_location=null;
    List<KeyValueItem> itemList=new ArrayList<KeyValueItem>();
    List<KeyValueItem> itemList_location=new ArrayList<KeyValueItem>();
     private AutoCompleteTextView AutoCompleteTextView_house;
    private AutoCompleteTextView AutoCompleteTextView_location_name;
AutoCompleteTextView_house = (AutoCompleteTextView) this.findViewById(R.id.AutoCompleteTextView_house);
        AutoCompleteTextView_location_name = (AutoCompleteTextView) this.findViewById(R.id.AutoCompleteTextView_location_name);
  KeyValueItem keyValueItem1=new KeyValueItem("001","仓库1");
        KeyValueItem keyValueItem2=new KeyValueItem("002","仓库2");
        KeyValueItem keyValueItem3=new KeyValueItem("003","仓库3");

        itemList.add(keyValueItem1);
        itemList.add(keyValueItem2);
        itemList.add(keyValueItem3);

        http_GetWarehouse(OrganizationNo);
//       ArrayAdapter<KeyValueItem> arrayAdapter_house2 = new ArrayAdapter<KeyValueItem>(this,
//                R.layout.list_item_layout, itemList );
//        AutoCompleteTextView_house.setAdapter(arrayAdapter_house2);

         adapter_house = new CustomAutoCompleteAdapter(this, R.layout.list_item_layout, itemList);
        AutoCompleteTextView_house.setAdapter(adapter_house);
 ImageButton button_house = (ImageButton) this.findViewById(R.id.ImageButton_house);
        button_house.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 显示下拉框
                AutoCompleteTextView_house.showDropDown();


                //Log.d("data1", "value:"+AutoCompleteTextView_house.getText().toString());
            }
        });

        AutoCompleteTextView_house.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

               // Log.d("data1", "2value:"+AutoCompleteTextView_house.getText().toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

//                Log.d("data1", "3value:"+AutoCompleteTextView_house.getText().toString());
//
//                if(adapter2.KeyValueItem_select!=null){
//                    String key= adapter2.KeyValueItem_select.getKey();
//                    Log.d("data1", "3key:"+key);
//                }

            }

            @Override
            public void afterTextChanged(Editable s) {

               // AutoCompleteTextView_house.clearFocus();
                Log.d("data1", "4value:"+AutoCompleteTextView_house.getText().toString());

                if(adapter_house.KeyValueItem_select!=null){
                    String key= adapter_house.KeyValueItem_select.getKey();
                    String value= adapter_house.KeyValueItem_select.getValue();

                    if(key!=null&&key!=""&&key.length()>0){
                        Log.d("data1", "4key:"+key+"-"+value);

                        http_GetLocation(key);
                        warehouse_no2=key;
                        warehouse_name2=value;

                    }else {
                        itemList_location.clear();
                        warehouse_no2="";
                        warehouse_name2="";
                        adapter_location.notifyDataSetChanged();
                    }

                }
            }
        });




        ImageButton button_location_name = (ImageButton) this.findViewById(R.id.ImageButton_location_name);
        button_location_name.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 显示下拉框
                AutoCompleteTextView_location_name.showDropDown();
            }
        });



        KeyValueItem keyValueItem4=new KeyValueItem("001","仓位1");
        KeyValueItem keyValueItem5=new KeyValueItem("002","仓位2");
        KeyValueItem keyValueItem6=new KeyValueItem("003","仓位3");

        itemList_location.add(keyValueItem4);
        itemList_location.add(keyValueItem5);
        itemList_location.add(keyValueItem6);


         adapter_location = new CustomAutoCompleteLocationAdapter(this, R.layout.list_item_layout, itemList_location);
        AutoCompleteTextView_location_name.setAdapter(adapter_location);


        AutoCompleteTextView_location_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // Log.d("data1", "2value:"+AutoCompleteTextView_house.getText().toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

//                Log.d("data1", "3value:"+AutoCompleteTextView_house.getText().toString());
//
//                if(adapter2.KeyValueItem_select!=null){
//                    String key= adapter2.KeyValueItem_select.getKey();
//                    Log.d("data1", "3key:"+key);
//                }

            }

            @Override
            public void afterTextChanged(Editable s) {

               // AutoCompleteTextView_location_name.clearFocus();
                Log.d("data1", "4仓位value:"+AutoCompleteTextView_location_name.getText().toString());

                if(adapter_location.KeyValueItem_select!=null){
                    String key= adapter_location.KeyValueItem_select.getKey();
                    String value= adapter_location.KeyValueItem_select.getValue();

                    if(key!=null&&key!=""&&key.length()>0){

                        location_no2=key;
                        location_name2=value;
                        Log.d("data1", "4仓位key:"+key+"-"+value);
                    }else {
                        location_no2="";
                        location_name2="";
                    }

                }
            }
        });

适配器代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  >


    <TextView
        android:id="@+id/TextView_value"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/textview_right_border_simple"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/TextView_Key"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="@drawable/textview_right_border_simple"
        android:textAlignment="center"
        android:textColor="#005500"
        android:textSize="12sp" />

</LinearLayout>

绑定数据实体类

public class KeyValueItem {

    private String key;
    private String value;
    public KeyValueItem() {

    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public KeyValueItem(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {


        return value; // AutoCompleteTextView显示value
    }
}

以上就是绑定下拉列表数据代码


原文地址:https://blog.csdn.net/baidu_38995168/article/details/142360405

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!