How can i retrieve the selected checkbox text from the list?
say i have 5 checkboxes selected, i need to retrieve the text from the selected checkboxes.
here's the XML and code, in which i set android contact names as checkbox text
XML:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:padding="5dip"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <ListView android:id="@+id/contactlist1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- <TextView android:id="@+id/empty"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/emptyPhonebook"
- />
- </FrameLayout>
- </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
JAVA:
Using java Syntax Highlighting
- package com.selectFaves;
- import java.util.ArrayList;
- import java.util.List;
- import com.t_systems.R;
- import com.ifaves.IFavesActivity;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.Contacts.People;
- import android.view.Menu;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Menu.Item;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.AdapterView.OnItemSelectedListener;
- /** Displays Android Contact list as Checkbox Text in a List View*/
- public class SelectFavesActivity1 extends ListActivity implements OnItemSelectedListener{
- private static final int UPDATE_FAVES_ID = Menu.FIRST;
- private static final int CANCEL_ID = Menu.FIRST + 1;
- private List<String> contact_list_array = new ArrayList<String>();
- private int currentPosition = 0;
- @Override
- protected final void onCreate(final Bundle icicle) {
- super.onCreate(icicle);
- setTheme(android.R.style.Theme_Dark);
- setContentView(R.layout.selectfaves1);
- final ListView listView = (ListView) findViewById(R.id.contactlist1);
- final Cursor peopleCursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
- startManagingCursor(peopleCursor);
- int count = peopleCursor.count();
- if (count == 0)
- {
- AlertDialog.show(this, null, 0, "Contact list Empty", "OK", true);
- }
- else
- {
- final MyPhonebookAdapter phonebookAdapter = new MyPhonebookAdapter(peopleCursor);
- listView.setAdapter(phonebookAdapter);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, UPDATE_FAVES_ID, R.string.Select_Faves_menu_UpdateFaves);
- menu.add(0, CANCEL_ID, R.string.Select_Faves_menu_Cancel);
- return true;
- }
- @Override
- public boolean onMenuItemSelected(int featureId, Item item) {
- switch(item.getId()) {
- case UPDATE_FAVES_ID:
- return true;
- case CANCEL_ID:
- final Intent i = new Intent(this, IFavesActivity.class);
- startActivity(i);
- return true;
- }
- return super.onMenuItemSelected(featureId, item);
- }
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- }
- public class MyPhonebookAdapter extends BaseAdapter {
- /** The list of contact names. */
- private final ArrayList<String> contactNameList = new ArrayList<String>();
- /**
- * The phonebook adapter that accepts the people cursor.
- *
- * @param peopleCursor -
- * The cursor representing people in the db.
- */
- public MyPhonebookAdapter(final Cursor peopleCursor)
- {
- final int nameColumnIndex = peopleCursor.getColumnIndex(People.NAME);
- while (peopleCursor.next())
- {
- contactNameList.add(peopleCursor.getString(nameColumnIndex));
- }
- }
- /**
- * {@inheritDoc}
- */
- public final int getCount() {
- return contactNameList.size();
- }
- /**
- * {@inheritDoc}
- */
- public final Object getItem(final int position) {
- return position;
- }
- /**
- * {@inheritDoc}
- */
- public final long getItemId(final int position) {
- return position;
- }
- /**
- * {@inheritDoc}
- */
- public final View getView(final int position, final View convertView,
- final ViewGroup parent) {
- final LinearLayout linearLayout = new LinearLayout(
- SelectFavesActivity1.this);
- linearLayout.setOrientation(LinearLayout.HORIZONTAL);
- linearLayout
- .setLayoutParams(new LinearLayout.LayoutParams(240, 50));
- final CheckBox cBox_contactName = new CheckBox(SelectFavesActivity1.this);
- cBox_contactName.setLayoutParams(new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT));
- cBox_contactName.setText(contactNameList.get(position));
- cBox_contactName.setTextSize(20);
- linearLayout.addView(cBox_contactName);
- return linearLayout;
- }
- }
- public void onItemSelected(AdapterView parent, View v, int position, long id) {
- }
- public void onNothingSelected(AdapterView arg0) {
- // TODO Auto-generated method stub
- }
- }
Parsed in 0.049 seconds, using GeSHi 1.0.8.4


