简体   繁体   中英

How to have a AutoCompleteTextView in each item of a ListView

I'v been trying to do this for a while but cant seem to do it. I apply the adapter to the object but when I enter characters into the View, still nothing comes up.

public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();

        view = mInflater.inflate(R.layout.list_item, parent, false);

        holder.nameTxt = (AutoCompleteTextView) view.findViewById(R.id.name_txt);

        String[] drinkArray = activity.getResources().getStringArray(R.array.drink_array);
        holder.adapter = new ArrayAdapter<>(activity, android.R.layout.simple_list_item_1, drinkArray);
        holder.nameTxt.setAdapter(holder.adapter);

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

    if (!itemExists(position)) return view;

    ListItem item = getItem(position);
    holder.nameTxt.setText(item.name);

    holder.nameTxt.setAdapter(holder.adapter);

    holder.nameTxt.setTag(position);

    holder.nameTxt.setOnFocusChangeListener(this);

    return view;
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) return;
    int position;
    ListItem item;
    switch (v.getId()) {
        case R.id.name_txt:
            position = (int) v.getTag();
            if (!itemExists(position)) return;
            item = getItem(position);
            AutoCompleteTextView txtView = (AutoCompleteTextView) v;
            item.name = txtView.getText().toString();
            break;
        default:
            Log.e(TAG, "Unknown onFocusChange View ID: " + v.getId());
            break;
    }
}

public class ViewHolder {
    public AutoCompleteTextView nameTxt;
    public ArrayAdapter<String> adapter;
}

public class ListItem {
    public String name;

    public ListItem() {
        name = "";
    }
}

Ok so this code above is what i have so far, this can perfectly save and load the data from the textview.

I think you have an error in your method getView else. you can try the following change

holder=(viewHolder) View.getTag();

by

holder = view;

then out of this type else

ViewHolder item = (ViewHolder) holder.getTag();
...

I found the way of doing this task. I put a normal TextView in the ListView (ListView1) and then created another ListView (ListView2) on top of the ListView1. Then depending on what item the user clicked in ListView1, I passed the item number (position) and the text within the TextViewer the user clicked on to ListView2 where it could set the top margin of itself and filter its results. Setting the top margin would move it under the TextView the user clicked on. If anyone would like code, just comment and ill update the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM