你必须像这样创建适配器:
package com.example.test_all;
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.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView List;
String a[] = { "a", "b", "c", "d", "e" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List = (ListView) findViewById(R.id.listView1);
List.setAdapter(new ListViewAdapter(MainActivity.this));
}
public class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter(Context con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}
public int getCount() {
// TODO Auto-generated method stub
return a.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
// return product_id1.size();
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
// return product_id1.get(position).hashCode();
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.custom, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.textView1);
holder.rad = (RadioButton) v.findViewById(R.id.radioButton1);
// holder.total_rate.setOnClickListener(mOnTitleClickListener1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.name.setText("" + a[position]);
return v;
}
}
static class ListContent {
TextView name;
RadioButton rad;
}
}
自定义.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:weightSum="5"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
有关更多详细信息,请查看此博客
此链接可帮助您管理单选按钮选择(这里是复选框代码从该代码中获取想法并管理您的选择)