SQlite 数据未更新为列表视图
SQlite Data is not updating into listview
SQlite Data is not updating into listview. no error showing just not save the edited value. when I click the list view it will open with the inserted value to edit but when I push the update button it won't update. Any Solution for my Problem ??
ListDataActivity.java
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
//Create an arraylist so that i can load the data to add in the arraylist
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.showAllData();
//jodi kono cursor e row na thake
if (cursor.getCount() == 0) {
Toast.makeText(getApplicationContext(), "no data is available", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textViewId, listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = adapterView.getItemAtPosition(position).toString();
cursor.moveToPosition(position);
String name = cursor.getString(1);
String surName = cursor.getString(2);
// listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
Intent intent = new Intent(ListDataActivity.this, UpdateActivity.class);
Toast.makeText(getApplicationContext(), "Selected Value : " + selectedValue, Toast.LENGTH_LONG).show();
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("surName", surName);
startActivity(intent);
finish();
}
});
UpdateActivity.java
id = getIntent().getExtras().getLong("id");
String name = getIntent().getExtras().getString("name");
String surName = getIntent().getExtras().getString("surName");
updateName.setText(name);
updatePhone.setText(surName);
updateButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String name = updateName.getText().toString();
String surName = updatePhone.getText().toString();
if(view.getId()==R.id.updatebuttonId){
ListDataActivity.databaseHelper.updateInformation(id,name,surName);
startActivity(new Intent(UpdateActivity.this,ListDataActivity.class));
finish();
}
}
});
DatabaseHelper.Java
public int updateInformation(long id, String name, String surName) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(SURNAME,surName);
String whereArgs[] = {""+id};
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,NAME+ "=?",whereArgs);
return count;
}
我认为您的问题是您正在尝试使用 WHERE 子句进行更新,该子句实际上是更新 NAME = 已传递的 ID 的行 (可能永远找不到这样的行).
您需要将传递给 update
方法的第三个参数更改为 id 列的名称。所以你想要这样的东西:-
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,ID+ "=?",whereArgs); //<<<<< ID instead of NAME
- 注意上面假定字符串常量 ID 包含 id 列的名称。因此,您可能需要相应地更改 ID。
SQlite Data is not updating into listview. no error showing just not save the edited value. when I click the list view it will open with the inserted value to edit but when I push the update button it won't update. Any Solution for my Problem ??
ListDataActivity.java
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
//Create an arraylist so that i can load the data to add in the arraylist
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.showAllData();
//jodi kono cursor e row na thake
if (cursor.getCount() == 0) {
Toast.makeText(getApplicationContext(), "no data is available", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textViewId, listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = adapterView.getItemAtPosition(position).toString();
cursor.moveToPosition(position);
String name = cursor.getString(1);
String surName = cursor.getString(2);
// listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
Intent intent = new Intent(ListDataActivity.this, UpdateActivity.class);
Toast.makeText(getApplicationContext(), "Selected Value : " + selectedValue, Toast.LENGTH_LONG).show();
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("surName", surName);
startActivity(intent);
finish();
}
});
UpdateActivity.java
id = getIntent().getExtras().getLong("id");
String name = getIntent().getExtras().getString("name");
String surName = getIntent().getExtras().getString("surName");
updateName.setText(name);
updatePhone.setText(surName);
updateButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String name = updateName.getText().toString();
String surName = updatePhone.getText().toString();
if(view.getId()==R.id.updatebuttonId){
ListDataActivity.databaseHelper.updateInformation(id,name,surName);
startActivity(new Intent(UpdateActivity.this,ListDataActivity.class));
finish();
}
}
});
DatabaseHelper.Java
public int updateInformation(long id, String name, String surName) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(SURNAME,surName);
String whereArgs[] = {""+id};
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,NAME+ "=?",whereArgs);
return count;
}
我认为您的问题是您正在尝试使用 WHERE 子句进行更新,该子句实际上是更新 NAME = 已传递的 ID 的行 (可能永远找不到这样的行).
您需要将传递给 update
方法的第三个参数更改为 id 列的名称。所以你想要这样的东西:-
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,ID+ "=?",whereArgs); //<<<<< ID instead of NAME
- 注意上面假定字符串常量 ID 包含 id 列的名称。因此,您可能需要相应地更改 ID。