我正在开发存在Sqlite数据库的应用程序。我做了什么以使用list在sqlite数据库上实现搜索,我实现了类似查询,并且我知道了; searchview不在从sqlite中搜索数据...
这是我的Sqlite文件。...
public List<GetSetClientsDetail> SearchClientsData() {
String[] columns = {
fname,
route,
cnic,
lname,
contact
};
String sortOrder = fname + " ASC";
List<GetSetClientsDetail> clientlist = new ArrayList<GetSetClientsDetail>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(
table_poducts_records,
new String[] { fname, route, cnic, lname, contact},
fname + " LIKE '%" + fname + "%'",
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
GetSetClientsDetail clientsDetail = new GetSetClientsDetail();
clientsDetail.setFNAME(cursor.getString(cursor.getColumnIndex(fname)));
clientsDetail.setROUTE(cursor.getString(cursor.getColumnIndex(route)));
clientsDetail.setCNIC(cursor.getString(cursor.getColumnIndex(cnic)));
clientsDetail.setLNAME(cursor.getString(cursor.getColumnIndex(lname)));
clientsDetail.setCONTACT(cursor.getString(cursor.getColumnIndex(contact)));
clientlist.add(clientsDetail);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return clientlist;
}
这是我实现搜索的搜索的浏览量类别。
public class Clients extends Fragment {
RecyclerView recyclerViewClients;
Button btnAll;
SearchView searchViewclient;
ClientRecyclerAdapter clientRecyclerAdapter;
List<GetSetClientsDetail> listclients;
DatabaseHelper databaseHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.clients, container, false);
btnAll=view.findViewById(R.id.searchallclients);
recyclerViewClients=view.findViewById(R.id.recyclerviewallclients);
searchViewclient=view.findViewById(R.id.searchclient);
listclients = new ArrayList<>();
clientRecyclerAdapter = new ClientRecyclerAdapter(listclients,recyclerViewClients);
recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
recyclerViewClients.setItemAnimator(new DefaultItemAnimator()); //multi copy paste!
recyclerViewClients.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewClients.setHasFixedSize(true);
recyclerViewClients.setAdapter(clientRecyclerAdapter);
databaseHelper = new DatabaseHelper(getActivity());
searchViewclient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SearchSQliteClientData();
}
});
btnAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getClientFromSqlite();
}
});
return view;
}
@SuppressLint("StaticFieldLeak")
private void SearchSQliteClientData() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listclients.clear();
listclients.addAll(databaseHelper.SearchClientsData());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
clientRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
@SuppressLint("StaticFieldLeak")
private void getClientFromSqlite() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listclients.clear();
listclients.addAll(databaseHelper.getAllClientsData());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
clientRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
}
我想要做的是在搜索视图上按A键;它将所有与a或如果我按一个字母相关的数据都带入我已实现的列表中。
答案 0 :(得分:2)
例如
更改public List<GetSetClientsDetail> SearchClientsData()
到
public List<GetSetClientsDetail> SearchClientsData(String mysearchstring)
然后更改:-
Cursor cursor = db.query(
table_poducts_records,
new String[] { fname, route, cnic, lname, contact},
fname + " LIKE '%" + fname + "%'",
null, null, null, null, null);
到
String[] whereargs = new String[]{"%" + mysearchstring + "%"}; //<<<<<<<<<< ADDED
Cursor cursor = db.query(
table_poducts_records,
new String[] { fname, route, cnic, lname, contact},
fname + " LIKE ?", //<<<<<<<<<< CHANGED
whereargs, //<<<<<<<<<< ADDED
null, null, null); //<<<<<<<<<< 3 nulls instead of 4 (as whereargs has replaced first null)
作为测试尝试:-
listclients.addAll(databaseHelper.SearchClientsData("A")); //<<<<<<<<<< look for all with A anywhere in the fname column
以下是基于问题代码的工作示例,但为方便起见进行了简化。
核心更改有两个方面,上面的代码位于数据库帮助器 DatabaseHelper.java 中,具体如下:-
也就是说SearchClientData方法是:-
public List<GetSetClientsDetail> SearchClientsData(String mysearchstring) {
String[] columns = {
fname, route, cnic, lname, contact
};
String sortOrder = fname + " ASC";
List<GetSetClientsDetail> clientlist = new ArrayList<GetSetClientsDetail>();
SQLiteDatabase db = this.getReadableDatabase();
String[] whereargs = new String[]{"%" + mysearchstring + "%"}; //<<<<<<<<<< ADDED
Cursor cursor = db.query(
table_poducts_records,
new String[]{fname, route, cnic, lname, contact},
fname + " LIKE ?",
whereargs,
null, null, sortOrder, null
);
if (cursor.moveToFirst()) {
do {
GetSetClientsDetail clientsDetail = new GetSetClientsDetail();
clientsDetail.setFNAME(cursor.getString(cursor.getColumnIndex(fname)));
clientsDetail.setROUTE(cursor.getString(cursor.getColumnIndex(route)));
clientsDetail.setCNIC(cursor.getString(cursor.getColumnIndex(cnic)));
clientsDetail.setLNAME(cursor.getString(cursor.getColumnIndex(lname)));
clientsDetail.setCONTACT(cursor.getString(cursor.getColumnIndex(contact)));
clientlist.add(clientsDetail);
} while (cursor.moveToNext());
}
cursor.close();
return clientlist;
}
public List<GetSetClientsDetail> getAllClientsData() {
return SearchClientsData("");
}
另一个核心变化是,它不依赖于SearchView的 OnClickListener ,这很可能是由于Views Other Listener失去了聚焦能力而被调用的。
SearchView的 setOnQueryTextListener 已被利用。这样可以将文本传递到SearchClientsData。
为方便起见,此示例使用ArrayAdapter和简单的Simple_List_Item1布局,并在主线程和活动上进行工作。
使用的活动代码为:-
public class MainActivity extends AppCompatActivity {
ListView listviewClients;
Button btnAll;
ArrayAdapter mSCA;
SearchView searchViewclient;
List<GetSetClientsDetail> listclients;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAll=this.findViewById(R.id.searchallclients);
listviewClients=this.findViewById(R.id.clients);
searchViewclient=this.findViewById(R.id.searchclient);
databaseHelper = new DatabaseHelper(this);
addSomeData();
manageListView("");
searchViewclient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manageListView("zzz");
}
});
searchViewclient.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
manageListView(newText);
return false;
}
});
btnAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manageListView("");
}
});
}
private void manageListView(String searchArgument) {
listclients = databaseHelper.SearchClientsData(searchArgument);
if (mSCA == null) {
mSCA = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listclients);
listviewClients.setAdapter(mSCA);
} else {
mSCA.clear();
mSCA.addAll(listclients);
mSCA.notifyDataSetChanged();
}
}
private void addSomeData() {
databaseHelper.add("Albert","001","123456789","Smith","someone");
databaseHelper.add("Freda","001","123456789","Jones","someone");
databaseHelper.add("Mike","002","0987654321","Allenby","him");
}
/*
private void SearchSQliteClientData() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listclients.clear();
listclients.addAll(databaseHelper.SearchClientsData());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
clientRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
private void getClientFromSqlite() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listclients.clear();
listclients.addAll(databaseHelper.getAllClientsData());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
clientRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
*/
}
第一次运行时(不是真正的第一次运行,而是经过多次运行之后才重新开始,因此有大量数据):-
键入 a 或 A ,所有Mike消失了
,依此类推,该列表会根据在搜索字段中输入的文本立即进行调整。单击全部按钮将重新显示全部。