简体   繁体   中英

Can't view saved data from SQLite database

public void viewAll() {
    btnHighScore.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Cursor res = myDb.getAllData();
                    if(res.getCount() == 0) {
                        showMessage("Error", "Nothing found");
                        return;
                    }
                    StringBuffer buffer = new StringBuffer();
                    while(res.moveToNext()) {
                        buffer.append("ID : " + res.getString(0) + "\n");
                        buffer.append("Firstname : " + res.getString(1) + "\n");
                        buffer.append("Lastname : " + res.getString(2) + "\n");
                    }
                    showMessage("High Score Table", buffer.toString());
                }
            }
    );
}

public void showMessage(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

You can see the full code here: SQLite App

高分表

I am having trouble to view the data saved on my database. I've upload the code here so someone can help me find the mistake I have made because I am getting no errors.

Based on the code in the link you need to get the firstname and lastname from the views. See line 142. Fetching the name values in onCreate the views are blank so your inserting empty strings into your database.

btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
    public void onClick(View v) {
        String firstname = etfirstname.getText().toString();
        String lastname = etlastname.getText().toString();
        boolean isInserted = myDb.insertdata(firstname, lastname);
        if(isInserted == true)
            Toast.makeText(SaveData.this, "Data Inserted", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(SaveData.this, "Data not Inserted", Toast.LENGTH_LONG).show();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM