Antonio Solana
Antonio Solana

Reputation: 119

How to add button listener to ListView?

I have a custom list item XML with 2 Buttons and I want to get when one is clicked (and a way of knowing which one). My ListView is made with data from a database. This is what I use for populating the list:

private DBManager manager;
private Cursor cursor;
private ListView lista;
SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_home_layout);

    manager = new DBManager(this);
    lista = (ListView) findViewById(R.id.listview);



    String[] from = new String[]{manager.CN_NAME, manager.CN_DESCRIPTION, manager.CN_TIME, manager.CN_DAY};
    int[] to = new int[] {R.id.list_title, R.id.list_description, R.id.list_time, R.id.list_day};
    cursor = manager.cargarCursorData("data");
    adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to,0);
    lista.setAdapter(adapter);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

I've seen people saying that you have to use bindView but I don't have anything like that and I don't know what it is. Sorry for my ignorance.

Upvotes: 0

Views: 84

Answers (1)

Asif Patel
Asif Patel

Reputation: 1764

You can go through this tutorials for Creating Custom Adapter, in which you can get the which Button is clicked:

http://www.survivingwithandroid.com/2012/10/android-listview-custom-adapter-and.html

http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

Upvotes: 2

Related Questions