0

I'm creating my own custom adapter. I have to put Radio button in front of every list item. I use this:

List<Address> result = myGeocoder.getFromLocationName(name,
                MAX_RESULT);


public class MyArrayAdapter extends ArrayAdapter<Address> {
    Context mycontext;

    public MyArrayAdapter(Context context, int textViewResourceId,
            List<Address> objects) {
        super(context, textViewResourceId, objects);            
        mycontext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int maxAddressLineIndex = getItem(position)
                .getMaxAddressLineIndex();
        String addressLine = "";
        for (int j = 0; j <= maxAddressLineIndex; j++) {
            addressLine += getItem(position).getAddressLine(j) + ",";
        }

        TextView rowAddress = new TextView(mycontext);
        rowAddress.setText(addressLine + "\n");

        return rowAddress;

    }

Can anybody tell how can I add Radio button.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vijay Bagul
  • 237
  • 7
  • 18
  • Check the answer of this [Question][1]. Same as you. [1]: http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-a-custom-row-layout – Pascal Piché Mar 06 '12 at 05:04

2 Answers2

1

try this

List<Address> result = myGeocoder.getFromLocationName(name,
            MAX_RESULT);

public class MyArrayAdapter extends ArrayAdapter { Context mycontext;

public MyArrayAdapter(Context context, int textViewResourceId,
        List<Address> objects) {
    super(context, textViewResourceId, objects);            
    mycontext = context;
}

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    int maxAddressLineIndex = getItem(position)
            .getMaxAddressLineIndex();
    String addressLine = "";
    for (int j = 0; j <= maxAddressLineIndex; j++) {
        addressLine += getItem(position).getAddressLine(j) + ",";
    }
    LinearLayout lyt = new LinearLayout(mycontext);
    TextView rowAddress = new TextView(mycontext);
    rowAddress.setText(addressLine + "\n");
    RadioButton rdo = new RadioButton(mycontext);

    lyt.addView(rowAddress);
    lyt.addView(rdo);
    return lyt;

}
Sunil_Suthar
  • 1,094
  • 1
  • 10
  • 20
0

First you would to create a custom layout for list items,then use LayoutInflater in your getView() method.

LayoutInflater Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on.

A good example is here and you can see fundamentals in it.A simple snippet is here:

…
private LayoutInflater mInflater;
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
…

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listItem, null);
                …
            } else {
                …
            }
            …
            return convertView;
        }

    }

Reference:
developer.android

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167