Android活动

时间:2015-04-11 13:54:31

标签: android android-intent android-context

我有代码在DrawerClass类中显示一个菜单抽屉,这个类是由所有活动实例化来显示菜单的,问题是不喜欢把每个菜单的Intents交给第一个参数,它通常是“theActivity”。这个“,这显然不适用于外部课程,那么你得到了什么?我尝试了getAplicationContext,将上下文作为参数传递给了类,没有任何内容。

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

1 个答案:

答案 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);
         }
        }
    });
}