D
D
DeadRayder2015-07-19 17:06:09
Android
DeadRayder, 2015-07-19 17:06:09

How to load data in AutoCompleteTextView?

Hello, I again ran into the problem of adapters. The bottom line is that when you run a specific fragment in the AutoCompleteTextView, data must be loaded in order to be able to find a specific field from the database. I wrote my own ArrayAdapter and with its help I try to put all the information I need into AutoComplete. Here is the code:

view = inflater.inflate(R.layout.fragment, null);

            ArrayList<Products> prodCollectionForSearch = new ArrayList<>();

            //prodCollectionForSearch = Query(SelectCityScreen.mCity);

            ResultSet rs1;
            try {
                Statement statement = MainActivity.connect.createStatement();
                rs1 = statement.executeQuery("select top 10 *\n" +
                        "from EXPORT_TABLE\n" +
                        "where City_name = 'Москва' AND Quantity = 1");

                while (rs1.next())
                {
                    prodCollectionForSearch.add(new Products(rs1.getString("Product_name")));
                }

                ArrayAdapterProduct prodAdapter = new ArrayAdapterProduct(getActivity(),prodCollectionForSearch);
                mAutoCompleteSearchProduct = (AutoCompleteTextView) view.findViewById(R.id.search_product_autoCompl);
                mAutoCompleteSearchProduct.setAdapter(prodAdapter);

            } catch (SQLException e) {
                e.printStackTrace();
            }

As I understand it, the adapter fills in AutoComplete, but if you enter something into it, then the add-on does not prompt anything and there are no options for selection. Where did I go wrong?
Below are the ArrayAdapterProduct and Products classes
public class ArrayAdapterProduct extends ArrayAdapter<Products> {
    public ArrayAdapterProduct(Context context, ArrayList<Products> mProducts) {
        super(context,0, mProducts);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Products prod = getItem(position);

        if (convertView == null)
        {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.search_item, parent, false);
        }

        TextView mTextViewSearchProducts = (TextView) convertView.findViewById(R.id.txt_search_product);
        mTextViewSearchProducts.setText(prod.product_name);

        return convertView;
    }
}

public class Products {

    public String product_name;

    public Products(String product_name) {

        this.product_name = product_name;
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeadRayder, 2015-07-19
@DeadRayder

In addition, when I tried to work with AutoComplete in MainActivity, I simply passed the same adapter to it, which loaded the data into the ListView from the database, and it immediately started working, but then nothing...

A
anyd3v, 2015-07-19
@anyd3v

developer.android.com/reference/android/widget/Aut...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question