如何在AutoCompleteTextView中填充数据?

3

我需要在AutoCompleteTextView中显示两个部分(类似于这样):

enter image description here

我创建了一个自定义布局,其中包含两个CardViews,每个CardView都有三个TextViews。目前,我没有根据type来分配部分。整个数据都加载到一个部分中。

活动

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

自动完成位置适配器

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类中的数据。

我需要怎么做才能按照给定的图片对搜索结果进行分类?ArrayAdapter肯定不适用于这里。

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

任何提示都将不胜感激。


请查看这个链接 - Nilesh Deokar
@ndeokar 这会如何帮助我呢? - Amit Pal
我有排序功能,所以认为它会有帮助。 - Nilesh Deokar
实际上并不是这样的。您能否解释一下呢?也许我没有理解清楚。 - Amit Pal
实际上我正在旅行中,所以我会在到家后尝试一下,然后在这里发送源代码!如果你在此期间发现了什么,请在这里发布答案。谢谢! - Nilesh Deokar
1个回答

1
可能的临时解决方案是使用 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接