0

In my code, searching for a word can be found very well,
But unfortunately, searching for two words or more doesn't work.
How to search a sentence (two words and more) in android programming?
My Java code:

public class MainActivity extends Activity {

    EditText etSearchKey;
    private ListView contentListView;
    private database db;
    private List<HashMap<String, Object>> books_list;
    private List<HashMap<String, Object>> resultBooks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_hezb);

        contentListView = (ListView) findViewById(R.id.tblhezb);
        etSearchKey = (EditText) findViewById(R.id.txthezb);
        db = new database(getBaseContext());
        db.open();

        Bundle data = getIntent().getExtras();
        books_list = db.getItem(data.getString("sea"));
        String[] from = {"name"};

        int[] to = {R.id.row_hezb_name};

        SimpleAdapter adb = new SimpleAdapter(getBaseContext(), books_list,
                R.layout.row_hezb, from, to) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View view = super.getView(position, convertView, parent);

                return view;

            }
        };

        contentListView.setAdapter(adb);

        db.close();
        etSearchKey.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                etSearchKey.setFocusableInTouchMode(true);

                etSearchKey.requestFocus();
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(etSearchKey, InputMethodManager.SHOW_FORCED);

            }
        });
        etSearchKey.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                String search_by = "name";

                if (etSearchKey.getText().length() < 1) {

                    return;
                }
                String key = etSearchKey.getText().toString().trim();

                String[] keys = key.split("\\s+");

                String query = search_by + " LIKE '%" + keys[0] + "%'";

                StringBuilder sb = new StringBuilder();

                for (int i = 1; i < keys.length; i++) {
                    sb.append(" OR " + search_by + " LIKE '%" + keys[i] + "%'");
                }

                query = query + sb.toString();

                showResultOfSearch(query);
            }
        });
        contentListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {

                        Intent i = new Intent(getBaseContext(),
                                List_hezbha.class);

                        String my_id = books_list.get(position).get("name")
                                .toString();

                        i.putExtra("name", my_id);

                        startActivity(i);

                    }
                });


    }

    public void showResultOfSearch(String query) {
        db.open();
        contentListView.setAdapter(null);
        resultBooks = db.getTableOfResultsOfSearchhezb(query);

        db.close();

        if (resultBooks.size() < 1) {

            return;
        }

        String[] from = {"name"};

        int[] to = {R.id.row_hezb_name};

        SimpleAdapter adb = new SimpleAdapter(
                getBaseContext(), resultBooks,
                R.layout.row_hezb, from, to
        ) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View view = super.getView(position, convertView, parent);

                return view;

            }
        };

        contentListView.setAdapter(adb);

        contentListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        try {
                            etSearchKey.setText("");
                            etSearchKey.clearFocus();
                            Intent i = new Intent(getBaseContext(), List_hezbha.class);

                            String my_id = resultBooks.get(position).get("name").toString();

                            i.putExtra("name", my_id);

                            startActivity(i);

                        } catch (Exception ex) {

                        }

                    }
                }
        );
    }
    

}

list_hezb.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e2e9fd"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txthezb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:background="@drawable/edt_txt"
        android:focusableInTouchMode="false"
        android:layout_margin="4dp"
        android:maxLines="1"
        android:padding="7dp"
        android:hint="Search"
        android:textSize="18sp" />

    <ListView
        android:id="@+id/tblhezb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="5dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="3dp"
        android:scrollbarThumbVertical="@drawable/scrollbar" />

</LinearLayout>

row_hezb.xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/border"
    android:paddingBottom="16dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="16dp" >

    <TextView
        android:id="@+id/row_hezb_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textColor="#000000"
        android:textSize="20dp" />

</RelativeLayout>

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database. I’d appreciate your cooperation.

5
  • @Quintin Balsdon I invite you to please answer my question. Commented Aug 16, 2023 at 12:35
  • @Just a Person I invite you to please answer my question. Commented Aug 16, 2023 at 12:40
  • @Thracian I invite you to please answer my question Commented Aug 16, 2023 at 12:44
  • @K M Rejowan Ahmmed I invite you to please answer my question Commented Aug 16, 2023 at 12:44
  • @Jatin Bhuva I invite you to please answer my question Commented Aug 16, 2023 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.