Reputation: 15
I have a custom ListView adapter. In the ListView I set a delete button.
Now I want to delete the fixed list item by clicking on that list's button. I have been able to delete it from the ListView. But when I refresh my layout activity, the list is shown in the same place.
Maybe it wasn't deleted from the database. I created the database in my main activity. But I deleted the list item from ListView adapter activity.
This is my ListView adapter:
import static com.example.shubho.digitalattendancesheet.Constant.FIRST_COLUMN;
import static com.example.shubho.digitalattendancesheet.Constant.SECOND_COLUMN;
public class ListViewAdapter extends BaseAdapter
{
public ArrayList<HashMap> list;
Activity activity;
public ListViewAdapter(Activity activity, ArrayList<HashMap> list) {
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.course_display_row, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.t_code);
holder.txtSecond = (TextView) convertView.findViewById(R.id.t_title);
ImageButton delete = (ImageButton) convertView.findViewById(R.id.t_delete);
convertView.setTag(holder);
delete.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
}
);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap map = list.get(position);
holder.txtFirst.setText((CharSequence) map.get(FIRST_COLUMN));
holder.txtSecond.setText((CharSequence) map.get(SECOND_COLUMN));
return convertView;
}
}
In my AddCourseActivity (main activity) I used:
public class AddCourseActivity extends AppCompatActivity implements View.OnClickListener{
EditText editCourseCode,editCourseTitle;
Button btnAdd,btnEdit,btnSearch,btnShowAll;
ImageButton btnDelete;
SQLiteDatabase db;
ListView listview;
int i;
ArrayList<String> ar =null;
ArrayList<String> ar1 =null;
ArrayList<String> SampleArrayList = new ArrayList<>();
private ArrayList<HashMap> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
editCourseCode = (EditText)findViewById(R.id.courseCode);
editCourseTitle = (EditText)findViewById(R.id.courseTitle);
btnAdd = (Button)findViewById(R.id.add);
btnShowAll = (Button)findViewById(R.id.show);
btnEdit = (Button)findViewById(R.id.update);
db = openOrCreateDatabase("CourseDB", Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS course(courseCode VARCHAR, courseTitle VARCHAR)");
btnAdd.setOnClickListener(this);
btnEdit.setOnClickListener(this);
btnShowAll.setOnClickListener(this);
}
public void onClick(View view){
if (view == btnAdd){
String str = editCourseCode.getText().toString().trim();
String str1 = editCourseTitle.getText().toString().trim();
if(str.length()>0 && str1.length()>0){
db.execSQL("insert into course values('" + str + "','" + str1 + "')");
Snackbar.make(view, "Inserted", Snackbar.LENGTH_SHORT).show();
editCourseCode.setText("");
editCourseTitle.setText("");
} else {
Snackbar.make(view, "Not Inserted", Snackbar.LENGTH_SHORT).show();
}
}
else if(view == btnShowAll){
try {
final ListView lview = (ListView) findViewById(R.id.list);
populateList();
final ListViewAdapter adapter = new ListViewAdapter(this, list);
lview.setAdapter(adapter);
} catch (Exception e) {
Snackbar.make(view, "No record found", Snackbar.LENGTH_SHORT).show();
}
}
else if(view==btnEdit){
if(editCourseCode.getText().toString().trim().length()==0 && editCourseTitle.getText().toString().trim().length()==0){
Snackbar.make(view, "Not Updated", Snackbar.LENGTH_SHORT).show();
return;
}
else {
if (editCourseCode.getText().toString().trim().length() != 0) {
db.execSQL("UPDATE course set courseTitle='"+editCourseTitle.getText()+"' WHERE " + " courseCode ='"+editCourseCode.getText()+"'");
}
else if (editCourseTitle.getText().toString().trim().length() != 0) {
db.execSQL("UPDATE course set courseCode='"+editCourseCode.getText()+"' WHERE " + " courseTitle ='"+editCourseTitle.getText()+"'");
}
Snackbar.make(view, "Updated", Snackbar.LENGTH_SHORT).show();
}
}
}
private void populateList() {
// TODO Auto-generated method stub
ar = new ArrayList();
ar1 = new ArrayList();
Cursor c = db.rawQuery("Select * from course", null);
c.moveToFirst();
do {
ar.add(c.getString(c.getColumnIndex("courseCode")));
ar1.add(c.getString(c.getColumnIndex("courseTitle")));
} while (c.moveToNext());
list = new ArrayList<HashMap>();
for ( i = 0; i < ar.size(); i++) {
HashMap temp = new HashMap();
temp.put(FIRST_COLUMN, ar.get(i));
temp.put(SECOND_COLUMN, ar1.get(i));
list.add(temp);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_course, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_profile:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
How can I delete the fixed list item from the database?
Upvotes: 0
Views: 237
Reputation: 24211
You just have to run a delete query in your database table as well when you are removing from the list. So the onClickListener
should look like this.
ImageButton delete = (ImageButton) convertView.findViewById(R.id.t_delete);
convertView.setTag(holder);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Delete from database as well
db.rawQuery("delete from course where courseCode = '" + list.get(position).get(FIRST_COLUMN) + "'", null);
// Now delete from list and update your ListView
list.remove(position);
notifyDataSetChanged();
}
});
Update
Based on the update of the question. I have modified your adapter and introduced some new variables in it. Please check the adapter and import the packages necessary in this adapter. Then modify the place from where you initiate the adapter.
public class ListViewAdapter extends BaseAdapter {
private ArrayList<HashMap> list;
private Context context;
private SQLiteDatabase db;
public ListViewAdapter(Context context, ArrayList<HashMap> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_display_row, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.t_code);
holder.txtSecond = (TextView) convertView.findViewById(R.id.t_title);
ImageButton delete = (ImageButton) convertView.findViewById(R.id.t_delete);
convertView.setTag(holder);
delete.setOnClickListener
(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = context.openOrCreateDatabase("CourseDB", Activity.MODE_PRIVATE, null);
db.execSQL("DELETE from course where courseCode = '" + list.get(position).get(FIRST_COLUMN) + "'");
db.close();
list.remove(position);
notifyDataSetChanged();
}
}
);
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap map = list.get(position);
holder.txtFirst.setText((CharSequence) map.get(FIRST_COLUMN));
holder.txtSecond.setText((CharSequence) map.get(SECOND_COLUMN));
return convertView;
}
}
Hope that helps!
Upvotes: 1
Reputation: 1335
Create a Interface
inside adapter class
interface OnDeleteListener {
onDeleteCourse(String code, int position)
}
Add this constructor to Adapter class
private OnDeleteListenre mListener;
public ListViewAdapter(List<HashMap> items, OnDeleteListener listener) {
mListener = listener
...
}
Add this method inside Adapter class
public void refresh(int position) {
// remove the item from the list
list.remove(position)
// refresh listview
notifyDataSetChanged();
}
Change delete
button implementation like below
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get the course code from the list according to position
...
...
// pass this code and item position here
mListener.onDelete(## PASS COURSE CODE HERE ##, position);
}
});
Let the Activity
class implement OnDeleteListener
and
@Override
public void onDeleteCourse(String code, int postion) {
// Delete the course from the database here
...
...
// notify the adapter
adapter.refresh(position)
}
Initialize the Adapter
adapter = ListViewAdapter(list, this)
Upvotes: 0
Reputation: 507
in OnClick
for the delete button, create a worker thread and execute a db query to delete the item.
You would need to uniquely identify the item in the list, so basically you need some sort of id on your item (which I presume you already have)
You can use AsyncTask
, Handlers
or Threads
to do this. Don't execute db queries on main thread otherwise your app would lag.
On a side note. Stop using notifyDataSetChanged
and instead use notifyItemRemoved
.
Upvotes: 0