Is not accessible in current context

后端 未结 4 1145
猫巷女王i
猫巷女王i 2021-02-05 04:58

I have the following code

public abstract class BaseAdapter extends ArrayAdapter {
    public BaseAdapter(Con         


        
4条回答
  •  北海茫月
    2021-02-05 05:40

    Happened the same to me when extending a Base class. But this time I had it like this (following the given example in the question)

    BaseAdapter.java

    public abstract class BaseAdapter extends ArrayAdapter {
        public BaseAdapter(Context context, int resource, Collection collection) {
            // typical constructor logic
        }
    
        // some other custom defined methods
    
        public static class ViewHolder {
            // custom defined logic
        }
    }
    

    ModelAdapter.java

    import com.mypackage.ModelAdapter.ModelViewHolder; ***//NOTE THIS IMPORT!!!***
    
    public class ModelAdapter extends BaseAdapter {
        public ModelAdapter(Context context, int resource, Collection collection) {
           super(context, resource, collection);
           // typical constructor logic
        }
    
        public static class ModelViewHolder extends ViewHolder {
            // custom defined logic
        }
    }
    

    That way the warning was gone and didnt need to separate ModelViewHolder into another '.java' file. Notice that they are in two different files and the import in ModelAdapter.java.

    I think Fei Liang's answer is partially incorrect because being ModelViewHolder static should make possible to initialize ModelViewHolder without initializing its parent class ModelAdapter

提交回复
热议问题