在 Android 上的 EditText 中显示 Cursor 返回的结果集
请帮助我调试或找到我遇到的问题的解决方案.. 我在这里想做的是,使用读取数据库表中的所有值 光标对象。
然后对于特定列,即 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您尝试做的事情应该让
您每次尝试访问数据库时都应该打开和关闭数据库。而 while 应该有 (cur.isAfterLast() != false
I think what you are trying do should have
you should open and close db each time you try to access it. and while should have (cur.isAfterLast() != false
您可以使用
cur.moveToNext
函数作为while
条件,然后使用cur.getString(1)
和cur.getString(2 )
在循环内:You can use
cur.moveToNext
function as thewhile
condition and then usecur.getString(1)
andcur.getString(2)
inside the loop: