Dynamic ListView in Android

Yesterday one of my readers mail me, asking help for creating the dynamic list. Here is the solution where I have listed the abbreviation and name of the countries in two column list.

In this project I have mainly worked with 4 files.

CountriesList.java : To store the list of all countries and their abbreviations.

MyList.java : It contain the context of the example.

Main.xml : for the creation of the layout of the MyList context.

Two_col_row.xml : It only contain two TextView for the rows in the ListView.

//CountriesList.java

package com.dynalist;

public class CountriesList {
	public static String[] countries = { "Afghanistan", "Albania", "Algeria",
			"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica",
			"Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
			"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",
			"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
			"Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
			"Botswana", "Bouvet Island", "Brazil",
			"British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria",
			"Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada",
			"Cape Verde", "Cayman Islands", "Central African Republic", "Chad",
			"Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",
			"Colombia", "Comoros", "Congo",
			"Congo, the Democratic Republic of the", "Cook Islands",
			"Costa Rica", "Cote D'Ivoire", "Croatia", "Cuba", "Cyprus",
			"Czech Republic", "Denmark", "Djibouti", "Dominica",
			"Dominican Republic", "Ecuador", "Egypt", "El Salvador",
			"Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
			"Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland",
			"France", "French Guiana", "French Polynesia",
			"French Southern Territories", "Gabon", "Gambia", "Georgia",
			"Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada",
			"Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
			"Guyana", "Haiti", "Heard Island and Mcdonald Islands",
			"Holy See (Vatican City State)", "Honduras", "Hong Kong",
			"Hungary", "Iceland", "India", "Indonesia",
			"Iran, Islamic Republic of", "Iraq", "Ireland", "Israel", "Italy",
			"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati",
			"Korea, Democratic People's Republic of", "Korea, Republic of",
			"Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
			"Latvia", "Lebanon", "Lesotho", "Liberia",
			"Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania",
			"Luxembourg", "Macao",
			"Macedonia, the Former Yugoslav Republic of", "Madagascar",
			"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
			"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
			"Mayotte", "Mexico", "Micronesia, Federated States of",
			"Moldova, Republic of", "Monaco", "Mongolia", "Montserrat",
			"Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal",
			"Netherlands", "Netherlands Antilles", "New Caledonia",
			"New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
			"Norfolk Island", "Northern Mariana Islands", "Norway", "Oman",
			"Pakistan", "Palau", "Palestinian Territory, Occupied", "Panama",
			"Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn",
			"Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania",
			"Russian Federation", "Rwanda", "Saint Helena",
			"Saint Kitts and Nevis", "Saint Lucia",
			"Saint Pierre and Miquelon", "Saint Vincent and the Grenadines",
			"Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia",
			"Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone",
			"Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia",
			"South Africa", "South Georgia and the South Sandwich Islands",
			"Spain", "Sri Lanka", "Sudan", "Suriname",
			"Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland",
			"Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan",
			"Tanzania, United Republic of", "Thailand", "Timor-Leste", "Togo",
			"Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
			"Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda",
			"Ukraine", "United Arab Emirates", "United Kingdom",
			"United States", "United States Minor Outlying Islands", "Uruguay",
			"Uzbekistan", "Vanuatu", "Venezuela", "Viet Nam",
			"Virgin Islands, British", "Virgin Islands, U.s.",
			"Wallis and Futuna", "Western Sahara", "Yemen", "Zambia",
			"Zimbabwe" };
	public static String[] abbreviations = { "AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ",
			"AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB",
			"BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BA", "BW", "BV", "BR",
			"IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF",
			"TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR",
			"CI", "HR", "CU", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG",
			"SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF",
			"PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD",
			"GP", "GU", "GT", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK",
			"HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IL", "IT", "JM", "JP",
			"JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB",
			"LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY",
			"MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD",
			"MC", "MN", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "AN",
			"NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK",
			"PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR",
			"QA", "RE", "RO", "RU", "RW", "SH", "KN", "LC", "PM", "VC", "WS",
			"SM", "ST", "SA", "SN", "CS", "SC", "SL", "SG", "SK", "SI", "SB",
			"SO", "ZA", "GS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH",
			"SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN",
			"TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY",
			"UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW" };
}
//MyList .java

package com.dynalist;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyList extends Activity {
	/** Called when the activity is first created. */
	ListView listView;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ListView) findViewById(R.id.lv_country);
		listView.setAdapter(new EfficientAdapter(this));
	}
	private static class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return CountriesList.abbreviations.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.two_col_row, null);
				holder = new ViewHolder();
				holder.text1 = (TextView) convertView
						.findViewById(R.id.TextView01);
				holder.text2 = (TextView) convertView
						.findViewById(R.id.TextView02);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text1.setText(CountriesList.abbreviations[position]);
			holder.text2.setText(CountriesList.countries[position]);

			return convertView;
		}

		static class ViewHolder {
			TextView text1;
			TextView text2;
		}
	}
}
<?xml version="1.0" encoding="utf-8"?>
<!--main.xml-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffccd0"
    >
    <TextView
    	    android:text="Countries List"
    	    android:textColor="#b3000d"
    	    android:gravity="center_vertical|center_horizontal"
    	    android:textSize="26dip"
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content"
    	    android:textStyle="bold"
    	    android:background="#ffb0b6"
    	    android:layout_marginBottom="5dip"
    	    android:typeface="sans"/>
   	<RelativeLayout
		android:layout_width="fill_parent"
	 	android:layout_height="wrap_content"
		android:background="#570000">
	    <TextView android:id="@+id/tv_1"
			android:textColor="#FFFFFF"
			android:gravity="center_vertical|left"
			android:textSize="16dip"
			android:layout_height="wrap_content"
			android:textStyle="bold"
			android:typeface="serif"
			android:layout_width="70dip"
			android:paddingLeft="20dip"
			android:text="Abbr">
		</TextView>
		<TextView android:id="@+id/tv_2"
			android:textColor="#FFFFFF"
			android:gravity="center_vertical|left"
			android:textSize="16dip"
			android:layout_height="wrap_content"
			android:textStyle="bold"
			android:typeface="serif"
			android:layout_width="200dip"
			android:layout_toRightOf="@+id/tv_1"
			android:text="Countries">
		</TextView>

	</RelativeLayout>
	<ListView
 		android:id="@+id/lv_country"
 		android:layout_height="wrap_content"
 		android:layout_width="fill_parent"
 		android:cacheColorHint="#00000000">
 	</ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<!--two_col_row.xml-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content"
	android:gravity="left|center"
 	android:layout_width="wrap_content"
 	android:paddingBottom="10px"
 	android:paddingTop="10px"
 	android:paddingLeft="3px">
	 <TextView
	 	android:id="@+id/TextView01"
	 	android:layout_width="70dip"
	 	android:layout_height="wrap_content"
	 	android:gravity="left"
	 	android:textSize="15dip"
	 	android:textStyle="bold"
	 	android:textColor="#d08021"
		android:paddingLeft="20dip">
	 </TextView>
	 <TextView
	 	android:id="@+id/TextView02"
	 	android:layout_width="200dip"
	 	android:layout_height="wrap_content"
	 	android:gravity="left"
	 	android:layout_marginLeft="10dip"
	 	android:textSize="15dip"
	 	android:textStyle="bold"
	 	android:textColor="#7f0000">
	 </TextView>

</LinearLayout>

The package explorer will look like this:

Output of the example looks like this:

This example can be very much helpful, when you are creating a list from the data from database.

//CountriesList.java

package com.dynalist;

public class CountriesList {
public static String[] countries = { “Afghanistan”, “Albania”, “Algeria”,
“American Samoa”, “Andorra”, “Angola”, “Anguilla”, “Antarctica”,
“Antigua and Barbuda”, “Argentina”, “Armenia”, “Aruba”,
“Australia”, “Austria”, “Azerbaijan”, “Bahamas”, “Bahrain”,
“Bangladesh”, “Barbados”, “Belarus”, “Belgium”, “Belize”, “Benin”,
“Bermuda”, “Bhutan”, “Bolivia”, “Bosnia and Herzegovina”,
“Botswana”, “Bouvet Island”, “Brazil”,
“British Indian Ocean Territory”, “Brunei Darussalam”, “Bulgaria”,
“Burkina Faso”, “Burundi”, “Cambodia”, “Cameroon”, “Canada”,
“Cape Verde”, “Cayman Islands”, “Central African Republic”, “Chad”,
“Chile”, “China”, “Christmas Island”, “Cocos (Keeling) Islands”,
“Colombia”, “Comoros”, “Congo”,
“Congo, the Democratic Republic of the”, “Cook Islands”,
“Costa Rica”, “Cote D’Ivoire”, “Croatia”, “Cuba”, “Cyprus”,
“Czech Republic”, “Denmark”, “Djibouti”, “Dominica”,
“Dominican Republic”, “Ecuador”, “Egypt”, “El Salvador”,
“Equatorial Guinea”, “Eritrea”, “Estonia”, “Ethiopia”,
“Falkland Islands (Malvinas)”, “Faroe Islands”, “Fiji”, “Finland”,
“France”, “French Guiana”, “French Polynesia”,
“French Southern Territories”, “Gabon”, “Gambia”, “Georgia”,
“Germany”, “Ghana”, “Gibraltar”, “Greece”, “Greenland”, “Grenada”,
“Guadeloupe”, “Guam”, “Guatemala”, “Guinea”, “Guinea-Bissau”,
“Guyana”, “Haiti”, “Heard Island and Mcdonald Islands”,
“Holy See (Vatican City State)”, “Honduras”, “Hong Kong”,
“Hungary”, “Iceland”, “India”, “Indonesia”,
“Iran, Islamic Republic of”, “Iraq”, “Ireland”, “Israel”, “Italy”,
“Jamaica”, “Japan”, “Jordan”, “Kazakhstan”, “Kenya”, “Kiribati”,
“Korea, Democratic People’s Republic of”, “Korea, Republic of”,
“Kuwait”, “Kyrgyzstan”, “Lao People’s Democratic Republic”,
“Latvia”, “Lebanon”, “Lesotho”, “Liberia”,
“Libyan Arab Jamahiriya”, “Liechtenstein”, “Lithuania”,
“Luxembourg”, “Macao”,
“Macedonia, the Former Yugoslav Republic of”, “Madagascar”,
“Malawi”, “Malaysia”, “Maldives”, “Mali”, “Malta”,
“Marshall Islands”, “Martinique”, “Mauritania”, “Mauritius”,
“Mayotte”, “Mexico”, “Micronesia, Federated States of”,
“Moldova, Republic of”, “Monaco”, “Mongolia”, “Montserrat”,
“Morocco”, “Mozambique”, “Myanmar”, “Namibia”, “Nauru”, “Nepal”,
“Netherlands”, “Netherlands Antilles”, “New Caledonia”,
“New Zealand”, “Nicaragua”, “Niger”, “Nigeria”, “Niue”,
“Norfolk Island”, “Northern Mariana Islands”, “Norway”, “Oman”,
“Pakistan”, “Palau”, “Palestinian Territory, Occupied”, “Panama”,
“Papua New Guinea”, “Paraguay”, “Peru”, “Philippines”, “Pitcairn”,
“Poland”, “Portugal”, “Puerto Rico”, “Qatar”, “Reunion”, “Romania”,
“Russian Federation”, “Rwanda”, “Saint Helena”,
“Saint Kitts and Nevis”, “Saint Lucia”,
“Saint Pierre and Miquelon”, “Saint Vincent and the Grenadines”,
“Samoa”, “San Marino”, “Sao Tome and Principe”, “Saudi Arabia”,
“Senegal”, “Serbia and Montenegro”, “Seychelles”, “Sierra Leone”,
“Singapore”, “Slovakia”, “Slovenia”, “Solomon Islands”, “Somalia”,
“South Africa”, “South Georgia and the South Sandwich Islands”,
“Spain”, “Sri Lanka”, “Sudan”, “Suriname”,
“Svalbard and Jan Mayen”, “Swaziland”, “Sweden”, “Switzerland”,
“Syrian Arab Republic”, “Taiwan, Province of China”, “Tajikistan”,
“Tanzania, United Republic of”, “Thailand”, “Timor-Leste”, “Togo”,
“Tokelau”, “Tonga”, “Trinidad and Tobago”, “Tunisia”, “Turkey”,
“Turkmenistan”, “Turks and Caicos Islands”, “Tuvalu”, “Uganda”,
“Ukraine”, “United Arab Emirates”, “United Kingdom”,
“United States”, “United States Minor Outlying Islands”, “Uruguay”,
“Uzbekistan”, “Vanuatu”, “Venezuela”, “Viet Nam”,
“Virgin Islands, British”, “Virgin Islands, U.s.”,
“Wallis and Futuna”, “Western Sahara”, “Yemen”, “Zambia”,
“Zimbabwe” };
public static String[] abbreviations = { “AF”, “AL”, “DZ”, “AS”, “AD”, “AO”, “AI”, “AQ”,
“AG”, “AR”, “AM”, “AW”, “AU”, “AT”, “AZ”, “BS”, “BH”, “BD”, “BB”,
“BY”, “BE”, “BZ”, “BJ”, “BM”, “BT”, “BO”, “BA”, “BW”, “BV”, “BR”,
“IO”, “BN”, “BG”, “BF”, “BI”, “KH”, “CM”, “CA”, “CV”, “KY”, “CF”,
“TD”, “CL”, “CN”, “CX”, “CC”, “CO”, “KM”, “CG”, “CD”, “CK”, “CR”,
“CI”, “HR”, “CU”, “CY”, “CZ”, “DK”, “DJ”, “DM”, “DO”, “EC”, “EG”,
“SV”, “GQ”, “ER”, “EE”, “ET”, “FK”, “FO”, “FJ”, “FI”, “FR”, “GF”,
“PF”, “TF”, “GA”, “GM”, “GE”, “DE”, “GH”, “GI”, “GR”, “GL”, “GD”,
“GP”, “GU”, “GT”, “GN”, “GW”, “GY”, “HT”, “HM”, “VA”, “HN”, “HK”,
“HU”, “IS”, “IN”, “ID”, “IR”, “IQ”, “IE”, “IL”, “IT”, “JM”, “JP”,
“JO”, “KZ”, “KE”, “KI”, “KP”, “KR”, “KW”, “KG”, “LA”, “LV”, “LB”,
“LS”, “LR”, “LY”, “LI”, “LT”, “LU”, “MO”, “MK”, “MG”, “MW”, “MY”,
“MV”, “ML”, “MT”, “MH”, “MQ”, “MR”, “MU”, “YT”, “MX”, “FM”, “MD”,
“MC”, “MN”, “MS”, “MA”, “MZ”, “MM”, “NA”, “NR”, “NP”, “NL”, “AN”,
“NC”, “NZ”, “NI”, “NE”, “NG”, “NU”, “NF”, “MP”, “NO”, “OM”, “PK”,
“PW”, “PS”, “PA”, “PG”, “PY”, “PE”, “PH”, “PN”, “PL”, “PT”, “PR”,
“QA”, “RE”, “RO”, “RU”, “RW”, “SH”, “KN”, “LC”, “PM”, “VC”, “WS”,
“SM”, “ST”, “SA”, “SN”, “CS”, “SC”, “SL”, “SG”, “SK”, “SI”, “SB”,
“SO”, “ZA”, “GS”, “ES”, “LK”, “SD”, “SR”, “SJ”, “SZ”, “SE”, “CH”,
“SY”, “TW”, “TJ”, “TZ”, “TH”, “TL”, “TG”, “TK”, “TO”, “TT”, “TN”,
“TR”, “TM”, “TC”, “TV”, “UG”, “UA”, “AE”, “GB”, “US”, “UM”, “UY”,
“UZ”, “VU”, “VE”, “VN”, “VG”, “VI”, “WF”, “EH”, “YE”, “ZM”, “ZW” };
}

About these ads

32 Responses to Dynamic ListView in Android

  1. Nasurudeen MK says:

    Hi prashant,

    Thanks, its working, now i need click handler for this list. pls help me to find out.

    • Prashant says:

      Hi Nasurudeen, You are welcome.
      I hope this will help you for click event.

      l1 = (ListView) findViewById(R.id.ListView01);
      l1.setAdapter(new EfficientAdapter(this));
      l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView a, View v, int position,
      long id) {
      //use position to get clicked position
      //your code goes here
      }
      });

      • gaurav kumar says:

        hi
        I want to extract string while applying onItemClick()
        means If I click “Antarctica” I want to get String ” AQ Antarctica”
        plz help

      • Prashant says:

        Create two array with pair value in same index, i.e. “Antarctica” is in 5th index in one array means, “AQ antarctica” in 5th index of another array
        then on click event

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView a, View v, int position,
        long id) {
        String contitent = continents[Integer.parseInt(list.getItemAtPosition(
        position).toString())];
        }
        });

  2. hsynt says:

    it doesnt work i copied all of this classes and xml files into new project.

  3. hsynt says:

    Can you send me the source project to my mail adress please

  4. hsynt says:

    thats my email adres batuhan_erkant@hotmail.com

  5. hsynt says:

    i have to set item id.how to fix it ??

  6. hsynt says:

    i am getting nullpointer exception on this line

    holder.sefer_saati.setText(sefer[0]);
    but sefer[0] is not null what is that

  7. hsynt says:

    I worked my project on debug mode (Suspend(exception NullPointerException))
    -String(bilet$sefer_tablosu).getView(int,View,ViewGroup)line :67

    that”s line 67
    holder.sefer_saati.setText(sefer[0]);

  8. hsynt says:

    i chanced it like
    holder.sefer_saati.setText(“ssdsad”);
    i got same error

    • Prashant says:

      if you will notice on the code, I will be adding array to the holder.
      So create an array of the ids and then add it to the holder, i think that may help you.

  9. hsynt says:

    it is working sometime but generally giving error

  10. hsynt says:

    how can i do it
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
    convertView = tablo_ekleyici.inflate(R.layout.sefer_tablo, null);
    holder = new ViewHolder();
    holder.sefer_saati = (TextView) convertView.findViewById(R.id.sefer_saati);
    holder.sefer_guzergahi = (TextView) convertView.findViewById(R.id.sefer_guzergahi);
    holder.otobus_ozellik = (TextView) convertView.findViewById(R.id.otobus_ozellik);
    holder.bilet_fiyat= (TextView) convertView.findViewById(R.id.bilet_fiyat);

    convertView.setTag(holder);
    } else {
    holder = (ViewHolder) convertView.getTag();
    }
    String sefer[];
    sefer=SeferBilgileri.get(position).split(“!”);

    holder.sefer_saati.setText(sefer[0]);
    holder.sefer_guzergahi.setText(sefer[1]);
    holder.otobus_ozellik.setText(sefer[2]);
    holder.bilet_fiyat.setText(sefer[3]);
    return convertView;
    }

  11. hsynt says:

    private static final ArrayList SeferBilgileri = new ArrayList();

    that’s my arraylist

  12. hsynt says:

    i solve my problem notifyDataSetChanged():
    i added this line before return convertvies its fixed

  13. justinkamp says:

    Wow..its a great information providing by you..

  14. heepie says:

    hi prashant i had used ur example here as guide for me to develop my app, i would like to delete item from the listview but its not deleting. it passes through the line but not deleting it from the list. could you help on this.

  15. ghouse says:

    everybody i am creating a custom listview with dynamic radiobuttons adding to radiogroup upto that i am getting what i want but when i try to select one radio button in first row then automatically first button in 5th ,9th,13 row is getting selected and when i select any button in second row then same button in 6th,8th,12th row is getting selected what i am doing wrong here and my adapter class

    public class InteractiveArrayAdapter extends ArrayAdapter {
    String tag = “Events”;
    private final List list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
    }

    static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox,checkbox1;
    protected RadioGroup mgroup;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    Log.d(tag,” 3″);
    View view = null;
    if (convertView == null) {
    LayoutInflater inflator = context.getLayoutInflater();
    view = inflator.inflate(R.layout.rowbuttonlayout, null);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.text = (TextView) view.findViewById(R.id.label);
    //viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
    //viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
    viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
    final RadioButton[] mbutton=new RadioButton[5];
    for(int l=0;l<5;l++){
    mbutton[l]=new RadioButton(context);
    mbutton[l].setText("test"+l);

    viewHolder.mgroup.addView(mbutton[l]);

    }
    viewHolder.mgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
    for(int i=0; i<mRadioGroup.getChildCount(); i++) {
    RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
    //int t=table.indexOfChild(table_row);
    //System.out.println(t);
    int t=mRadioGroup.getId();
    System.out.println(t);

    if(btn.getId() == checkedId) {
    String text = btn.getText().toString();
    // do something with text
    Log.d(text," event1");
    return;
    }
    }
    }
    });

    view.setTag(viewHolder);
    Log.d(tag,"me");

    } else {
    view = convertView;
    Log.d(tag,"meeee");

    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());

    Log.d(tag," event7");

    return view;

    }

  16. Krishnaveni says:

    Thank u for ur code….
    my requirement is to search the countries using an edit text.
    while we are entering any text into the edit text then the countries starting with that text should be displayed automatically…….
    can u help me out with this

  17. iamabooger says:

    hey thanks for the code! i want to add to the list using an Edittext and a button in the layout.. how do i set the adapter to the text i want to insert in the list??

  18. srdr says:

    How can i getting from xml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 182 other followers

%d bloggers like this: