MainActivity
DrawerClass d=new DrawerClass();
d.crear(listado,getApplicationContext(),getResources().getString(R.string.title_activity_home));
DrawerClass
public void crear(ListView listado, final Context context, final String nom){
final ArrayList<String> datos=new ArrayList<String>();
datos.add(nom);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(contexto,android.R.layout.simple_list_item_1,datos);
listado.setAdapter(adapter);
listado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String valor=datos.get(position);
if (valor.equals(nom)) {
Intent home = new Intent(context, HomeActivity.class);
startActivity(home);
}
}
});
}
答案 0 :(得分:0)
您可以将活动作为参数传递给函数crear
:
MainActivity
DrawerClass d = new DrawerClass();
d.crear(listado, MainActivity.this,
getResources().getString(R.string.title_activity_home));
DrawerClass
public void crear(ListView listado, final Activity activity, final String nom) {
final ArrayList<String> datos = new ArrayList<String>();
datos.add(nom);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
activity, android.R.layout.simple_list_item_1, datos);
listado.setAdapter(adapter);
listado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String valor = datos.get(position);
if (valor.equals(nom)) {
Intent home = new Intent(activity, HomeActivity.class);
activity.startActivity(home);
}
}
});
}