Я пытаюсь создать список SingleSelect, расширяя ListView с помощью пользовательской строки, как
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
>
<CheckBox
android:id="@+id/chk"
android:button="@drawable/q_list_check_box"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/txtChoice"
android:textColor="@color/white"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
и с кодом для SingleSelectList, таким как
public class SingleList extends ListView {
int selected;
List<String> data;
public SingleList(Context context) {
super(context);
this.selected = -1;
this.data = new ArrayList<String>();
}
public SingleList(Context context, AttributeSet attributes) {
super(context, attributes);
this.selected = -1;
this.data = new ArrayList<String>();
}
public int getSelectedIndex() {
return this.selected;
}
public void setSelectedIndex(int selected) {
this.selected = selected;
}
public List<String> getData() {
return this.data;
}
public void setData(List<String> data) {
this.data = data;
this.selected = -1;
}
public String getSelectedValue() {
if (this.selected > -1)
return this.data.get(selected);
return "";
}
public void addDataItem(String item) {
this.data.add(item);
}
public void initialize_list(List<String> data) {
setData(data);
this.setAdapter(new SingleListAdapter());
}
private class SingleListAdapter extends BaseAdapter {
public SingleListAdapter() {
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return data.get(position).hashCode();
}
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.q_list_row_text, null);
}
final CheckBox chkChoice = (CheckBox) view.findViewById(R.id.chk);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (selected != -1) {
View tempView = SingleListAdapter.this.getView(
selected, convertView, parent);
CheckBox tempChk = (CheckBox) tempView
.findViewById(R.id.chk);
tempChk.performClick();
}
chkChoice.performClick();
selected = position;
}
});
chkChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
((View) chkChoice.getParent())
.setBackgroundResource(R.drawable.backgroun_checked);
} else {
((View) chkChoice.getParent())
.setBackgroundResource(R.drawable.background_unchecked);
}
}
});
TextView txtChoice = (TextView) view.findViewById(R.id.txtChoice);
txtChoice.setText(data.get(position));
return view;
}
}
}
// int selected содержит текущий отмеченный элемент, и я хочу, чтобы строка щелчка сняла отметку с этой строки и проверила newone.
Может кто-нибудь подсказать мне, что я делаю не так?