【问题标题】:How to show all the search items from the android database如何显示android数据库中的所有搜索项
【发布时间】:2016-04-10 18:00:21
【问题描述】:

我正在制作一个需要从其数据库中查询数据项的 android 应用程序。我马上就好。但是当我搜索一个项目时它只显示一个项目,尽管有更多项目要显示(即有多个项目与搜索值匹配。 我的搜索代码在这里......

 search_group.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            et_group = ET_GROUP.getText().toString();
            et_district = ET_DISTRICT.getText().toString();
            et_city = ET_CITY.getText().toString();
            DatabaseOperations DOP = new DatabaseOperations(CTX);
            Cursor CR = DOP.getInformation_Search(DOP);
            CR.moveToFirst();
            boolean login_status = false;
            String NAME = "";
            String PHONE_NUM = "";
            String DISTRICT = "";
            String CITY = "";
            String GROUP = "";

            do {

                NAME = CR.getString(0);
                DISTRICT = CR.getString(1);
                CITY = CR.getString(2);
                GROUP = CR.getString(3);
                PHONE_NUM = CR.getString(4);


                if (et_group.equals(CR.getString(3))&& et_district.equals(CR.getString(1)) ) {
                    login_status = true;

                    for (CR.moveToFirst(); !CR.isAfterLast(); CR.moveToNext())
                    {
                        show_result.setText("found\n" + "Group = " + GROUP + "\n Name = " + NAME + "\nDistrict = " + DISTRICT + "\nCity = " + CITY + "\nPhone Number = " + PHONE_NUM);
                    }
                }

                else
                {
                    show_result.setText("Group not FOUND !!");
                }


            } while (CR.moveToNext());

我已经声明了,

 TextView show_result;
show_result = (TextView) findViewById(R.id.tv_search_blood_group);

.xml 代码是

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv_search_blood_group"
    android:layout_marginTop="30dp"

    android:textColor="#ffffff"
    android:textStyle="bold|italic"
    android:gravity="center"
    android:layout_gravity="center"
    />

    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android database search


    【解决方案1】:

    您已经在此处的内部 for 循环中移动到最后一个:

    for (CR.moveToFirst(); !CR.isAfterLast(); CR.moveToNext())
                        {
                            show_result.setText("found\n" + "Group = " + GROUP + "\n Name = " + NAME + "\nDistrict = " + DISTRICT + "\nCity = " + CITY + "\nPhone Number = " + PHONE_NUM);
                        }
    

    要么使用另一个光标,要么记住位置并向后移动。

    评论后更新

    经过 cmets 中的所有讨论,最终代码应该是这样的:

    search_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                et_group = ET_GROUP.getText().toString();
                et_district = ET_DISTRICT.getText().toString();
                et_city = ET_CITY.getText().toString();
                DatabaseOperations DOP = new DatabaseOperations(CTX);
                Cursor CR = DOP.getInformation_Search(DOP);
                CR.moveToFirst();
                boolean login_status = false;
                String NAME = "";
                String PHONE_NUM = "";
                String DISTRICT = "";
                String CITY = "";
                String GROUP = "";
    
                do {
    
                    NAME = CR.getString(0);
                    DISTRICT = CR.getString(1);
                    CITY = CR.getString(2);
                    GROUP = CR.getString(3);
                    PHONE_NUM = CR.getString(4);
    
    
                    if (et_group.equals(CR.getString(3))&& et_district.equals(CR.getString(1)) ) {
                        login_status = true;
                        show_result.setText(show_result.getText() + "found\n" + "Group = " + GROUP + "\n Name = " + NAME + "\nDistrict = " + DISTRICT + "\nCity = " + CITY + "\nPhone Number = " + PHONE_NUM);
                    }
    
                    else
                    {
                        show_result.setText(show_result.getText() +  "Group not FOUND !!");
                    }
    
    
                } while (CR.moveToNext());
    

    【讨论】:

    • 但是我认为内部循环应该从头到尾打印或显示匹配的项目,不是吗??
    • 没有。您正在循环外初始化打印的变量。我会在一分钟内发布并更新答案。坚持
    • 我已经复制并粘贴了您的答案,但这次它也显示了最后一个匹配的值,而不是全部显示。这是我在滚动视图下使用 TextView 的原因吗?还是什么???
    • 哦!您正在替换它...尝试附加
    • 我已经尝试用你更新的 for 循环替换我的 for 循环,但问题仍然存在。
    【解决方案2】:

    看起来您正在替换 show_result TextView 上的值。连接您的 Cursor 字符串值并在 show_result 上添加一次。

    【讨论】:

      【解决方案3】:

      而不是

      show_result.setText("text here");
      

      使用

      show_result.append("text here");
      

      你做对了,但是任何时候条件都是真的。它将新文本设置为 textview 并替换以前的文本。这就是为什么最后,textview 中只显示一个值。所以你可以使用

      textView.append() 
      

      它将新值附加到前一个值。希望成功

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 2014-09-23
        相关资源
        最近更新 更多