How to retrieve data from sqlite database in android and display it in TextView

后端 未结 5 540
攒了一身酷
攒了一身酷 2020-11-27 07:10

I am learning Android. I have a problem and I can\'t solve it. I want to retrieve data from an existing database and display it in a TextView after click button

相关标签:
5条回答
  • 2020-11-27 07:36

    You may use this following code actually it is rough but plz check it out

    db = openOrCreateDatabase("sms.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    Cursor cc = db.rawQuery("SELECT * FROM datatable", null);
    final ArrayList<String> row1 = new ArrayList<String>();
    final ArrayList<String> row2 = new ArrayList<String>();
    
    if(cc!=null) {
        cc.moveToFirst();   
        startManagingCursor(cc);
        for (int i=0; i<cc.getCount(); i++) {
    
        String number  = cc.getString(0);
        String message = cc.getString(1);
        row1.add(number);
        row2.add(message);
    
        final EditText et3 = (EditText) findViewById(R.id.editText3);
        final EditText et4 = (EditText) findViewById(R.id.editText4);
        Button bt1 = (Button) findViewById(R.id.button1);
        bt1.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                switch (v.getId()) {
                    case R.id.button1:
                        et3.setText(row1.get(count));
                        et4.setText(row2.get(count));
                        count++;
                        break;
                    default:
                        break;
                }
    
            }
        });
    
    cc.moveToNext();
    }
    
    0 讨论(0)
  • 2020-11-27 07:39

    on button click, first open the database, fetch the data and close the data base like this

    public class cytaty extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.galeria);
    
            Button bLosuj = (Button) findViewById(R.id.button1);
            bLosuj.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                myDatabaseHelper = new DatabaseHelper(cytaty.this);
                myDatabaseHelper.openDataBase();
    
                String text = myDatabaseHelper.getYourData(); //this is the method to query
    
                myDatabaseHelper.close(); 
                // set text to your TextView
                }
            });
        }
    }
    

    and your getYourData() in database class would be like this

    public String[] getAppCategoryDetail() {
    
        final String TABLE_NAME = "name of table";
    
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db  = this.getReadableDatabase();
        Cursor cursor      = db.rawQuery(selectQuery, null);
        String[] data      = null;
    
        if (cursor.moveToFirst()) {
            do {
               // get the data into array, or class variable
            } while (cursor.moveToNext());
        }
        cursor.close();
        return data;
    }
    
    0 讨论(0)
  • 2020-11-27 07:39
    TextView tekst = (TextView) findViewById(R.id.editText1); 
    

    You cannot cast EditText to TextView.

    0 讨论(0)
  • 2020-11-27 07:41

    First cast your Edit text like this:

    TextView tekst = (TextView) findViewById(R.id.editText1);
    tekst.setText(text);
    

    And after that close the DB not befor this line...

     myDataBaseHelper.close(); 
    
    0 讨论(0)
  • 2020-11-27 07:43

    You are using getData() method as void.

    You can not return values from void.

    0 讨论(0)
提交回复
热议问题