我有一个自定义ListView适配器。在ListView中,我设置了一个删除按钮。
现在我想通过点击该列表的按钮删除固定列表项。我已经能够从ListView中删除它。但是当我刷新布局活动时,列表显示在同一个地方。
也许它没有从数据库中删除。我在主要活动中创建了数据库。但是我从ListView适配器活动中删除了列表项。
这是我的ListView适配器:
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;
}
}
在我使用的AddCourseActivity(主要活动)中:
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);
}
}
}
如何从数据库中删除固定列表项?
答案 0 :(得分:0)
在OnClick
中删除按钮,创建工作线程并执行数据库查询以删除该项。
您需要唯一地标识列表中的项目,所以基本上您需要在项目上使用某种ID(我认为您已经拥有)
您可以使用AsyncTask
,Handlers
或Threads
来执行此操作。不要在主线程上执行数据库查询,否则你的应用程序会滞后。
旁注。停止使用notifyDataSetChanged
,而是使用notifyItemRemoved
。
答案 1 :(得分:0)
当您从列表中删除时,您还必须在数据库表中运行删除查询。所以onClickListener
应该是这样的。
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();
}
});
<强>更新强>
根据问题的更新。我修改了你的适配器并在其中引入了一些新的变量。请检查适配器并导入此适配器中所需的包。然后修改启动适配器的位置。
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;
}
}
希望有所帮助!
答案 2 :(得分:0)
在适配器类中创建Interface
interface OnDeleteListener {
onDeleteCourse(String code, int position)
}
将此构造函数添加到适配器类
private OnDeleteListenre mListener;
public ListViewAdapter(List<HashMap> items, OnDeleteListener listener) {
mListener = listener
...
}
在适配器类
中添加此方法public void refresh(int position) {
// remove the item from the list
list.remove(position)
// refresh listview
notifyDataSetChanged();
}
更改delete
按钮实现,如下所示
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);
}
});
让Activity
班级implement OnDeleteListener
和
@Override
public void onDeleteCourse(String code, int postion) {
// Delete the course from the database here
...
...
// notify the adapter
adapter.refresh(position)
}
初始化适配器
adapter = ListViewAdapter(list, this)