I am absolute beginner to Android. Now I start working with Sqlite database in my tutorial project. I tried insert and selecting data.All worked fine. But now I start updating row as my first time. But row is actually not updated in database. But it is not throwing error. Please what is wrong with my code?
My database helper class
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "todo.db";
private static final String TABLE_NAME = "task";
private static final String COLUMN_ID = "id";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_DATE ="date";
private static final String COLUMN_DONE = "done";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_DESCRIPTION+" TEXT,"+
COLUMN_DATE+" DATE,"+COLUMN_DONE+" BOOLEAN)";
SQLiteDatabase db;
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
this.db = db;
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
public void insertTask(Task task)
{
db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DESCRIPTION,task.getDescription());
values.put(COLUMN_DATE,task.getDate().toString());
values.put(COLUMN_DONE,Boolean.FALSE.toString());
db.insert(TABLE_NAME, null, values);
db.close();
}
public ArrayList<Task> getAllTasks()
{
ArrayList<Task> items = new ArrayList<Task>();
db = getReadableDatabase();
String query = "SELECT * FROM "+TABLE_NAME;
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst())
{
do{
Task item = new Task();
String date = cursor.getString(2);
Date parsedDate = new Date();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try{
parsedDate = format.parse(date);
}
catch (ParseException e)
{
parsedDate = null;
}
item.setId(cursor.getInt(0));
item.setDescription(cursor.getString(1));
item.setDate(parsedDate);
item.setDone(Boolean.valueOf(cursor.getString(3)));
items.add(item);
}
while (cursor.moveToNext());
}
return items;
}
public void markAsDone(int id){
db = getWritableDatabase();
ContentValues updatedData = new ContentValues();
updatedData.put(COLUMN_DONE, Boolean.TRUE);
String where = COLUMN_ID+" = "+String.valueOf(id);
db.update(TABLE_NAME,updatedData,where,null);
}
}
This is how I update database in fragment class. My fragment class
public class TaskListFragment extends Fragment {
private DatabaseHelper dbHelper;
private TextView taskTitle;
private ListView taskListView;
private ArrayAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
dbHelper = new DatabaseHelper(getActivity());
View view= inflater.inflate(R.layout.task_list, container, false);
taskTitle = (TextView)view.findViewById(R.id.task_textview);
taskListView = (ListView)view.findViewById(R.id.listViewTaskList);
int type = getArguments().getInt("type");
switch (type){
case R.integer.task_list_all:
ArrayList<Task> items = dbHelper.getAllTasks();
adapter = new TaskListAdapter(getActivity(),items);
taskListView.setAdapter(adapter);
taskTitle.setText("All tasks");
break;
}
taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
int tagId = Integer.valueOf(view.getTag().toString());
showOptionDialog(tagId);
return true;
}
});
return view;
}
public void showOptionDialog(final int id)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.row_option_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
Button doneBtn = (Button)view.findViewById(R.id.btn_row_option_done);
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.markAsDone(id);
Toast.makeText(getActivity().getBaseContext(),"Marked as done",Toast.LENGTH_SHORT).show();
//This is showig toast messagee "Mark as done". But data is not actually updated. Please help me why?
}
});
Button editBtn = (Button)view.findViewById(R.id.btn_row_option_edit);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Button deleteBtn = (Button)view.findViewById(R.id.btn_row_option_delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Button cancelBtn = (Button)view.findViewById(R.id.btn_row_option_cancel);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
alertDialog.setView(view);
alertDialog.show();
}
}
I am updating row using markAsDone method in my Fragment. Please what is wrong with my code? I have no idea to solve it cause it is not throwing any error. Is my update code correct please?
Aucun commentaire:
Enregistrer un commentaire