[英]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.