Blog purpose for android basic example for android app developer. any query please my contact

Tuesday, 16 February 2021

android autocompletetextview custom adapter example

 android autocompletetextview custom adapter example

In this Article Today Learn android autocompletetextview custom adapter. follow the Full Source Code android autocompletetextview custom adapter.


1.Autcompletetex.java:

package com.akash.myapplication.autocomplete;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AutoCompleteTextView;

import com.akash.myapplication.R;

import java.util.ArrayList;

import java.util.List;


public class Autcompletetex extends AppCompatActivity {

    AutoCompleteTextView txtSearch;

    List<People> mList;

    PeopleAdapter adapter;

    private People selectedPerson;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_autcompletetex);

        mList = retrievePeople();

        txtSearch = (AutoCompleteTextView) findViewById(R.id.auto_name);

        adapter = new PeopleAdapter(this, R.layout.activity_main, R.id.lbl_name, mList);

        txtSearch.setAdapter(adapter);

        txtSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {

                //this is the way to find selected object/item

                selectedPerson = (People) adapterView.getItemAtPosition(pos);

            }

        });

    }

    private List<People> retrievePeople() {

        List<People> list = new ArrayList<People>();

        list.add(new People("Akash", "Mali", 1));

        list.add(new People("Vijay", "Chougule", 2));

        list.add(new People("Prakash", "Mali", 3));

        list.add(new People("Nagaraj", "Dhanavade", 4));

        list.add(new People("Ravi", "Patil", 5));

        list.add(new People("Keta", "Phadke", 6));

        list.add(new People("Satish", "Mali", 7));

        return list;

    }

}


2.People.java:

package com.akash.myapplication.autocomplete;


public class People {

    private String name, lastName;

    private int id;

    public People(String name, String lastName, int id) {

        this.name = name;

        this.lastName = lastName;

        this.id = id;

    }

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getlastName() {

        return lastName;

    }

    public void setlastName(String lastName) {

        this.lastName = lastName;

    }

}


3.PeopleAdapter.java:

package com.akash.myapplication.autocomplete;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.akash.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class PeopleAdapter extends ArrayAdapter<People> {

    Context context;
    int resource, textViewResourceId;
    List<People> items, tempItems, suggestions;

    public PeopleAdapter(Context context, int resource, int textViewResourceId, List<People> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<People>(items); // this makes the difference.
        suggestions = new ArrayList<People>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row, parent, false);
        }
        People people = items.get(position);
        if (people != null) {
            TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
            if (lblName != null)
                lblName.setText(people.getName());
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((People) resultValue).getName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (People people : tempItems) {
                    if (people.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(people);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<People> filterList = (ArrayList<People>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (People people : filterList) {
                    add(people);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

4.activity_autcompletetex.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoCompleteTextView
        android:id="@+id/auto_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="2"
        android:hint="@string/hint_enter_name" />
</LinearLayout>

5.row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lbl_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="16dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

6.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".autocomplete.Autcompletetex">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

7.Output:


android autocompletetextview custom adapter


android autocompletetextview custom adapter






No comments:

Post a Comment