如何在 AutoCompleteTextView 中填充数据?

How to populate data in AutoCompleteTextView?

我必须在 AutoCompleteTextView 中显示两个部分(类似这样的内容):

我创建了一个 custom layout,其中有两个 CardViews,每个 CardView 有三个 TextViews。现在我不会根据 type 分发该部分。整个数据被加载到一个部分。

Activity

final AutocompleteLocalityAdapter adapterLocalities = new AutocompleteLocalityAdapter(context,
                 R.layout.support_simple_spinner_dropdown_item, new ArrayList<Locality>());

AutocompleteLocalityAdapter

public class AutocompleteLocalityAdapter extends ArrayAdapter<Locality> {
public AutocompleteLocalityAdapter(Context context, int layout, List<Locality> localities) {
    super(context, layout, localities);
    this.localities = localities;
    updateList("");
}

updateList 方法中,我正在进行新的网络调用以填充 Locality class.

中的数据

如何根据给定图像对搜索结果进行分类? ArrayAdapter 肯定不会在这里工作。

我在这里想到的可能的解决方案是: 将 ArrayAdapter 替换为 RecyclerViewAdapter

任何提示都将不胜感激。

此解决方案可能的权宜之计是 PopUpWindow。在 PopUpWindow 里面我放了两个 RecyclerView 并通过网络调用填充它们。

dashboard_profile_popup_window

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_low"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/locationCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="@dimen/corner_radius"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/landmark"
            android:textStyle="bold" />

        <ListView
            android:id="@+id/localityView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
    android:id="@+id/landmarkCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="@dimen/corner_radius"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/location"
            android:textStyle="bold" />

        <ListView
            android:id="@+id/landmarkView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</android.support.v7.widget.CardView>

自定义控件

public class CustomAutoCompleteView extends EditText {
private Context context;
TextListViewAdapter locationAdapter;
TextListViewAdapter landmarkAdaper;
PopupWindow pwindow;
ClickListener clickListener;

public CustomAutoCompleteView(Context context) {
    super(context);
    this.context = context;
    setCustomization();
}

public void closeWindow(){
    pwindow.dismiss();
}

public CustomAutoCompleteView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    setCustomization();
}

public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    setCustomization();
}

public void updateList(List<LocalityEntity> locationList, List<LocalityEntity> landmarkList) {
    if (pwindow == null) {
        initPopupWindow();
    }
    locationAdapter.updateList(locationList);
    landmarkAdaper.updateList(landmarkList);
}

public void initPopupWindow() {
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.dashboard_profile_popup_window, null);

    ListView landmarkRecyclerView = (ListView) layout.findViewById(R.id.localityView);
    ListView localityRecyclerView = (ListView) layout.findViewById(R.id.landmarkView);
    landmarkRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
            String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
            clickListener.placeSelected(gid);
        }
    });
    localityRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
            String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
            clickListener.placeSelected(gid);
        }
    });
    landmarkRecyclerView.setAdapter(landmarkAdaper);
    localityRecyclerView.setAdapter(locationAdapter);

    pwindow = new PopupWindow(context);
    pwindow.setContentView(layout);
    pwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    pwindow.setWidth(this.getWidth());
    pwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    pwindow.setFocusable(true);
    pwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

        @Override
        public void onDismiss() {
            pwindow = null;
        }

    });
    pwindow.showAsDropDown(this);
}

private void setCustomization() {
    locationAdapter = new TextListViewAdapter(getContext());
    landmarkAdaper = new TextListViewAdapter(getContext());
    initPopupWindow();

}

public void setClickListener(ClickListener clickListener) {
    this.clickListener = clickListener;
}

public interface ClickListener {
    void placeSelected(String gid);
}
}

现在通过以下代码调用这个customViewWidget:

 place_pop_up.setClickListener(this);
 place_pop_up.addTextChangedListener(new TextWatcher() {
 @Override
 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   }

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

    private Timer timer = new Timer();
        private final long DELAY = 2000;

        @Override
        public void afterTextChanged(final Editable s) {
            if (s.length() > 3) {
                timer.cancel();
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        getAutoCompleteSearchResult(s.toString());
                    }
                }, DELAY);
            }
        }
    });

getAutoCompleteSearchResult进行网络调用并调用 place_pop_up.updateList(locality, landmark);