1

我正在编写一个相当复杂的ListView,其中(除其他外)需要在每个列表项中格式化视图。

为了让我完全控制视图在每个列表项中的绑定方式,我CursorAdapter以这种方式进行子类化:

public class MyAdapter extends CursorAdapter {

    public final LayoutInflater mInflater;

    public MyAdapter(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ToggleButton tButton = (ToggleButton) view.findViewById(R.id.tbutton);
        tButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // start activity based on a pending intent
            }
        });     

    }
}

问题是我的ToggleButton点击监听器应该基于待处理的意图启动一个活动。未决意图在使用此自定义适配器的活动中实例化。

我知道我可以SimpleCursorAdapter在 main中使用 aActivity和 a ViewBinder,以便只需要从 main 启动意图Activity。但SimpleCursorAdapter不太正确,因为我没有将列直接映射到视图。

但是,我在这里的替代方案建议Activity从游标子类访问 main 的数据。我觉得必须有更好的方法来设计应用程序。

4

1 回答 1

0

从 API Demos 中汲取灵感——具体来说EfficientAdapter,我已将CursorAdaptersublcas 声明为我的活动的内部类。

这避免了在主要活动之外传递未决意图。

来源:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

于 2011-11-17T22:04:12.223 回答