我试图用用户的输入更新数据库,但数据库没有更新。以下是我的java代码,其中用户提供了新的输入&另一个DatabaseHelper代码,其中我试图更新数据库。
UpdateTour.java
public class TourActivity extends AppCompatActivity {
DatabaseHelper db;
private LayoutInflater inflater;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new DatabaseHelper(getApplicationContext());
displayList();
ListView tourList = (ListView) findViewById(R.id.tour_listview);
tourList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editCashCarried = (EditText) content.findViewById(R.id.edTxt_CashCarried);
final EditText editOnTour = (EditText) content.findViewById(R.id.edTxt_TourCollection);
String ID = String.valueOf(db.getEventID(position));
String NAME = db.getTourName(position + 1);
String CASH = db.getOnTour(position + 1);
String CASHCARRIED = db.getCashCarried(position + 1);
editEvent.setText(NAME);
editCashCarried.setText(CASH);
editOnTour.setText(CASHCARRIED);
AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Edit Event")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Tour tour = new Tour();
//tour.setEventId((int) System.currentTimeMillis());
tour.setEventName(editEvent.getText().toString());
tour.setCashCarried(editCashCarried.getText().toString());
tour.setOnTourCollection(editOnTour.getText().toString());
db.updatesTour(tour);
displayList();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
}
private void displayList() {
ListView tourListView = (ListView) findViewById(R.id.tour_listview);
TourAdapter tourAdapter = new TourAdapter(
this,
R.layout.tour_list,
db.getAllTours()
);
tourListView.setAdapter(tourAdapter);
registerForContextMenu(tourListView);
}
}
DBHelper.java
public boolean updatesTour(Tour tour) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EVENTNAME, tour.getEventName());
values.put(KEY_EVENTSTARTDATE, tour.getEventStartDate());
values.put(KEY_EVENTENDDATE, tour.getEventEndDate());
values.put(KEY_LEADERS, tour.getLeaders());
values.put(KEY_CASHCARRIED, tour.getCashCarried());
values.put(KEY_ONTOURCOLLECTION, tour.getOnTourCollection());
values.put(KEY_CREATED_AT, getDateTime());
values.put(KEY_UPDATED_AT, getDateTime());
int i = db.update(TABLE_TOUR, values, KEY_ID + " = " + tour.getId(), null);
return i > 0;
}
在onclick方法中你还没有设置旅游对象的id
tour.setId(ID);
所以id应该是null
试试这个:
int i = db.update(TABLE_TOUR, values, KEY_ID + "=?" , new String[]{tour.getId()});