Dan Forbes
Dan Forbes

Reputation: 2824

Use Data from SQLite Database to Create Buttons in Android

I have successfully implemented an SQLite database in my Android app insofar as I can add information using EditTexts, Spinners and RatingsBars and view that information in a ListView. The next step I am trying to implement is to turn the ListView into a list of buttons rather than just text. Essentially, I would like to be able to click on an element in the list and then be taken to a screen that lists that element's attributes - at the bottom, I would like to put two buttons: "Edit Entry" and "Delete Entry". How do I go about using the information from my SQLite database to dynamically populate the texts visible on buttons?

Upvotes: 0

Views: 2065

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

Yes you can use buttons you can add them in getView() method. You need to fetch the data store it ArrayList, feed this ArrayList to the Adapter you gonna use. Then the listview will populate. (This is how I imagine:) If you are fetching 3 words then 3 buttons must be number of buttons present in each item of the listview. Overlapping, might scramble the listview. At the button, you can inflate another layout with "Edit Entry" and "Delete Entry" everytime, item/button of the item is clicked. If you are displaying one piece from the SQL db in each item, there is no need for buttons onItemClickedListener() will do the work.

Upvotes: 0

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

I don't think you will need to use buttons. You can implement the OnItemClickListener interface from your Activity and detect when an item in the list was pressed and perform your actions there.

For populating the ListView from the database you will need to extend Android CursorAdapter.

If you really want to use buttons in your ListView you will need to extend BaseAdapter and in the adapter

   getView(int position, View convertView, ViewGroup parent)

place a button in the List Item. But I still think that using just the ListView and OnItemClickListener will solve your problem.

Upvotes: 1

Related Questions