Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:11:06+00:00 2026-06-10T02:11:06+00:00

So I’ve added button, but I’m getting an error with position variable. Am I

  • 0

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());
}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:11:07+00:00Added an answer on June 10, 2026 at 2:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.