在 Android 上的 EditText 中显示 Cursor 返回的结果集

发布于 2024-10-22 05:59:13 字数 1994 浏览 1 评论 0原文

请帮助我调试或找到我遇到的问题的解决方案.. 我在这里想做的是,使用读取数据库表中的所有值 光标对象。

然后对于特定列,即 UID(它是外键),我想显示所有多个行条目。 (将 UID 与 cur.getString(3) 进行比较)

我使用下面的代码在 TextView 中显示结果,但存在一些延迟 我遇到了。

调用此活动后,结果集并未立即显示,但是在尝试几次重新登录后,它工作正常。 (我的应用程序有一个登录表单)

因此我决定使用 TextView,它可以在单击按钮时显示结果集,但是当我单击按钮时活动崩溃。

package com.androidarun.mobilewallet;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayCCards extends Creditcard_Registration {


    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.creditcards_ui);
            final EditText view = (EditText) findViewById(R.id.displayarea);
           final StringBuilder ret = new StringBuilder(1000);        

            final MyDBAdapter db = new MyDBAdapter(getBaseContext()); 


            Button showcc = (Button) findViewById(R.id.showcc);  

            showcc.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    Cursor cur = db.getAllCards();
                  cur.moveToFirst();

                  while (cur.isAfterLast() == false) {
                      if ( Integer.parseInt(cur.getString(3))== UID)
                          ret.append("Credit card number: " + cur.getString(1) + " \n" + 
                                  " CVV : " + cur.getString(2)+ " \n " + " Validity : " + cur.getString(4)+ " \n ");
                                        cur.moveToNext();
                  }
                  cur.close();
                  db.close();
                 view.setText(ret);




                }

            });

kindly help me debug or find a solution to a problem im encountering..
What i'm trying to do here is,reading all the values from a table in database using a
Cursor object.

Then for a particular column i.e. the UID (which is a foreign key), i want to display all the multiple row-entries. ( comparing UID to cur.getString(3) )

I used the below code to display the result in a TextView but there was some latency which
i encountered.

The result set was not displayed as soon as this activity was called, however after a couple of re-log in attempts, it worked fine. ( my application has a log in form )

Hence i decided to use a TextView that can display the result set on the click of a button, however the activity crashes when i click on the button.

package com.androidarun.mobilewallet;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayCCards extends Creditcard_Registration {


    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.creditcards_ui);
            final EditText view = (EditText) findViewById(R.id.displayarea);
           final StringBuilder ret = new StringBuilder(1000);        

            final MyDBAdapter db = new MyDBAdapter(getBaseContext()); 


            Button showcc = (Button) findViewById(R.id.showcc);  

            showcc.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    Cursor cur = db.getAllCards();
                  cur.moveToFirst();

                  while (cur.isAfterLast() == false) {
                      if ( Integer.parseInt(cur.getString(3))== UID)
                          ret.append("Credit card number: " + cur.getString(1) + " \n" + 
                                  " CVV : " + cur.getString(2)+ " \n " + " Validity : " + cur.getString(4)+ " \n ");
                                        cur.moveToNext();
                  }
                  cur.close();
                  db.close();
                 view.setText(ret);




                }

            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女皇必胜 2024-10-29 05:59:14

我认为您尝试做的事情应该让

 db.open();
 while (cur.isAfterLast() != false) {
                  if ( Integer.parseInt(cur.getString(3))== UID)
                      ret.append("Credit card number: " + cur.getString(1) + " \n" + 
                              " CVV : " + cur.getString(2)+ " \n " + " Validity : " +     cur.getString(4)+ " \n ");
                                    cur.moveToNext();
               }
 db.close();

您每次尝试访问数据库时都应该打开和关闭数据库。而 while 应该有 (cur.isAfterLast() != false

I think what you are trying do should have

 db.open();
 while (cur.isAfterLast() != false) {
                  if ( Integer.parseInt(cur.getString(3))== UID)
                      ret.append("Credit card number: " + cur.getString(1) + " \n" + 
                              " CVV : " + cur.getString(2)+ " \n " + " Validity : " +     cur.getString(4)+ " \n ");
                                    cur.moveToNext();
               }
 db.close();

you should open and close db each time you try to access it. and while should have (cur.isAfterLast() != false

定格我的天空 2024-10-29 05:59:14

您可以使用 cur.moveToNext 函数作为 while 条件,然后使用 cur.getString(1)cur.getString(2 ) 在循环内:

while (cur.moveToNext()) {
    Log.e("h", c1.getString(1));
    Log.e("h", c1.getString(2));
    Log.e("h", c1.getString(3));     
}

You can use cur.moveToNext function as the while condition and then use cur.getString(1) and cur.getString(2) inside the loop:

while (cur.moveToNext()) {
    Log.e("h", c1.getString(1));
    Log.e("h", c1.getString(2));
    Log.e("h", c1.getString(3));     
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文