1

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

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

2 回答 2

1

在TaskListItem中添加按钮及其点击监听,删除item并调用list上的notifyDataSetChanged()进行刷新

于 2012-08-18T15:56:34.987 回答
0

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

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

于 2012-08-18T15:54:00.540 回答