Android CursorAdapter不更新所有字段

时间:2015-09-07 17:54:41

标签: android sqlite simplecursoradapter

我的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: .等等......为什么我得到这个结果?

enter image description here

编辑:

这是我如何将数据写入数据库

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);
    }

1 个答案:

答案 0 :(得分:1)

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

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