不使用SQLite显示任何内容的SearchView

时间:2019-05-23 06:41:41

标签: android sqlite android-recyclerview android-sqlite

我正在开发android studio应用程序,并且已经在myviewpager类上实现了searchview;使用searchview时,我必须在searchview中写任何东西或字母时显示数据。它从sqlite数据库中获取数据或信件...

我已经在searchview上实现了clicklistner,搜索功能在It之下很糟糕。

我要面对什么问题;当我单击searchview时,写点东西;它什么也没显示...

这里是数据库助手类,以及我用来获取Java类进行搜索的功能……

 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;
}

这是我的Jave课程;我用来显示数据...并且与数据库助手链接...

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("A"));
            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();
}
}

我只想在文本过滤器的基础上显示相关数据;当我在searchview中写一些东西时,它从sqlite数据库中过滤了相同的数据,并在我已经实现的列表中显示

0 个答案:

没有答案