1

So I've added button, but I'm getting an error with position variable. Am I doing it right?

    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;
    }

I'm writing a task manager program, and having trouble with the listView. I've created the list and now I want to delete an item on the list when the button on the item is clicked. I'm not sure where the button should be created and add the listener. Can someone help me please thanks.

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 Answers 2

1

Add the button and its click listener in TaskListItem, delete the item and call the notifyDataSetChanged() on the list to refresh

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, do I have to delete the task in TaskListAdapter. I'm not quite sure how to do it, do I find the position of the button that's been clicked on.
Take the position of the list item and delete the item from "tasks" in your adapter
If I create the button in the TaskListItem, I can't call the adapter.
0

If the button is subview of row view then in getView() set the position as tag of button view. and set the activity as onclicklistener.

In onClick, check the tag of button which will tell you the exact position of row.

2 Comments

Thanks, can you explain more about "set the position as tag of button view", I haven't dealt with tags yet.
button.setTag(new Integer(position)); && Integer position = (Integer)button.getTag();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.