Флажок в списке с пользовательской привязкой SimpleCurser
Я думаю, что ive попробовал все (сделал focusable: false!!)
и я не могу захватить в любом случае выбранный флажок в элементе списка.
даже OnItemClickListener, не отвечает на любой щелчок.
как я могу получить отмеченные флажки в элементе списка?
мой элемент списка inclused: просмотр изображения, 4 текстовых просмотров и флажок
некоторый код:
это в моем классе ListActivity:
final String columns[] = new String[] { MyUsers.User._ID,
MyUsers.User.MSG, MyUsers.User.LOCATION };
int[] to = new int[] { R.id.toptext, R.id.bottomtext,R.id.ChkBox, R.id.Location};
Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users");
Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);
startManagingCursor(cursor);
ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this,
R.layout.listitem, cursor, columns, to);
this.setListAdapter(myCursorAdapter);
и это мой класс адаптеров Custom Cursor:
public class ListCursorAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public ListCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c)
{
TextView topText = (TextView) v.findViewById(R.id.toptext);
if (topText != null)
{
topText.setText("");
}
int nameCol = c.getColumnIndex(MyUsers.User.MSG);
String name = c.getString(nameCol);
TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext);
if (buttomTxt != null)
{
buttomTxt.setText("Message: "+name);
}
nameCol = c.getColumnIndex(MyUsers.User.LOCATION);
name = c.getString(nameCol);
TextView location = (TextView) v.findViewById(R.id.Location);
if (locationLinkTxt != null)
{
locationLinkTxt.setText(name);
}
Буду признателен за любую помощь! это действительно удручающе, я пробовал много способов. многие слушатели, не могут понять, как захватить прослушиватель для моих флажков.
Данные, которые я привязываю к элементам списка из базы данных, не оговариваются относительно флажка. Только текстовые просмотры.. между базой данных и флажком у меня нет элемента списка.
Спасибо,
Идан.
<ImageView
android:id="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="6dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/icon" >
</ImageView>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="1sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<TextView
android:id="@+id/toptext"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:singleLine="true"
android:text="OrderNum" >
</TextView>
<TextView
android:id="@+id/bottomtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="TweetMsg" >
</TextView>
<TextView
android:id="@+id/twittLocation"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="location" >
</TextView>
<TextView
android:id="@+id/twittLocationlink"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="fill_horizontal"
android:text="locationlink" >
</TextView>
</LinearLayout>
<CheckBox
android:id="@+id/deleteTwittChkBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="2dp"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Delete" >
</CheckBox>
Ответы
Ответ 1
Я бы порекомендовал вам использовать встроенную поддержку Android для списков с несколькими вариантами выбора (CHOICE_MODE_MULTIPLE
).
List11.java
Пример SDK демонстрирует это. Вы также можете найти проект из одного из моих учебников, который использует его здесь.
Вы все равно можете использовать эту технику в своем собственном макете, если вы включили CheckedTextView
с android:id="@android:id/text1"
, как показано в ресурсе android.R.layout.simple_list_item_multiple_choice
, копия которого поставляется с вашим SDK.
Ответ 2
http://www.coderanch.com/t/513608/Android/Mobile/Multiple-Checkboxes-ListView
проверьте этот пример, это очень хороший пример для настраиваемого списка с флажком.
Ответ 3
Я хочу также выбрать один элемент в списке (без CHOICE_MODE_MULTIPLE) и получить состояния флажков. В моем пользовательском ListAdapter я реализовал что-то вроде:
private void bindView(int position, View view) {
final CheckBox myCheckBox = (CheckBox)view.findViewById(R.id.my_checkbox);
final MyObject myModelObject = (MyObject)getItem(position);
myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myModelObject.setSelected(isChecked);
}
});
И в прослушивателе onItemClick() ListView я могу получить состояние флажков с помощью:
((MyObject)listView.getAdapter().getItem(n)).isSelected()