我正在测试从 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];
}
}
}
我已经看过很多关于如何引用嵌套类的外部成员的示例,但没有找到一个很好的示例来查找外部类的视图实例以用作方法参数。谁能指出我正确的方向?