簡體   English   中英

AutoCompleteTextView 建議無法更新?

[英]AutoCompleteTextView suggestions can't be updated?

我正在嘗試根據在另一個視圖(微調器)中選擇的選項更改 AutoCompleteTextView 的建議列表。 不幸的是,似乎沒有從另一個視圖的 setOnClickListener 方法中通知此 TextView 的適配器。 我認為這可能是由於上下文的差異而發生的? 如果是這樣,我應該如何解決它?


public class AutoCompleteGOTPointAdapter extends ArrayAdapter<GOTPoint> {

    Context mContext;
    List<GOTPoint> GOTPointListAll; 

    public AutoCompleteGOTPointAdapter(@NonNull Context context, @NonNull List<GOTPoint> GOTPointList) {
        super(context, 0, GOTPointList);
        this.mContext = context;
        this.GOTPointListAll = new ArrayList<>(GOTPointList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        if (convertView == null) { 
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line_spinner,parent,false); 
        }

        TextView textViewGOTPointName = convertView.findViewById(R.id.my_spinner);

        GOTPoint GOTPoint = this.getItem(position);

        if (GOTPoint != null) {
            textViewGOTPointName.setText(GOTPoint.getName());
        }

        return convertView;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            public String convertResultToString(Object resultValue) {
                return ((GOTPoint) resultValue).getName();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                List<GOTPoint> suggestion = new ArrayList<>();

                if (constraint == null || constraint.length() == 0) { //
                    suggestion.addAll(GOTPointListAll);
                }
                else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    for (GOTPoint gotPoint : GOTPointListAll) {
                        if (gotPoint.getName().toLowerCase().contains(filterPattern)) { 
                            suggestion.add(gotPoint);
                        }
                    }
                }

                filterResults.values = suggestion;
                filterResults.count = suggestion.size();

                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                clear();
                addAll((List) results.values);
                notifyDataSetChanged();
            }
        };
    }
}

public class SearchActivity extends AppCompatActivity {
    Spinner spnMountainChains;
    AutoCompleteTextView edtEndPoint;
    AutoCompleteGOTPointAdapter GOTPointAdapter;
    List<GOTPoint> GOTPoints;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        spnMountainChains = findViewById(R.id.spn_mountain_chains);
        edtEndPoint = findViewById(R.id.actv_end_point);
        GOTPoints = new ArrayList<GOTPoint>();
        GOTPointAdapter = new AutoCompleteGOTPointAdapter( SearchActivity.this,GOTPoints);
        edtEndPoint.setAdapter(GOTPointAdapter);

        spnMountainChains.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                MountainChain mountainChain = (MountainChain) spnMountainChains.getSelectedItem();

                List<GOTPoint> filteredGOTPoints = dao.getGOTPoints(mountainChain.getId());

                GOTPointAdapter.clear();
                GOTPointAdapter.addAll(filteredGOTPoints);

//                GOTPoints.clear();            // Also doesn't work
//                GOTPoints.addAll(filteredGOTPoints); 
//                GotPointAdapter.notifyDataSetChanged();
            }

    }
}

one_line_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:textSize="21sp">
</TextView>

好的,感謝@MikeM。 評論我設法得到了解決方案。 初始化后問題確實沒有改變GOTPointsListAll 要在從微調器中選擇項目時更改它,我必須做幾件事:

  • GOTPointList屬性添加到適配器 class 並在構造函數中對其進行初始化:

     Context mContext; List<GOTPoint> GOTPointListAll; List<GOTPoint> GOTPointList; public AutoCompleteGOTPointAdapter(@NonNull Context context, @NonNull List<GOTPoint> GOTPointList) { super(context, 0, GOTPointList); this.mContext = context; this.GOTPointList = GOTPointLi this.GOTPointListAll = new ArrayList<>(GOTPointList); }
    • 覆蓋我的適配器的notifyDataSetChanged方法:
     @Override public void notifyDataSetChanged() { GOTPointListAll.clear(); GOTPointListAll.addAll(GOTPointList); notifyDataSetChangedSuper(); }

在哪里

  • notifyDataSetChangedSuper是:

     private void notifyDataSetChangedSuper() { super.notifyDataSetChanged(); }

我可能應該將GOTPoinListAll上的操作放到另一個 function 上,而不是覆蓋現有的,但這是一個細節。 擁有這兩個功能的原因是GOTPointListAll在選擇微調器項時才需要更新,而GOTPointList需要始終在過濾完成時更新。 這就是為什么:

  • publishResults()部分:
    clear();
    addAll((List) results.values);
    notifyDataSetChangedSuper();

應改為:

    GOTPointList.clear();
    GOTPointList.addAll((List) results.values);
    notifyDataSetChangedSuper();
  • 最后在onItemSelected()
    GOTPoints.clear();
    GOTPoints.addAll(filteredGOTPoints);
    GOTPointAdapter.notifyDataSetChanged();

感謝幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM