0

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

我尝试了几十种组合,并花了 2 天时间研究。这对我认识的许多人来说似乎很基础,但我很抱歉,这是我学习 Java、Eclipse 和 Android SDK(目标 = API 8)的第 2 周

    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];
    }

}

}

我已经看过很多关于如何引用嵌套类的外部成员的示例,但没有找到一个很好的示例来查找外部类的视图实例以用作方法参数。谁能指出我正确的方向?

4

3 回答 3

0
CustomListViewDemo.this

为了this工作,你需要一个实例。

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

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

于 2012-06-05T03:49:48.860 回答
0

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

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

由于您已经存储了一个上下文,您可以通过使用new Intent(this.context, IntentA.class)来解决您的问题。

于 2012-06-05T03:49:50.607 回答
0

EfficientAdapter是一个静态类,所以你不一定有一个CustomListViewDemo可以使用的实例。静态意味着可以在没有实例的情况下使用该类,因此您的错误

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

您在这里有两个选择。

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

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

2)不要制作EfficientAdapter静态类(让它成为静态的原因是什么?)

于 2012-06-05T03:56:38.630 回答