Android listview rename button

I have a Listview that will list the alarms that are in the database. I need to add a Toggle button next to each list item in order to enable / disable the alarm.

How to add Toggle button in Listview ?

R.layout.alarm_list :

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_reminders" android:textColor="#FFF"/> </LinearLayout> 

Java Code:

  private void fillData() { Cursor remindersCursor = aDbHelper.fetchAllAlarms(); startManagingCursor(remindersCursor); // Create an array to specify the fields we want to display in the list // (only TITLE) String[] from = new String[] { AlarmDbAdapter.KEY_TITLE }; // and an array of the fields we want to bind those fields to (in this // case just text1) int[] to = new int[] { R.id.text1}; // Now create a simple cursor adapter and set it to display SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.alarm_row, remindersCursor, from, to); setListAdapter(reminders); } 

R.layout.alarm_row :

  <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:padding="10dip" android:layout_width="242dp" android:layout_height="wrap_content"/> 

My project is delayed.

reference

+1
source share
2 answers

There is no small fragment of ANS. to your problem. I suppose you need to have several choices. Now here is what you need.

Since you are using a SimpleCursorAdapter , you must replace it with a CursorAdapter . To do this, you must expand it as an abstract adapter. Once you do this, you will override the two functions.

  • newView Where you will create views of the list items by inflating R.layout.alarm_row (it should also contain your toggle button). You have a switch button that is not clickable.
  • bindView , where you will set the state of the switch and text to view the text

However, this is what you need on the side of the activity.

  • You put your ListView into multiple selection mode using android:choiceMode in xml or using setChoiceMode .

Now bindView will look like this:

 ListView lv = ((ListActivity)context).getListView(); // Containing all check states SparseBooleanArray sba = lv.getCheckedItemPositions(); // I am using check box cb.setChecked(false); // Cursor is passed as an argument. if(sba != null) if(sba.get(cursor.getPosition())) cb.setChecked(true); 

Link to documents:

CursorAdapter docs

http://developer.android.com/reference/android/widget/ListView.html

+1
source

Try creating a custom adapter, for example:

 public class YourAdapter extends BaseAdapter 

and a custom layout for the rows using the toggle button (you need to inflate the layout in the geView method).

Here you can find an example: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

Hope this helps.

+1
source

All Articles