【问题标题】:Android CursorAdapter not updating all fieldsAndroid CursorAdapter 未更新所有字段
【发布时间】:2015-09-07 17:54:41
【问题描述】:

我的 android 应用程序有奇怪的问题。我有一些数据,我将这些数据保存在 SQLite 数据库中。在这个片段中,我尝试使用SimpleCursorAdapter从表中读取我的数据

public class LogFragment extends Fragment{

    private static SQLiteDatabase db;
    private static SQLiteOpenHelper helper;
    private static Context context;
    private static ListView listView;
    private static String senderOrReceiver;
    private static SimpleCursorAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();

        adapter = new SimpleCursorAdapter(context, R.layout.log_message, null,
                new String[]{Constants.COMMAND, Constants.VALUE, Constants.TIME_STAMP, Constants.MESSAGE_ID, Constants.SESSION_ID, Constants.PARAMS},
                new int[]{R.id.command, R.id.value, R.id.time_stamp, R.id.message_id, R.id.session_id, R.id.params}, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_log, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        listView = (ListView) view.findViewById(R.id.listView);
    }

    @Override
    public void onDestroy() {
        super.onDestroyView();
        if(db != null){
            db.close();
        }
    }

    public static void readFromDatabase(String sender){
        senderOrReceiver = sender;
        new DatabaseTalker().execute();
    }

    private static class DatabaseTalker extends AsyncTask <Void, Void, Cursor>{

        @Override
        protected Cursor doInBackground(Void... params) {
            helper = new Database(context);
            db = helper.getReadableDatabase();
            return db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);
            adapter.changeCursor(cursor);
            listView.setAdapter(adapter);
        }
    }
}

这就是我在 ListView 中得到的内容。我有六个字段(Command、Value、Time Stamp、MessageID、SessionID、Params),你可以看到只有一个字段被填写(例如)Command: On, Value: , Time Stamp: , MessageID: , SessionID: , Params: . 等等......为什么我会得到这个结果?

编辑:

我如何将数据写入数据库

public void addInfo(Information info){
        SQLiteDatabase db = this.getWritableDatabase();

        addToTable(db, Constants.COMMAND, info.getCommand());
        addToTable(db, Constants.VALUE, info.getValue());
        addToTable(db, Constants.TIME_STAMP, info.getTimeStamp());
        addToTable(db, Constants.MESSAGE_ID, info.getMessageID());
        addToTable(db, Constants.SESSION_ID, info.getSessionID());
        addToTable(db, Constants.PARAMS, info.getParams());

        db.close();
    }

    private static void addToTable(SQLiteDatabase db, final String TAG, String value){
        ContentValues values = new ContentValues();
        values.put(TAG, value);
        db.insert(Constants.TABLE_NAME, null, values);
    }

【问题讨论】:

  • 你是如何将数据写入数据库的?
  • @laalto 我刚刚编辑了我的帖子。

标签: android sqlite simplecursoradapter


【解决方案1】:

您的每个 addToTable() 调用都会插入一个仅包含一个列值的新行。

要插入包含所有值的行,请将值添加到同一 ContentValues 并调用 insert() 一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多