Android List View 无法从数据库动态填充
创始人
2024-08-14 16:30:17
0

要在Android List View中动态填充数据,你可以使用适配器(Adapter)来连接数据库和List View。下面是一个使用SimpleCursorAdapter和SQLite数据库来动态填充List View的示例代码:

  1. 创建一个SQLite数据库和表格:
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_NAME = "mytable";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_NAME = "name";

    // 构造函数
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " TEXT)";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void insertData(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return cursor;
    }
}
  1. 在Activity或Fragment中使用SimpleCursorAdapter将数据填充到List View:
public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private DatabaseHelper databaseHelper;
    private SimpleCursorAdapter cursorAdapter;

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

        listView = findViewById(R.id.listView);
        databaseHelper = new DatabaseHelper(this);

        // 获取数据库中的所有数据
        Cursor cursor = databaseHelper.getAllData();

        // 定义要显示的列名
        String[] columns = new String[] {DatabaseHelper.COLUMN_NAME};

        // 定义要显示的控件id
        int[] to = new int[] {R.id.textViewName};

        // 创建适配器
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, columns, to, 0);

        // 将适配器设置到List View
        listView.setAdapter(cursorAdapter);
    }

    // 在某个事件触发时添加数据到数据库并更新List View
    public void addButtonClicked(View view) {
        EditText editTextName = findViewById(R.id.editTextName);
        String name = editTextName.getText().toString();
        databaseHelper.insertData(name);

        // 更新List View
        Cursor newCursor = databaseHelper.getAllData();
        cursorAdapter.swapCursor(newCursor);

        editTextName.setText("");
    }
}
  1. 在布局文件中定义ListView和添加按钮:


    

    

这样,当点击添加按钮时,会将EditText中的文本插入到数据库,并更新List View中显示的数据。

相关内容

热门资讯

安装Pillow时遇到了问题:... 遇到这个问题,可能是因为缺少libwebpmux3软件包。解决方法是手动安装libwebpmux3软...
安装React Native时... 当安装React Native时出现构建错误的情况,可以尝试以下解决方法:确保已经安装了最新版本的C...
安装Python库"... 安装Python库"firedrake"的解决方法如下:打开终端或命令提示符(Windows系统)。...
安装Rails时构建webso... 在安装Rails时,如果构建websocket-driver时发生错误,可以尝试以下解决方法:更新系...
安装react-native-... 要安装react-native-onesignal并在应用关闭时仍能接收通知,可以按照以下步骤进行:...
Apache Nifi在Kub... Apache Nifi可以在Kubernetes上运行,并且已经准备好用于生产环境。下面是一个使用H...
安装React-Scripts... 这是因为React-Scripts使用Facebook工具包中的一些脚本。 joinAdIntere...
安装React Native时... 安装React Native时可能会出现各种错误,下面是一些常见错误和解决方法的代码示例:Error...
按转换模式过滤日志【%t】。 要按照转换模式过滤日志,可以使用正则表达式来实现。下面是一个示例代码,使用Java语言的Patter...
安装ug未能链接到许可证服务器 安装UG未能链接到许可证服务器是UG用户在安装软件时常遇到的问题之一。该问题的解决方法需要技术向的知...