适配器中的AutoCompleteTextView无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适配器中的AutoCompleteTextView无法正常工作相关的知识,希望对你有一定的参考价值。

我有一个listlayout列表视图,其中包含autocompletextview和删除图标。主布局中有一个加btn和listview。当我们点击加号btn时,会动态地将新的rowlayout添加到listview。单击删除图标,我们可以删除该位置的rowlayout。 autocompletetextview显示了一些建议(我在listview适配器中使用了字符串数组和ArrayAdapter),当我选择其中任何一个建议时,它将设置为autocompletetextview。我的问题是每次我从listview添加/删除项目时,前一个autocompletetextview中显示的值被删除,以便它显示第一个字符串数组

// Row Layout

<?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"
    android:gravity="center">

    <AutoCompleteTextView
        android:id="@+id/myEt"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/delImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:src="@android:drawable/ic_delete"/>



</LinearLayout>
public class MainActivity extends AppCompatActivity {

    Button openBtn;
    ArrayList<Model> listItems=new ArrayList<Model>();
    ContactsListAdapter adapter;
    int resource;
    NonScrollListView list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        openBtn = (Button) findViewById(R.id.openBtn);

        list = (NonScrollListView)findViewById(R.id.list);
        adapter = new ContactsListAdapter(this, resource, listItems);
        list.setAdapter(adapter);

        openBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addItems();
            }
        });


    }

    public void addItems() {
        Model model = new Model();
        model.setCount(listItems.size()+1);
        listItems.add(model);
        adapter.notifyDataSetChanged();
    }




}
public class ContactsListAdapter extends ArrayAdapter<Model> {

    private Context mContext;
    int resource;
    private ArrayList<Model> arrayList = new ArrayList<Model>();
    String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
    ArrayAdapter<String> adapter;

    public ContactsListAdapter(Context mContext, int resource, ArrayList<Model> arrayList) {
        super(mContext, resource, arrayList);
        this.resource = resource;
        this.mContext = mContext;
        this.arrayList = arrayList;
        adapter = new ArrayAdapter<String>(mContext, android.R.layout.select_dialog_item, fruits);

    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
        View v = convertView;
        final ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

            v = inflater.inflate(R.layout.contact_item, parent, false);

            holder.myEt  = (AutoCompleteTextView)v.findViewById(R.id.myEt);
            holder.delImg = (ImageView)v.findViewById(R.id.delImg);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.myEt.setThreshold(3);
        holder.myEt.setAdapter(adapter);

        final Model item = arrayList.get(position);

        holder.myEt.setText(item.getName());



        holder.myEt.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                arrayList.get(position).setName(fruits[pos]);

            }
        });

        /*holder.myEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                *//*item.setName(s.toString());
                notifyDataSetChanged();*//*

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setName(s.toString());
                //notifyDataSetChanged();
            }
        });*/


        holder.delImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                arrayList.remove(position);
                notifyDataSetChanged();
            }
        });



        return v;
    }

    class ViewHolder {

        AutoCompleteTextView myEt;
        ImageView delImg;
    }

}

.

答案

尝试在arrayList.size()方法中返回getCount()

    @Override
    public int getCount() {
        return arrayList.size();
    }

class ViewHolder是Adapter的内部类,把它放在ContactsListAdapter类中

另一答案

自从我得到解决后,我就分享了我的答案。我更改了AutoCompleteTextView的setOnItemClickListener()方法。完整代码如下。

public class ContactsListAdapter extends ArrayAdapter<Model> {

    private Context mContext;
    int resource;
    private ArrayList<Model> arrayList = new ArrayList<Model>();
    String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
    ArrayAdapter<String> adapter;

    public ContactsListAdapter(Context mContext, int resource, ArrayList<Model> arrayList) {
        super(mContext, resource, arrayList);
        this.resource = resource;
        this.mContext = mContext;
        this.arrayList = arrayList;
        adapter = new ArrayAdapter<String>(mContext, android.R.layout.select_dialog_item, fruits);

    }

    @Nullable
    @Override
    public Model getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
        View v = convertView;
        final ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

            v = inflater.inflate(R.layout.contact_item, parent, false);

            holder.myEt  = (AutoCompleteTextView)v.findViewById(R.id.myEt);
            holder.delImg = (ImageView)v.findViewById(R.id.delImg);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.myEt.setThreshold(3);
        holder.myEt.setAdapter(adapter);
        holder.myEt.setTag(position);

        final Model item = arrayList.get(position);

        holder.myEt.setText(item.getName());

        System.out.println("sammy_name: "+item.getName() + " position: "+position);



        holder.myEt.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                String value = parent.getAdapter().getItem(pos).toString();
                Model model = getItem(position);

                if (model != null) {
                    //model.setName(fruits[pos]);
                    model.setName(value);
                }
                System.out.println("sammy_name1: "+item.getName());

            }
        });


        holder.delImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                arrayList.remove(position);
                notifyDataSetChanged();
            }
        });



        return v;
    }

    class ViewHolder {
        ArrayAdapter<String> adapter;
        AutoCompleteTextView myEt;
        ImageView delImg;
    }

}

以上是关于适配器中的AutoCompleteTextView无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

动态更新 AutoCompleteTextView 适配器

使用 Async Task 读取电话联系人并将其设置为 AutoCompleteTextView 的适配器

使用Async Task读取手机通讯录并将其设置为AutoCompleteTextView的适配器

将其值设置为“”后无法过滤 AutoCompleteTextView

IllegalStateException - 使用 AutocompleteTextView 支持 LoaderManager

Arraylist 中有 1000 个条目的 AutocompleteTextView 不起作用