我创建了一个列表视图,并在我的自定义对话框中实现了该列表视图。列表视图使用数组适配器,在我的数组适配器中,我使用自己的布局和所需的颜色。代码如下。
listView = new ListView(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
listView.setAdapter(adapter);
在这里,点击我的列表项的监听器工作正常。
问题现在开始了。我的自定义警报对话框中需要一个列表视图,每行包含一个单选按钮。我使用相同的方法。这是我的代码。
listView = new ListView(context);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,R.layout.my_single_choice_layout, choice);
listView.setAdapter(adapter);
这里可以同时检查所有radioButtons。我的听众工作不正常。
my_spinner_layout_xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
style="@style/ListItemTextColor"
/>
和my_single_choice_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/my_choice_radio"
android:layout_height="match_parent"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Option"
style="@style/ListItemTextColor" >
</RadioButton>
答案 0 :(得分:5)
试试这个:
list.setAdapter(new EfficientAdapter(context,R.layout.my_single_choice_layout, choice));
然后创建一个类
public class EfficientAdapter extends ArrayAdapter {
private LayoutInflater mInflater;
private String[] mStrings;
private int mViewResourceId;
public EfficientAdapter(Context ctx, int viewResourceId,String[] strings) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
convertView.setMinimumHeight(132);
TextView tv = (TextView)convertView.findViewById(R.id.option_text); //Give Id to your textview
tv.setText(mStrings[position]);
tv.setTextColor(Color.RED);
RadioButtons r=(RadioButtons)convertview.findviewById(Radio button id);
r.setOnCheckedListener(new ur listener()
{
/////////Do whatever you wanna do overhere
});
return convertView;
}
}
希望它有所帮助。
答案 1 :(得分:0)
您必须使用自定义适配器。
jst参考的示例代码,它不是完整的实现
class Myadapter extends ArrayAdapter<String>{
LayoutInflater inflater=null;
public Myadapter(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
inflater = (LayoutInflater)getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if(convertView==null){
row= inflater.inflate(R.layout.activity_list_item, null);
RadioButton rb = row.findViewById(R.id.radiobtn);
rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//here write your code for radio button event
}
});
}
return row;
}
}