Android 更改阵列适配器时出现错误<;字符串>;到扩展ArrayAdapter的自定义适配器<;字符串>;

Android 更改阵列适配器时出现错误<;字符串>;到扩展ArrayAdapter的自定义适配器<;字符串>;,android,android-fragments,android-adapter,Android,Android Fragments,Android Adapter,基本上我得到的是一个显示ListView的片段。它当前使用ArrayAdapter。不过,我正在尝试扩展ArrayAdapter以创建自己的自定义适配器。然后,当我更改代码以使用新适配器时,出现以下错误: “无法访问MyActivity类型的封闭实例。必须使用MyActivity类型的封闭实例限定分配(例如,x.new A(),其中x是MyActivity的实例)。” 代码如下:注意,这些都嵌套在MyActivity中 public static class MyFragment extends

基本上我得到的是一个显示ListView的片段。它当前使用ArrayAdapter。不过,我正在尝试扩展ArrayAdapter以创建自己的自定义适配器。然后,当我更改代码以使用新适配器时,出现以下错误:

“无法访问MyActivity类型的封闭实例。必须使用MyActivity类型的封闭实例限定分配(例如,x.new A(),其中x是MyActivity的实例)。”

代码如下:注意,这些都嵌套在MyActivity中

public static class MyFragment extends ListFragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public MyFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fixed_tab, container, false);

        // Temporarily get the content from an array
        String [] values = new String[] { "Item1", "Item2", "Item3", "Item4" };

        /******** This has no error as it is, but if I change it to CustomListAdapter, it shows the error ********/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.workout_row, R.id.workout_name, values); 
        setListAdapter(adapter);

        return rootView;
    }
}

private class CustomListAdapter extends ArrayAdapter<String> {
    String[] list;
    public CustomListAdapter(Context context, int resource,
            int textViewResourceId, String[] array) {
        super(context, resource, textViewResourceId, array);
        list = array;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
        cb.setTag(position);

        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    // TBI
                }

            }
        });

        return row;
    }
}
公共静态类MyFragment扩展了ListFragment{
/**
*表示此文件节号的片段参数
*碎片。
*/
公共静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
公共MyFragment(){
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.fragment\u fixed\u选项卡,容器,false);
//临时从数组中获取内容
字符串[]值=新字符串[]{“Item1”、“Item2”、“Item3”、“Item4”};
/********这没有错误,但如果我将其更改为CustomListAdapter,它会显示错误********/
ArrayAdapter=新的ArrayAdapter(this.getActivity(),R.layout.workout_行,R.id.workout_名称,值);
setListAdapter(适配器);
返回rootView;
}
}
私有类CustomListAdapter扩展了ArrayAdapter{
字符串[]列表;
公共CustomListAdapter(上下文、int资源、,
int textViewResourceId,字符串[]数组){
super(上下文、资源、textViewResourceId、数组);
列表=数组;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=super.getView(位置、转换视图、父级);
复选框cb=(复选框)findViewById(R.id.CheckBox);
cb.设置标签(位置);
cb.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,
布尔值(已检查){
如果(已检查){
//TBI
}
}
});
返回行;
}
}

您的适配器需要在
MyFragment
类中,当前它在类括号外

您的适配器需要在
MyFragment
类中,当前它在类括号外

创建一个单独的.java文件作为CustomListAdapter.java并将适配器代码复制到那里

在CustomListAdapter的构造函数中

 LayoutInflater inflater;
 public CustomListAdapter(Context context, int resource,
        int textViewResourceId, String[] array) {
    super(context, resource, textViewResourceId, array);
    list = array;
    inflater = LayoutInlfater.from(context); 
}
getView

View row = inflater.inflate(R.layout.workout_row,parent,null); // inflate custom layout
CheckBox cb = (CheckBox) row.findViewById(R.id.checkbox);
// use the inflated view object to initialize checkbox
此外,最好使用ViewHolder图案


创建一个单独的.java文件CustomListAdapter.java,并将适配器代码复制到那里

在CustomListAdapter的构造函数中

 LayoutInflater inflater;
 public CustomListAdapter(Context context, int resource,
        int textViewResourceId, String[] array) {
    super(context, resource, textViewResourceId, array);
    list = array;
    inflater = LayoutInlfater.from(context); 
}
getView

View row = inflater.inflate(R.layout.workout_row,parent,null); // inflate custom layout
CheckBox cb = (CheckBox) row.findViewById(R.id.checkbox);
// use the inflated view object to initialize checkbox
此外,最好使用ViewHolder图案


MyFragment
类中创建
CustomListAdapter
类,或者作为单独的类,而不是在同一文件中创建两个类。另外,getView实现错误。我正在尝试此操作,但如果适配器嵌套在片段中,findViewById将无法工作。另外,请详细说明我的getView是如何出错的?在
MyFragment
类中创建
CustomListAdapter
类,或者作为单独的类,而不是在同一文件中创建两个类。getView实现也是错误的。我正在尝试此操作,但是如果适配器嵌套在片段中,findViewById将不起作用。另外,请详细说明我的getView是如何错误的?