简体   繁体   English

无法找出正确的参数形式-从Java的静态嵌套类中调用方法

[英]Can't figure out correct argument form - calling a method from within a static nested class in Java

I'm testing code copied from a Java Android examples site. 我正在测试从Java Android示例站点复制的代码。 I'm trying to modify it to start a new activity when a user clicks into a row of the current activity. 我正在尝试对其进行修改,以在用户单击当前活动的行时启动一个新活动。 So I'm using the Intent method but I'm not able to figure out how to reference the current View instance argument to the Intent method. 所以我正在使用Intent方法,但无法弄清楚如何将当前View实例参数引用到Intent方法。

I've tried dozens of combinations, and spent 2 days researching. 我尝试了数十种组合,并花了2天的时间进行研究。 This must seem basic to many of you I know, but my apologies, this is week #2 for me learning Java, Eclipse and the Android SDK (target= API 8) 这对我认识的许多人来说似乎必须是基本的,但是我很抱歉,这是我学习Java,Eclipse和Android SDK的第二周(target = API 8)

    public class CustomListViewDemo extends ListActivity {
private EfficientAdapter adap;
private static String[] data = new String[] { "0", "1" };
private static String[] TitleString = new String[] { "Title1", "Title2" };
private static String[] DetailString = new String[] { "Detail1", "Detail2" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    adap = new EfficientAdapter(this);
    setListAdapter(adap);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Toast.makeText(this, "Click-" + String.valueOf(position),
            Toast.LENGTH_SHORT).show();
}

public static class EfficientAdapter extends BaseAdapter implements
        Filterable {
    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Context context;

    public EfficientAdapter(Context context) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    /**
     * Make a view to hold each row.
     * 
     */
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adaptor_content, null);

            holder = new ViewHolder();
            holder.textLine = (TextView) convertView
                    .findViewById(R.id.textLine);
            holder.buttonLine = (Button) convertView
                    .findViewById(R.id.buttonLine);
            holder.DbuttonLine = (Button) convertView
                    .findViewById(R.id.DbuttonLine);
            holder.textLine2 = (TextView) convertView
                    .findViewById(R.id.textLine2);

            convertView.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    // Toast.makeText(context, "Click-" +
                    // String.valueOf(pos),
                    // Toast.LENGTH_SHORT).show();

        // ******************** ERROR IS LINE BELOW *********
                    // "No enclosing instance of the type CustomListViewDemo is accessible in scope"
                    Intent i = new Intent(CustomListViewDemo.this, IntentA.class);
                    startActivity(i);

                }
            });


            holder.buttonLine.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    Toast.makeText(context,
                            "Delete-" + String.valueOf(pos),
                            Toast.LENGTH_SHORT).show();

                }
            });

            holder.DbuttonLine.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    Toast.makeText(context,
                            "Details-" + String.valueOf(pos),
                            Toast.LENGTH_SHORT).show();

                }
            });

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textLine.setText(TitleString[position]
                + String.valueOf(position));
        holder.textLine2.setText(DetailString[position]
                + String.valueOf(position));

        return convertView;
    }

    static class ViewHolder {
        TextView textLine;
        TextView textLine2;
        Button buttonLine;
        Button DbuttonLine;

    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data[position];
    }

}

} }

I have seen many examples about how to refer to outer members of nested classes but not found a good example on find the view instance of an outer class for use as a method argument. 我已经看到了许多有关如何引用嵌套类的外部成员的示例,但是找不到有关用作方法参数的外部类的视图实例的好示例。 Can anyone point me in the right direction? 谁能指出我正确的方向?

CustomListViewDemo.this

For this to work, you need an instance. 对于this工作,你需要一个实例。

In a static nested class, there is no outer instance. 在静态嵌套类中,没有外部实例。

You have to either make the class "non-static", or explicitly pass a reference to a CustomListViewDemo instance around that you want to use here. 您必须使类成为“非静态”类,或者将引用显式传递给要在此处使用的CustomListViewDemo实例。

If you look at the docs, the constructor you're invoking ( new Intent(CustomListViewDemo.this, IntentA.class) ), is this one: 如果您看一下文档,正在调用的构造函数( new Intent(CustomListViewDemo.this, IntentA.class) )就是这个:

public Intent (Context packageContext, Class<?> cls)

Since you're already storing a Context, you can fix your problem by using new Intent(this.context, IntentA.class) instead. 由于已经存储了一个Context,因此可以通过使用new Intent(this.context, IntentA.class)来解决问题。

EfficientAdapter is a static class so you don't necessarily have an instance of CustomListViewDemo that you can use yet. EfficientAdapter是一个静态类,因此您不一定有可以使用的CustomListViewDemo实例。 Static implies that the class can be used without an instance, hence your error 静态表示可以在没有实例的情况下使用该类,因此会出现错误

"No enclosing instance of the type CustomListViewDemo is accessible in scope" 

You have two options here. 您在这里有两个选择。

1) Go with dmon's suggestion and use the context you have stored: 1)遵循dmon的建议并使用您存储的上下文:

Intent i = new Intent(context, IntentA.class);

2) Do not make EfficientAdapter a static class (what is the reason for having it static?) 2)不要将EfficientAdapter静态类(将其设为静态的原因是什么?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM