0

I am facing an issue of autocompletetextview dropdown not showing when searching dynamically from Sqlite database. Below is my code. Any help would be grateful. Thanks

activity_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".List">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="vertical"
        android:paddingTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done"
            android:textColor="@color/purple_500"
            android:layout_gravity="right"
            android:layout_marginEnd="20dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="20dp">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_weight="1"
                android:src="@drawable/bullets" />

            <AutoCompleteTextView
                android:id="@+id/actvItems"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:hint="Add your item here"
                android:layout_weight="5"
                android:textColor="@color/black" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:hint="Quantity"
                android:layout_weight="3"
                android:textColor="@color/black"
                android:background="@drawable/rounded_black_stroke"
                android:padding="10dp"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="x3"
                android:gravity="center"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.3"
        android:orientation="horizontal"
        android:paddingTop="30dp"
        android:background="@drawable/bottom_nav_bg">

        <LinearLayout
            android:id="@+id/icon_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="list"
            android:orientation="vertical">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/icon_list"
                app:tint="@color/purple_500" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MY LIST"
                android:textColor="@color/purple_500"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/icon_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="home"
            android:orientation="vertical">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/icon_home2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="HOME" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/icon_profile"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="profile"
            android:orientation="vertical">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/icon_person" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MY PROFILE" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

items_drop_down dropdown layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="15dp"
    android:background="#F9F6FF">

    <TextView
        android:id="@+id/tvBrand"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Product Brand"
        android:textAllCaps="true"
        android:textSize="12sp"/>

    <TextView
        android:id="@+id/tvItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Product Name"
        android:textSize="18sp"
        android:textColor="@color/black"/>

</LinearLayout>

itemsList Model class

public class ItemsList {
    int item_id;
    String item, brand;

    public ItemsList() {
        this.item_id = item_id;
        this.item = item;
        this.brand = brand;
    }

    public int getItem_id() {
        return item_id;
    }

    public void setItem_id(int item_id) {
        this.item_id = item_id;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
}

Sqlite Database class AnnapurnaDB

public class AnnapurnaDB {
    private static final String db_name = "annapurnadb";
    private static final String tbl_items = "items";
    private static final String tbl_user_items = "user_items";
    private static final String tbl_user_items_tmp = "user_items_tmp";
    private static final String tbl_user_items_inventory = "user_items_inventory";
    private static final int db_version = 1;

    public static final String item_id = "item_id";
    public static final String brand = "brand";
    public static final String item_desc = "item_desc";
    public static final String item_size = "item_size";
    public static final String item_type = "item_type";
    public static final String weight = "weight";
    public static final String mrp = "mrp";
    public static final String item = "item";

    public static final String user_id = "user_id";
    public static final String avg_days = "avg_days";
    public static final String avg_quantity = "avg_quantity";
    public static final String status = "status";
    public static final String purchase_date = "purchase_date";
    public static final String date_added = "date_added";

    public static final String unit = "unit";
    public static final String quantity = "quantity";
    public static final String manufacturing_date = "manufacturing_date";
    public static final String expiry_date = "expiry_date";
    public static final String exhaust_date = "exhaust_date";

    private DBHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    public AnnapurnaDB(Context context) {
        this.ourContext = context;
    }

    private class DBHelper extends SQLiteOpenHelper{

        public DBHelper(Context context){
            super(context, db_name, null, db_version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sqlCreateItems = "CREATE TABLE " + tbl_items + " (" +
                    item_id + " INTEGER, " +
                    brand + " TEXT NOT NULL, " +
                    item_desc + " TEXT NOT NULL, " +
                    item_size + " TEXT DEFAULT NULL, " +
                    item_type + " TEXT DEFAULT NULL, " +
                    weight + " TEXT DEFAULT NULL, " +
                    mrp + " TEXT DEFAULT NULL, " +
                    item + " TEXT DEFAULT NULL);";

            String sqlCreateUserItems = "CREATE TABLE " + tbl_user_items + " (" +
                    user_id + " INTEGER NOT NULL, " +
                    item_id + " INTEGER NOT NULL, " +
                    avg_days + " INTEGER DEFAULT NULL, " +
                    avg_quantity + " INTEGER DEFAULT NULL, " +
                    status + " TEXT DEFAULT NULL, " +
                    purchase_date + " DATE DEFAULT NULL, " +
                    date_added + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP);";

            String sqlCreateUserItemsTmp = "CREATE TABLE " + tbl_user_items_tmp + " (" +
                    item_id + " INTEGER NOT NULL, " +
                    avg_days + " INTEGER DEFAULT NULL, " +
                    avg_quantity + " INTEGER DEFAULT NULL, " +
                    status + " TEXT DEFAULT NULL, " +
                    purchase_date + " DATE DEFAULT NULL, " +
                    date_added + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP);";

            String sqlCreateUserItemsInventory = "CREATE TABLE " + tbl_user_items_inventory + " (" +
                    user_id + " INTEGER NOT NULL, " +
                    item_id + " INTEGER NOT NULL, " +
                    unit + " TEXT DEFAULT NULL, " +
                    quantity + " TEXT DEFAULT NULL, " +
                    manufacturing_date + " TEXT DEFAULT NULL, " +
                    expiry_date + " TEXT DEFAULT NULL, " +
                    purchase_date + " DATE DEFAULT NULL, " +
                    exhaust_date + " DATE DEFAULT NULL);";

            sqLiteDatabase.execSQL(sqlCreateItems);
            sqLiteDatabase.execSQL(sqlCreateUserItems);
            sqLiteDatabase.execSQL(sqlCreateUserItemsTmp);
            sqLiteDatabase.execSQL(sqlCreateUserItemsInventory);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }

    public AnnapurnaDB open() throws SQLException {
        ourHelper = new DBHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }

    public void insertItems(String item_id_v, String brand_v, String item_desc_v, String item_size_v, String item_type_v, String weight_v, String mrp_v, String item_v){
        ContentValues contentValues = new ContentValues();
        contentValues.put(item_id, item_id_v);
        contentValues.put(brand, brand_v);
        contentValues.put(item_desc, item_desc_v);
        contentValues.put(item_size, item_size_v);
        contentValues.put(item_type, item_type_v);
        contentValues.put(weight, weight_v);
        contentValues.put(mrp, mrp_v);
        contentValues.put(item, item_v);

        ourDatabase.insert(tbl_items, null, contentValues);
    }

    public Cursor getAllItems(){
        return ourDatabase.rawQuery("SELECT * FROM " + tbl_items, null);
    }

    public List<ItemsList> search(String keyword){
        List<ItemsList> itemsLists = null;
        Cursor cursor = ourDatabase.rawQuery("SELECT item_id, brand, item FROM " + tbl_items + " WHERE  " + item + " LIKE ?", new String[] {"%" + keyword + "%"} );
        if(cursor.moveToFirst()){
            itemsLists = new ArrayList<ItemsList>();
            do{
                ItemsList itemsList = new ItemsList();
                itemsList.setItem_id(cursor.getInt(0));
                itemsList.setBrand(cursor.getString(1));
                itemsList.setItem(cursor.getString(2));
            }while (cursor.moveToNext());
        }
        return itemsLists;
    }
}

ItemSearchAdapter

public class ItemSearchAdapter extends ArrayAdapter<ItemsList> {
    private Context context;
    private int LIMIT = 5;
    private List<ItemsList> itemsLists;

    public ItemSearchAdapter(Context context, List<ItemsList> itemsLists){
        super(context, R.layout.items_drop_down, itemsLists);
        this.context = context;
        this.itemsLists = itemsLists;
    }

    @Override
    public  int getCount(){
        return Math.min(LIMIT, itemsLists.size());
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.items_drop_down, null);
        ItemsList itemsList = itemsLists.get(position);
        TextView tvBrand = view.findViewById(R.id.tvBrand);
        TextView tvItem = view.findViewById(R.id.tvItem);

        tvBrand.setText(itemsList.getBrand());
        tvItem.setText(itemsList.getItem());

        return view;
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return new ItemFilter(this, context);
    }

    private  class ItemFilter extends Filter {

        private ItemSearchAdapter itemSearchAdapter;
        private Context context;

        public ItemFilter(ItemSearchAdapter itemSearchAdapter, Context context){
            super();
            this.itemSearchAdapter = itemSearchAdapter;
            this.context = context;
        }

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            itemSearchAdapter.itemsLists.clear();
            FilterResults filterResults = new FilterResults();
            if(charSequence == null || charSequence.length() == 0){
                filterResults.values = new ArrayList<ItemsList>();
                filterResults.count = 0;
            }else{
                AnnapurnaDB annapurnaDB = new AnnapurnaDB(context);
                annapurnaDB.open();
                List<ItemsList> itemsLists = annapurnaDB.search(charSequence.toString());
                filterResults.values = itemsLists;
                filterResults.count = itemsLists.size();
                annapurnaDB.close();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            itemSearchAdapter.itemsLists.clear();
            if(filterResults.values != null && filterResults.count > 0) {
                itemSearchAdapter.itemsLists.addAll((List<ItemsList>) filterResults.values);
                itemSearchAdapter.notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

        @Override
        public CharSequence convertResultToString(Object resultValue) {
            ItemsList itemsList = (ItemsList) resultValue;
            return itemsList.getItem();
        }
    }
}

Item List Activity

public class List extends AppCompatActivity {

    AutoCompleteTextView actvItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        actvItems = findViewById(R.id.actvItems);

        java.util.List<ItemsList> itemsLists = new ArrayList<ItemsList>();
        ItemSearchAdapter itemSearchAdapter = new ItemSearchAdapter(getApplicationContext(), itemsLists);

        actvItems.setThreshold(1);
        actvItems.setAdapter(itemSearchAdapter);
    }

    public void list(View view) {
        startActivity(new Intent(List.this, List.class));
    }

    public void home(View view) {
        startActivity(new Intent(List.this, Home.class));
    }

    public void profile(View view) {
        startActivity(new Intent(List.this, MyProfile.class));
    }
}
Tariq
  • 21
  • 1
  • 1
  • 8
  • https://stackoverflow.com/questions/16782288/autocompletetextview-with-custom-adapter-and-filter – Taa Lee Mar 12 '22 at 23:34
  • I couldn’t find your mistake as this is so much code but recheck your adapter. I hope this answer your question – Taa Lee Mar 12 '22 at 23:37

0 Answers0