使用android中的按钮删除listview项目

时间:2012-08-18 15:31:48

标签: android listview adapter

所以我添加了按钮,但是我的位置变量出错了。我做得对吗?

    public View getView(int position, View convertView, ViewGroup parent) {
    TaskListItem tli;
    if(null == convertView){
        tli =(TaskListItem)View.inflate(context, R.layout.task_list_items, null);
        tli.setBinButton((ImageButton) tli.findViewById(R.id.bin));
        tli.getBinButton().setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                tasks.remove(position);
                notifyDataSetChanged();
            }
        });
    }
    else{
        tli = (TaskListItem)convertView;
    }
    tli.setTask(tasks.get(position));
    return tli;
    }

我正在编写一个任务管理器程序,并且遇到listView问题。我创建了列表,现在我想在单击项目上的按钮时删除列表中的项目。我不确定应该在哪里创建按钮并添加监听器。请有人帮助我,谢谢。

public class ViewTasksActivity extends ListActivity {

private Button addButton;
private TaskManagerApplication app;
private TaskListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setUpViews();

    app = (TaskManagerApplication)getApplication();
    adapter = new TaskListAdapter(app.getCurrentTask(), this);
    setListAdapter(adapter);
}

protected void onResume(){
    super.onResume();
    adapter.forceReload();
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    adapter.toggleTaskCompleteAtPosition(position);
}

protected void removeCompletedTasks() {
    adapter.removeCompletedTasks();

}

private void setUpViews() {
    addButton = (Button)findViewById(R.id.add_button);      
    addButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(ViewTasksActivity.this, AddTaskActivity.class);
            startActivity(intent);              
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}


public class TaskListAdapter extends BaseAdapter {

private ArrayList<Task> tasks;
private Context context;

public TaskListAdapter(ArrayList<Task> tasks, Context context) {
    super();
    this.tasks = tasks;
    this.context = context;
}

public int getCount() {
    return tasks.size();
}

public Object getItem(int position) {
    return (null == tasks) ? null: tasks.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    TaskListItem tli;
    if(null == convertView){
        tli =(TaskListItem)View.inflate(context, R.layout.task_list_items, null);
    }
    else{
        tli = (TaskListItem)convertView;
    }
    tli.setTask(tasks.get(position));
    return tli;
}

public void forceReload() {
    notifyDataSetChanged();
}

public void toggleTaskCompleteAtPosition(int position) {
    Task t = tasks.get(position);
    t.toggleComplete();
    notifyDataSetChanged();
}

public void removeCompletedTasks() {
    ArrayList<Task> completedTasks = new ArrayList<Task>();
    for(Task task : tasks){
        if(task.isComplete()){
            completedTasks.add(task);
        }
    }
    tasks.removeAll(completedTasks);
    notifyDataSetChanged();
}

public void removeTask(int position){
    tasks.remove(position);
}}

public class TaskListItem extends LinearLayout {

private Task task;
private TextView taskName;
private TextView resp;
private TextView prio;
private Button binButton;

public TaskListItem(Context context, AttributeSet attrs) {
    super(context, attrs);
}

protected void onFinishInflate(){
    super.onFinishInflate();
    taskName = (TextView) findViewById(R.id.the_task);
    resp = (TextView) findViewById(R.id.resp);
    prio = (TextView) findViewById(R.id.prio);
}

public Button getBin(){
    return binButton;
}

public Task getTask() {
    return task;
}

public void setTask(Task task) {
    this.task = task;
    taskName.setText(task.getName());
    resp.setText("Resp: " + task.getResponsible());
    prio.setText("Prio: " + task.getPriority());
}}

2 个答案:

答案 0 :(得分:1)

在TaskListItem中添加按钮及其单击侦听器,删除该项并调用列表上的notifyDataSetChanged()进行刷新

答案 1 :(得分:0)

如果按钮是行视图的子视图,则在getView()中将位置设置为按钮视图的标记。并将活动设置为onclicklistener。

在onClick中,检查按钮的标签,它会告诉您行的确切位置。