Android SQLite:应用程序的模糊行为

时间:2016-06-30 13:03:38

标签: android sqlite

我正在尝试编辑并在SQLite数据库中保存一个注释。但是,当我编辑数组项Akhilesh Chobey并按后退键时,会发生以下情况。并且在重新启动应用程序时都没有保存。

enter image description here

MainActivity:

public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;

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

    notesListView = (ListView) findViewById(R.id.notesListView);
    notesArrayList = new ArrayList<String>();

    if(Main2Activity.myDb != null) {
        notesArrayList.clear();
        Cursor res = Main2Activity.myDb.getData();
        if (res.getCount() == 0) {
            Log.i("Error", "error");
            return;
        }

        while (res.moveToNext()) {
            notesArrayList.add(String.valueOf(res));
        }
    }

    notesArrayList.add("Akhilesh Chobey");
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
    notesListView.setAdapter(adapter);

    notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
            intent.putExtra("notePosition", i);
            startActivity(intent);
        }
    });
}
}

Main2Activity(编辑注释):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    editNote = (EditText) findViewById(R.id.noteEditText);

    myDb = new DatabaseOperations(Main2Activity.this);

    Intent intent = getIntent();
    position = intent.getIntExtra("notePosition", -1);
    if(position != -1){

        editNote.setText(MainActivity.notesArrayList.get(position));

    }
    editNote.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    if (myDb != null) {
        boolean isInserted = myDb.insertData(editNote.getText().toString());
        if (isInserted) {
            Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
        }
        MainActivity.notesArrayList.set(position, String.valueOf(charSequence));
        MainActivity.adapter.notifyDataSetChanged();
    }
}

数据库助手类:

public class DatabaseOperations extends SQLiteOpenHelper {

public static final String DatabaseName = "notes.db";
public static final String TableName = "notes";
public static final String Col1 = "text";

public DatabaseOperations(Context context) {
    super(context, DatabaseName, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TableName + " (text TEXT PRIMARY KEY AUTOINCREMENT) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    db.execSQL("DROP TABLE IF EXISTS " + TableName);
    onCreate(db);

}

public boolean insertData(String note){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Col1, note);

    long result = db.insert(TableName, null, contentValues);
    if(result == -1){
        return false;
    }else {
        return true;
    }
}

public Cursor getData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from " + TableName, null);
    return result;
}
}

1 个答案:

答案 0 :(得分:1)

你的错误就是你一直在使用

String.valueOf(res)

哪个错了。 您应该使用res.getString(res.getColumnIndex("text"))代替。

getString方法要求您的Cursor获取String类型的值,getColumnIndex返回给定列名称的列索引。在您的情况下,您的架构中只有一个名为text的列。

有关详细信息,请参阅Cursor文档。