I have retrieved some data from sqlite database to custom ListView
. Now when I view the data in list view I want to click on a row and an alert box should appear to tell me do you want to delete this row? If I click yes then the particular row should be deleted please help me in this situation I am posting my code right down
public class UserDbHelper extends SQLiteOpenHelper {
String[] projections = {};
private static final String DATABASE_NAME = "userinfo.db";
private static final Integer DATABASE_VERSION = 1;
private static final String CREAT_QUERY =
"CREATE TABLE "+ UserContracts.NewUserInfo.TABLE_NAME+"("+ UserContracts.NewUserInfo.USER_NAME+" TEXT,"+
UserContracts.NewUserInfo.USER_MOB+" TEXT,"+ UserContracts.NewUserInfo.USER_EMAIL+" TEXT);";
public UserDbHelper (Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created / opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREAT_QUERY);
Log.e("DATABASE OPERATIONS", "Table created...");
}
public void addInformation(String name,String mob,String email,SQLiteDatabase db) {
ContentValues contentValues = new ContentValues();
contentValues.put(UserContracts.NewUserInfo.USER_NAME,name);
contentValues.put(UserContracts.NewUserInfo.USER_MOB,mob);
contentValues.put(UserContracts.NewUserInfo.USER_EMAIL,email);
db.insert(UserContracts.NewUserInfo.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATIONS", "One row inserted");
}
public Cursor getInformation(SQLiteDatabase db){
Cursor cursor;
String[] projections = {UserContracts.NewUserInfo.USER_NAME, UserContracts.NewUserInfo.USER_MOB,
UserContracts.NewUserInfo.USER_EMAIL};
cursor=db.query(UserContracts.NewUserInfo.TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}
public void deleteInformation(String projections, SQLiteDatabase sqLiteDatabase) {
String selection = UserContracts.NewUserInfo.USER_NAME+"LIKE?";
String[] selection_args = {projections};
sqLiteDatabase.delete(UserContracts.NewUserInfo.TABLE_NAME,selection,selection_args);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
And below is my another activity in which I wrote an alert box but I am not getting the required result here is the code
List list;
ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView)findViewById(R.id.list_view);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdapter);
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getInformation(sqLiteDatabase);
if (cursor.moveToFirst()){
do {
String name,mobile,email;
name = cursor.getString(0);
mobile = cursor.getString(1);
email = cursor.getString(2);
DataProvider dataProvider = new DataProvider(name,mobile,email);
listDataAdapter.add(dataProvider);
}while (cursor.moveToNext());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, final int position, long arg2) {
final AlertDialog.Builder builder = new AlertDialog.Builder(DataListActivity.this);
builder.setMessage("Do you want to delete this from database?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
listDataAdapter.remove(listDataAdapter.getItem(0));
Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
});
}
}
And here is my list data adapter class
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler{
TextView NAME,MOBILE,EMAIL;
}
ListView listView;
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public void remove(Object position) {
super.remove(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if (row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView)row.findViewById(R.id.text_user_name);
layoutHandler.MOBILE = (TextView)row.findViewById(R.id.text_user_mobile);
layoutHandler.EMAIL = (TextView)row.findViewById(R.id.text_user_email);
row.setTag(layoutHandler);
}
else {
layoutHandler = (LayoutHandler)row.getTag();
}
DataProvider dataProvider = (DataProvider)this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.MOBILE.setText(dataProvider.getMobile());
layoutHandler.EMAIL.setText(dataProvider.getEmail());
return row;
}
}
Any help would be highly appreciated thank you :)
You can get id of the row to the alert dialog. One solution would then be to write a delete method in your ListDataAdapter
:
public class ListDataAdapter extends ArrayAdapter {
//Your existing code here
// Remove your element from list and inform Adapter that data set has changed
public boolean removeRow(int rowIndex){
if(list.size() > rowIndex){
list.remove(rowIndex);
notifyDataSetChanged();
return true;
}
return false;
}
}
At the place you need to remove a row, simply get instance to your adapter and call removeRow
method. Example for removing 3th row:
((ListDataAdapter)listView.getAdapter()).removeRow(2);
But beware, notifyDataSetChanged()
will basically redraw your ListView, so if this is a common action, try to use optimised version - RecyclerView. At first it looks a bit more complicated, but usually it is worth it.
You can find some good tutorials, like this one: http://www.vogella.com/tutorials/AndroidRecyclerView/article.html