Listview,打开onClick的新活动

时间:2011-07-12 06:06:08

标签: android listview

嘿大家我已经找了好几个小时试图找到解决方案,我的目标是在打开另一个活动时有一个Listview。好吧,实际上我得到它可以在点击时打开另一个活动,但我如何得到它,以便每个列表项将打开自己的活动?如果这个问题已经得到解答,我非常抱歉,但我找到的链接并没有真正描述代码正在做什么[是的,我是新手:)]

这是使用

的代码
@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] countries = getResources().getStringArray(R.array.countries_array);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.newfile, countries));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
             Intent myIntent = new Intent(view.getContext(), Html_file.class);
             startActivityForResult(myIntent, 0);

        }
      });
    }
}

5 个答案:

答案 0 :(得分:17)

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // When clicked, show a toast with the TextView text
        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(view.getContext(), Html_file1.class);
            startActivityForResult(myIntent, 0);
        }

        if(position == 2) {
            //code specific to 2nd list item    
            Intent myIntent = new Intent(view.getContext(), Html_file2.class);
            startActivityForResult(myIntent, 0);
        }
    }
});

答案 1 :(得分:4)

public void onItemClick(AdapterView<?> parent, View view,
  int position, long id) {
   switch( position ) {
     case 0:  Intent newActivity = new Intent(this, i1.class);     
              startActivity(newActivity);
              break;
     case 1:  Intent newActivity = new Intent(this, i2.class);     
              startActivity(newActivity);
              break;
     case 2:  Intent newActivity = new Intent(this, i3.class);     
              startActivity(newActivity);
              break;
     case 3:  Intent newActivity = new Intent(this, i4.class);     
              startActivity(newActivity);
              break;
     case 4:  Intent newActivity = new Intent(this, i5.class);     
              startActivity(newActivity);
              break;
    }
}

答案 2 :(得分:3)

如果您的列表数量有限,可以在位置

使用开关案例
lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
             Intent myIntent = new Intent(view.getContext(), Html_file.class);
             startActivityForResult(myIntent, 0);

        }
});

答案 3 :(得分:1)

如果您知道在单击不同的列表项时要打开哪个活动,则只需为列表项指定ID或标记。
在onItemClick的回调中,您有一个参数View,
用它来获取id或tag来区分它们并调用各自的Activity。

答案 4 :(得分:0)

//添加ArrayList和ArrayAdapter:

    final ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("image_one");
        listItems.add("image_two");
        listItems.add("image_three");

    ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this, 
        android.R.layout.simple_list_item_1, listItems);
        myListView.setAdapter(myArrayAdapter);

//添加类的ArrayList

    final ArrayList<Class> intents = new ArrayList<Class>();
        intents.add(image_one.class);
        intents.add(image_two.class);
        intents.add(image_three.class);

//单击列表项以从类的ArrayList

打开类
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
        position, long id) {

            Intent listIntent = new Intent(getApplicationContext(), 
            intents.get(position)); 
            startActivity(listIntent);
        }
    });

SEE IMAGE OF CLASS NAMES HERE