0


所以我正在尝试制作一个自定义的SimpleCursorAdapter ,因为我想制作看起来像这样的列表行:文本视图 | ImageButton,我知道如果不制作自定义适配器就无法做到这一点。

问题是我的代码不起作用,我不确定为什么。即使没有要显示的数据,我也会得到默认格式的一行:
ToggleButton | “默认” | 图像按钮。

此外,显示的所有行看起来都与默认行完全相同,并且我设置的 OnClickListener 不执行任何操作。

有人可以告诉我我做错了什么,或者至少为我指出一个关于如何处理自定义 CursorAdapters 和 OnClickListeners 的体面教程的方向?因为我完全找不到任何有帮助的东西。

这是我的适配器代码:

public class AlarmCursorAdapter extends SimpleCursorAdapter {

private Cursor mCursor;
private Context mContext;
private Activity mActivity;

public AlarmCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to, int flags) {
    super(context, layout, c, from, to, flags);
    // TODO Auto-generated constructor stub
    mCursor = c;
    mContext = context;
    mActivity = (Activity) context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(view == null) {
        view = LayoutInflater.from(mContext).inflate(R.layout.alarm_list_row, parent, false);
    }
    if(mCursor == null || mCursor.getCount() == 0) {
        return view;
    }
    mCursor.moveToPosition(position);

    // Set the alarm time view
    TextView alarmView = (TextView) view.findViewById(R.id.alarmView);
    int timeStringIndex = mCursor.getColumnIndexOrThrow(DailyAlarmTable.ALARM_TIME);
    String alarmTime = mCursor.getString(timeStringIndex);
    alarmView.setText(alarmTime);

    // Set up the toggle button
    int isActiveIndex = mCursor.getColumnIndexOrThrow(DailyAlarmTable.ALARM_ISACTIVE);
    int isActive = mCursor.getInt(isActiveIndex);
    ToggleButton alarmToggle = (ToggleButton)view.findViewById(R.id.alarmToggle);
    if(isActive == 1) {
        alarmToggle.setChecked(true);
    } else {
        alarmToggle.setChecked(false);
    }
    final int currentPosition = mCursor.getPosition();
    int idIndex = mCursor.getColumnIndexOrThrow(DailyAlarmTable.ALARM_ID);
    final long alarmId = mCursor.getLong(idIndex);
    alarmToggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String toastStr = "clicked alarm " + alarmId + " at position " + currentPosition;
            Toast.makeText(mContext, toastStr, Toast.LENGTH_SHORT).show();
        }

    });

    return view;
}

}

这是实现,它发生在片段内:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    /*
    mAdapter = new SimpleCursorAdapter(getActivity(),
            R.layout.alarm_list_row, null,
            new String[] { DailyAlarmTable.ALARM_TIME, DailyAlarmTable.ALARM_ISACTIVE },
            new int[] { R.id.alarmView, R.id.alarmToggle }, 0);

    */
    mAdapter = new AlarmCursorAdapter(getActivity(),
            R.layout.alarm_list_row, null,
            new String[] { DailyAlarmTable.ALARM_TIME, DailyAlarmTable.ALARM_ISACTIVE },
            new int[] { R.id.alarmView, R.id.alarmToggle }, 0);

    //mAdapter.setViewBinder(new AlarmViewBinder());
    ListView alarmList = (ListView) this.getActivity().findViewById(R.id.alarmListView);
    TextView emptyView = (TextView) this.getActivity().findViewById(R.id.empty);
    alarmList.setEmptyView(emptyView);
    alarmList.setAdapter(mAdapter);

    // Initialize the loader
    getLoaderManager().initLoader(1, savedInstanceState, this);
}

这是该行的 XML 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ToggleButton
    android:id="@+id/alarmToggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="@string/details_default" />

<TextView
    android:id="@+id/alarmView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/details_default"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageButton
    android:id="@+id/alarmDiscard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_discard"
    android:contentDescription="@string/alarm_discard_description" />

</LinearLayout>

如果您需要任何其他代码,我很乐意添加。非常感谢您提前。

4

1 回答 1

0

正如 pskink 的评论所建议的,解决方案根本不使用自定义 SCA,而只是实现一个 View Binder。

于 2013-09-08T20:20:06.850 回答