2012-03-06 8 views
0

私は自分のカスタムアダプタを作成しています。すべてのリスト項目の前にラジオボタンを置く必要があります。私はこれを使用します:ラジオボタン付きカスタムアダプタ

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; 

    } 

ラジオボタンを追加する方法は誰にでも分かります。

+0

[質問] [1]の回答を確認してください。あなたと同じ。 [1]:http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-a-custom-row-layout –

答えて

0

まず、リスト項目のカスタムレイアウトを作成し、getView()メソッドでLayoutInflaterを使用します。

LayoutInflaterレイアウトXMLファイルを対応する Viewオブジェクトにインスタンス化します。直接使用されることはありません。代わりに、 getLayoutInflater()またはgetSystemService(String)を使用して、現在の コンテキストにフックされ、実行中のデバイス用に正しく設定されている標準の LayoutInflaterインスタンスを取得します。

良い例がhereであり、あなたがit.A簡単なスニペットで基礎を見ることができるがここにある:

… 
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; 
     } 

    } 

はリファレンス:
developer.android

1

この

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

を試してみてくださいパブリッククラスMyArrayAdapter extends ArrayAdapter { コンテキスト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; 

} 
関連する問題