java - SQLite 从数据库读取数据

标签 java android android-sqlite

我正在尝试从数据库获取所有数据并显示。但它不起作用。谁能告诉代码有什么问题吗?谢谢。这是代码

数据库.java

//onlly the method

public List<Appo> getAllData(){

        List<Appo> appos = new ArrayList<>();

        String APPOINTMENT_TABLE_NAME_SELECT_QUERY =
                String.format("SELECT * FROM %s ",
                        APPOINTMENT_TABLE_NAME
                       );

        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(APPOINTMENT_TABLE_NAME_SELECT_QUERY, null);
        try {
            if (cursor.moveToFirst()) {
                do {
                    Appo.title = cursor.getString(cursor.getColumnIndex(APPOINTMENT_COLUMN_TITLE));
                    Appo.time = cursor.getString(cursor.getColumnIndex(APPOINTMENT_COLUMN_TITLE));
                    Appo.details = cursor.getString(cursor.getColumnIndex(APPOINTMENT_COLUMN_DETAILS));
                    Appo.day = cursor.getString(cursor.getColumnIndex(APPOINTMENT_COLUMN_DAY));
                } while(cursor.moveToNext());
            }
        } catch (Exception e) {

        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return appos;
    }

Appo.java

public class Appo {

    public static int id;
    public static String title;
    public static String time;
    public static String details;
    public static String day;
}

ViewAndEdit.java

public class ViewAndEdit extends AppCompatActivity {

    private TextView list;
    private Button btnview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_and_edit);

        list =(TextView) findViewById(R.id.listappo);
        btnview =(Button)findViewById(R.id.btnview);

        final DataBase databaseHelper = DataBase.getInstance(this);

        btnview.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final EditText title = new EditText(ViewAndEdit.this);
                new AlertDialog.Builder(ViewAndEdit.this)
                        .setTitle("Edit")
                        .setView(title)

                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();

                List<Appo> appos = databaseHelper.getAllData();
                for (Appo appo : appos) {
                    list.setText(appo.toString());
                }
            }
        });

    }
}

最佳答案

public List<Appo> getAllData() {
    List<Appo> appos = new ArrayList<Appo>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + APPOINTMENT_TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Appo appo = new Appo();
            appo.setTital(cursor.getString(0));
            appo.setTime(cursor.getString(1));
            appo.setDetails(cursor.getString(2));
            appo.setday(cursor.getString(3));

            //Adding contact to list
            appos.add(appo);
        } while (cursor.moveToNext());
    }
    // return all list
    return appos;
}

有关更多信息,请查看 Android SQLite Database

关于java - SQLite 从数据库读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43540957/

相关文章:

Java赋值运算符执行

java - DocumentFilter 只允许将数字和句点 (.) 放入 JTextField 中?

java - AsyncTask 复制文件

javascript - deviceready 未在 Android 设备上触发

android - 如何禁用android手机中的清除数据按钮

java - 在 roomdb android 中面临 @Relation 的问题

android - 从 SQLite 数据库的列中获取总和和平均值

java - Spring MVC REST - 根据请求内容类型返回 xml 或 json

android - Android:组织代码-Retrofit2样板

java - 在不破坏不变性的情况下更新不可变对象(immutable对象)