0

我是 Android 新手,我的问题是我创建了一个方法“标记为已完成”,当我点击它时,它在数据库中设置了真实值。我现在想要的是当我单击标记为已从列表中删除但未从数据库中删除的项目时。是否可以?

编辑:

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();
   }
}
4

1 回答 1

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;

这将删除列表视图中的项目。

于 2013-08-21T06:34:06.837 回答