在循环中使用intent来获取单击的不同listview项目的不同活动

时间:2017-04-15 06:57:19

标签: android android-studio-2.2

private void firstyearsublist() {
    //string array for intents
    String[] intents = {"Calculus","eee","eee","cpu","physics","chemistry","es","be","vcla"};
    //CREATE STRINGLIST
    String[] subs = {"Calculus","EME","EEE","CPU","Physics","Chemistry","ES","BE","VCLA"};
    //BUILD ADAPTER
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subs);
    //Configure list view
    ListView sublist = (ListView) findViewById(R.id.firstyearsub);
    sublist.setAdapter(adapter1);

    sublist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                       @Override
                                       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                          for(int i = 0;i<9;i++)
                                          {if (position == i) {
                                                  Intent intents[i] =
                                                  new Intent(Sem1.this, intents[i]. class);
                                                  startActivity(intents[i]);
                                              }
                                                  }

                                       }
                                   }


    );
}}

我正在制作一个应用程序,用于访问我所在学院所有学期的工程综合笔记。我收到太多错误。难道我做错了什么?请查看代码。

1 个答案:

答案 0 :(得分:0)

这实际上是你应该怎么做的

  //replace with your activity classess
  Class intents[] = {ClassA.class,ClassB.class,ClassC.class,ClassS.class};

  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(BaseActivity.this,intents[position]));
            }
        });