Issue
Okay so I made module for searching medicines and display items in a listview, I am using JDBC here. The main issue is when I place an underscore_
in the searchview, It kind of makes a bug where the listview items duplicate themselves.
Based on my observation; this bug only occurs when the underscore is inserted simultaneously, but if pressed at slower interval, It doesn't duplicate items anyway.
Here is what it looks without anything on the SearchView:
This is when underscores are placed at slower interval
And even if underscores doesn't exist in the listview it still displays it, but when I add even more underscores, they started to disappear one by one starting from the bottom.
And here's the main problem; when Underscores are placed simultaneously
Here are my code for the search in AsyncTask
private class searchUnfiltered extends AsyncTask<String, String, String> {
String z = "";
String fromSearchView = sv.getQuery().toString().trim();
boolean isSuccess = false;
String about;
@Override
protected void onPreExecute() {
myArrayList2.clear();
myArrayList.clear();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Please check your internet connection";
} else {
String querySearch = "select name from medicines where name like '%"+ fromSearchView +"%' order by name ASC";
Statement stmt1 = con.createStatement();
ResultSet rs1 = stmt1.executeQuery(querySearch);
if(rs1 != null) {
while (rs1.next()) {
myArrayList2.add(rs1.getString("name"));
}
}
}
} catch (Exception ex) {
isSuccess = false;
z = "Exceptions" + ex;
}
return z;
}
@Override
protected void onPostExecute(String s) {
if(!isSuccess && !z.equals("")) {
Toast.makeText(getBaseContext(), z, Toast.LENGTH_LONG).show();
}
ListAdapter listAdapter = new ArrayAdapter<>(MedicineSearch.this, android.R.layout.simple_list_item_1, myArrayList2);
lv.setAdapter(listAdapter);
}
}
Whenever value is changed in search view the searchUnfiltered
will be triggered, please tell me if what I'm doing is efficient or if you have any better suggestions, it would be very helpful.
Solution
That is a race condition reason you are clearing the data onPreExecute then doInBackground is working typing 5 _ at a fast pace will clear 5 times before you get any results back.
as a Solution move:
myArrayList2.clear();// if this didn't work try myArrayList2 = new ArrayList<>();
from onPreExecute to doInBackground.
Answered By - Mohammad Tabbara
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.