3

I have to show two section in AutoCompleteTextView (Something like this):

enter image description here

I have created a custom layout which have two CardViews and each CardView have three TextViews. Right now I am not distributing the section on the basis of type. The whole data is loaded into one section.

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("");
}

In updateList method I am making a new network call to fill the data in Locality class.

What do I need to do to categories the search result as per given image? ArrayAdapter is not going to work here for sure.

The possible solution I am thinking here is: Replace ArrayAdapter to RecyclerViewAdapter.

Any hint will be appreciable.

Amit Pal
  • 10,604
  • 26
  • 80
  • 160

1 Answers1

1

The possible makeshift to this solution is PopUpWindow. Inside the PopUpWindow I put two RecyclerView and populate them through network calls.

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

Now call this customViewWidget through following code:

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

In getAutoCompleteSearchResult make the network call and call place_pop_up.updateList(locality, landmark);

Community
  • 1
  • 1
Amit Pal
  • 10,604
  • 26
  • 80
  • 160