如何在AutoCompleteTextView中填充数据?

时间:2016-05-10 12:28:59

标签: android android-layout android-recyclerview android-adapter autocompletetextview

我必须在AutoCompleteTextView中展示两个部分(像这样的东西):

enter image description here

我创建的custom layout有两个CardViews,每个CardView有三个TextViews。现在我不是在type的基础上分发该部分。整个数据加载到一个部分。

活动

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

根据给定的图像,我需要对搜索结果进行分类? ArrayAdapter肯定不会在这里工作。

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

任何提示都会很明显。

1 个答案:

答案 0 :(得分: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>

CustomWidget

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);