如何从列表中删除项目而不在Android中删除它?

时间:2013-08-21 06:13:51

标签: android android-listview android-sqlite

我是Android新手,我的问题是,当我点击它时,我创建了一个方法“Mark As Completed”,它将真值设置到数据库中。我现在想要的是当我点击标记为已完成该项目从列表中删除但不是从数据库中删除。有可能吗?

编辑:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    addTasklist = (EditText)findViewById(R.id.addTasklist);
    taskList_completed = (CheckBox)findViewById(R.id.completedflag);
    Button ok = (Button)findViewById(R.id.add);
    ok.setOnClickListener(this);
    list();
    }
public void list(){
    db = new TodoTask_Database(getApplicationContext());
    list_tasklistname = (ListView)findViewById(R.id.listview);
    list = db.getAllTaskList();
    adapter = new CustomAdapter(Main_Activity.this, R.layout.tasklist_row, list);
    list_tasklistname.setAdapter(adapter);
    db.close();
    adapter.notifyDataSetChanged();
    registerForContextMenu(list_tasklistname);

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    Position = info.position;
    switch (item.getItemId()) {
case R.id.MarkAsCompleted:
        db = new TodoTask_Database(getApplicationContext());
        task = adapter.getItem(Position);
        int taskList_Id = task.getTaskListId();
        // here is database method which is setting item value true when mark as completed on long click listener is pressed.
db.Complete_TaskList(taskList_Id);              
        break;
        }
    return super.onOptionsItemSelected(item);
    }

我的自定义列表适配器:

public class CustomAdapter extends ArrayAdapter<Task> {

    private List<Task> dataitem;
    private Activity activity;
    TodoTask_Database db;

public CustomAdapter(Activity a, int textViewResourceId, List<Task> items) {
    super(a, textViewResourceId, items);
    this.dataitem = items;
    this.activity = a;
}

public static class ViewHolder{
    public TextView tasklistTitle;
    public TextView createdDate;
    public CheckBox completedflag;
}

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    final ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.tasklist_row, null);
        holder = new ViewHolder();
        holder.tasklistTitle = (TextView) v.findViewById(R.id.tasklistTitle);
        holder.createdDate = (TextView) v.findViewById(R.id.createdDate);
        holder.completedflag = (CheckBox) v.findViewById(R.id.completedflag);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final Task custom = dataitem.get(position);
    if (custom != null) {
        holder.tasklistTitle.setText(custom.getTaskListTitle());
        holder.createdDate.setText(custom.getTaskListCreated());
        holder.completedflag.setText(custom.getTaskListCompletedFlag());
    }
    return v;
}
   public synchronized void refresAdapter(List<Task> dataitems) {
    dataitem.clear();
    dataitem.addAll(dataitems);
    notifyDataSetChanged();
   }
}

1 个答案:

答案 0 :(得分:2)

添加

list.remove(Position); // Remove item from List
adapter.notifyDataSetChanged(); // Notify adapter 

case R.id.MarkAsCompleted。如下所示

case R.id.MarkAsCompleted:
        db = new TodoTask_Database(getApplicationContext());
        task = adapter.getItem(Position);
        int taskList_Id = task.getTaskListId();
        // here is database method which is setting item value true when mark as completed on long click listener is pressed.
db.Complete_TaskList(taskList_Id); 
// Remove from List
list.remove(Position);  // Added here
adapter.notifyDataSetChanged();   // Added here          
        break;

这将删除项目表单ListView。