Answer the question
In order to leave comments, you need to log in
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();
}
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
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...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question