Populate ArrayList from sqllite, and check if it contains

时间:2017-04-10 01:21:12

标签: android sqlite

I am populating the ArrayList of strings from sqllite database. The user types the value he wants to find, and search result is displayed in the list view to the user. Right now when i input few entries in to the data base it always all the data. I want to do this search with java not Sql queries, the code i have seems to work to some extent, but it doesn't really do what i want it to do. What am I doing wrong here?

public void searchEventDetails(){

    final ArrayList<String> dbEntries = new ArrayList<>();
    final ArrayList<String> entryId = new ArrayList<>();

    Cursor cursor = eventDB.getAllDataSearch();

    if (cursor.moveToFirst()){
        do{

                dbEntries.add("Title: "+ cursor.getString(1)+"\n" + "Description: "+cursor.getString(2));

               entryId.add(cursor.getString(0));

        }while(cursor.moveToNext());
    }
    cursor.close();

        if (dbEntries.contains(txtSearch.getText())){
            System.out.print("Value");
        }
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dbEntries);

        ListView listView = (ListView) findViewById(R.id.listViewFromDBSearch);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String itemSelected = "You Selected " + String.valueOf(parent.getItemAtPosition(position));
                Toast.makeText(SearchEvent.this, itemSelected, Toast.LENGTH_SHORT).show();
                Intent showSearchEvents = new Intent(getApplicationContext(),ShowSearchEvents.class);
                search = String.valueOf(parent.getItemAtPosition(position));

                for (int i=0;i<dbEntries.size();i++){
                    if (search.equals(dbEntries.get(i))){

                        SearchEvent.this.id = entryId.get(i);
                    }

                }
                showSearchEvents.putExtra("id", SearchEvent.this.id);
                startActivity(showSearchEvents);
            }
        });
}

1 个答案:

答案 0 :(得分:0)

You must do it like this

public void searchEventDetails(String search){

    final ArrayList<String> dbEntries = new ArrayList<>();
    final ArrayList<String> entryId = new ArrayList<>();

    Cursor cursor = eventDB.getAllDataSearch(search);

    if (cursor.moveToFirst()){
        do{

                dbEntries.add("Title: "+ cursor.getString(1)+"\n" + "Description: "+cursor.getString(2));

               entryId.add(cursor.getString(0));

        }while(cursor.moveToNext());
    }
    cursor.close();


        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dbEntries);

        ListView listView = (ListView) findViewById(R.id.listViewFromDBSearch);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String itemSelected = "You Selected " + String.valueOf(parent.getItemAtPosition(position));
                Toast.makeText(SearchEvent.this, itemSelected, Toast.LENGTH_SHORT).show();
                Intent showSearchEvents = new Intent(getApplicationContext(),ShowSearchEvents.class);
                search = String.valueOf(parent.getItemAtPosition(position));

                for (int i=0;i<dbEntries.size();i++){
                    if (search.equals(dbEntries.get(i))){

                        SearchEvent.this.id = entryId.get(i);
                    }

                }
                showSearchEvents.putExtra("id", SearchEvent.this.id);
                startActivity(showSearchEvents);
            }
        });
}

then in your method getAllDataSearch() add String parameter like this and use LIKE to your query

public cursor getAllDataSearch(String search){

String query = "SELECT * FROM your_table WHERE your column LIKE '%" + search + "%'"

//your stuff here
}

dont forget to use textwatcher in your editetxt

txtSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Call back the Adapter with current character to Filter

             searchEventDetails(txtSearch.getText().toString());

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

hope this answer helps