我有一个问题:我可以使用在另一个方法中的方法中实例化的内部类变量吗?
public void onItemClick(AdapterView<?> a, android.view.View v, int position, long id) {
// TODO Auto-generated method stub
int diffInDays=0;
Object o = lv1.getItemAtPosition(position);
Masina m = (Masina) o;
Toast.makeText(MainActivity.this, "Selected :" + " " + m, Toast.LENGTH_LONG).show();
String data=((Masina) image_details.get(position)).getData_fabr();
Calendar c=Calendar.getInstance();
SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");
String currData=df.format(c.getTime());
try {
Date obj=df.parse(data);
Date obj2=df.parse(currData);
diffInDays = (int) ((obj2.getTime() - obj.getTime())/ (1000 * 60 * 60 * 24));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int years=diffInDays/365;
int days=diffInDays%365;
poz=position;
mar=((Masina) image_details.get(poz)).getMarca();
mod=((Masina) image_details.get(poz)).getModel();
nr=((Masina) image_details.get(poz)).getNr_inm();
date=((Masina) image_details.get(poz)).getData_fabr();
afisare.setText("Numar inmatriculare: "+((Masina) image_details.get(position)).getNr_inm());
afisare.append("\nVarsta masina: "+years+" ani "+days+" zile");
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.edit:
Toast.makeText(this, "Ai ales: " + getResources().getString(R.string.Editeaza), Toast.LENGTH_SHORT).show();
Intent startNewAct=new Intent(MainActivity.this, EditActivity.class);
startNewAct.putExtra("marca",mar);
startNewAct.putExtra("model",mod);
startNewAct.putExtra("Nr.inm",nr);
startNewAct.putExtra("Data",date);
startActivityForResult(startNewAct,0);
return true;
我想在方法onContextItemSelected中使用vb mar。请帮帮忙。非常感谢你。
答案 0 :(得分:0)
你可以用方法中的public static初始化vb mar。