繁体   English   中英

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

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

我正在测试从Java Android示例站点复制的代码。 我正在尝试对其进行修改,以在用户单击当前活动的行时启动一个新活动。 所以我正在使用Intent方法,但无法弄清楚如何将当前View实例参数引用到Intent方法。

我尝试了数十种组合,并花了2天的时间进行研究。 这对我认识的许多人来说似乎必须是基本的,但是我很抱歉,这是我学习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];
    }

}

}

我已经看到了许多有关如何引用嵌套类的外部成员的示例,但是找不到有关用作方法参数的外部类的视图实例的好示例。 谁能指出我正确的方向?

CustomListViewDemo.this

对于this工作,你需要一个实例。

在静态嵌套类中,没有外部实例。

您必须使类成为“非静态”类,或者将引用显式传递给要在此处使用的CustomListViewDemo实例。

如果您看一下文档,正在调用的构造函数( new Intent(CustomListViewDemo.this, IntentA.class) )就是这个:

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

由于已经存储了一个Context,因此可以通过使用new Intent(this.context, IntentA.class)来解决问题。

EfficientAdapter是一个静态类,因此您不一定有可以使用的CustomListViewDemo实例。 静态表示可以在没有实例的情况下使用该类,因此会出现错误

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

您在这里有两个选择。

1)遵循dmon的建议并使用您存储的上下文:

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

2)不要将EfficientAdapter静态类(将其设为静态的原因是什么?)

暂无
暂无

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

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