的Android列表視圖我有它實現列表視圖與列表中的每個行中的旋轉器和單選按鈕定製陣列適配器。選擇單選按鈕時,每個列表單選按鈕被選中。如何使用單選列表行進行選擇。與旋轉器和單選按鈕
如何使用單選按鈕,選擇只有一個列表。
對不起,英語不好。
這是代碼。
getView():
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView == null)
{
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.single_list_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.data = new DataModel(myContext);
viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1);
viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice);
viewHolder.radioBtn.setTag(position);
//viewHolder.radioBtn.setChecked(selectedPos == position);
viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
viewHolder.data.setSelected(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
viewHolder.radioBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
selectedPos = (Integer)view.getTag();
}
});
view.setTag(viewHolder);
}
else
{
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.spin.setSelection(getItem(position).getSelected());
holder.radioBtn.setChecked(selectedPos == position);
return view;
}
main_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search list.."
android:inputType="textVisiblePassword"/>
<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cacheColorHint="#f8f8ff"
android:divider="#000000"
android:fadeScrollbars="true"
android:choiceMode="singleChoice"
android:descendantFocusability="beforeDescendants"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:textFilterEnabled="true"
android:textStyle="normal" />
</LinearLayout>
single_row_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:gravity="center_vertical">
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/options"
android:prompt="@string/spinner_prompt" />
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/choice"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:checked="false"/>
</LinearLayout>
</RelativeLayout>
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) this.findViewById(R.id.listview1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
DataModel data = new DataModel(this);
DataModel data1 = new DataModel(this);
DataModel data2 = new DataModel(this);
DataModel data3 = new DataModel(this);
DataModel data4 = new DataModel(this);
d = new ListAdapter(this, R.layout.single_list_row, new DataModel[] { data, data1, data2, data3, data4 });
lv.setAdapter(d);
}
的DataModel類
public class DataModel {
private int selected;
private ArrayAdapter<CharSequence> adapter;
public DataModel(Context parent){
adapter = ArrayAdapter.createFromResource(parent, R.array.options, android.R.layout.simple_spinner_item);
sadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter() { return adapter; }
public int getSelected() { return selected; }
public void setSelected(int selected) { this.selected = selected; }
}
完整的適配器類別:
public class ListAdapter extends ArrayAdapter<DataModel>
{
private Activity myContext;
int selectedPos = 0;
int flag = 0;
int pos = 0;
public ListAdapter(Activity context, int textViewResourceId, DataModel[] objects)
{
super(context, textViewResourceId, objects);
myContext = context;
}
static class ViewHolder
{
protected DataModel data;
protected Spinner spin;
protected RadioButton radioBtn;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
final ViewHolder viewHolder;
if (view == null)
{
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.single_list_row, null);
viewHolder = new ViewHolder();
viewHolder.data = new DataModel(myContext);
viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1);
viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice);
viewHolder.radioBtn.setTag(position);
if(selectedPos == position)
{
viewHolder.radioBtn.setChecked(true);
}
else
{
viewHolder.radioBtn.setChecked(false);
}
view.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) view.getTag();
}
//viewHolder.data = new DataModel(myContext);
//viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
viewHolder.data.setSelected(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
viewHolder.radioBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
selectedPos = (Integer)view.getTag();
}
});
viewHolder.spin.setSelection(getItem(position).getSelected());
viewHolder.radioBtn.setChecked(selectedPos == position);
view.setTag(viewHolder);
return view;
}
}
你能跟着我的答案,看看那是怎麼回事嗎? –