如何在android编程中从ListView中搜索一个句子而不是一个单词?

How to search a sentence not a word from the ListView in android programming?

提问人:Shahin Sadeghi 提问时间:8/13/2023 更新时间:8/13/2023 访问量:49

问:

在我的代码中,搜索一个单词可以很好地找到,但不幸的是,
搜索两个或更多单词不起作用。
如何在android编程中搜索一个句子(两个单词和更多)?
我的 Java 代码:

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代码:

<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代码:

<?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 是一个视图,它将多个项目分组并在垂直可滚动列表中显示它们。使用适配器自动将列表项插入到列表中,该适配器从源(如数组或数据库)中提取内容。 感谢您的合作。

java 搜索 android-listview android-edittext

评论

0赞 Shahin Sadeghi 8/16/2023
@Quintin巴尔斯登,我邀请你回答我的问题。
0赞 Shahin Sadeghi 8/16/2023
@Just一个人,我邀请你回答我的问题。
0赞 Shahin Sadeghi 8/16/2023
@Thracian 我邀请您回答我的问题
0赞 Shahin Sadeghi 8/16/2023
@K M Rejowan Ahmmed 我邀请你回答我的问题
0赞 Shahin Sadeghi 8/16/2023
@Jatin Bhuva,我邀请您回答我的问题

答: 暂无答案