开发者

Displaying results set returned by a Cursor in EditText on Android

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开发者_Python百科. 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);




                }

            });


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


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));     
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜