Listview , open new activity onClick

2020-01-30 12:03发布

Hey everyone I've looked for hours trying to find a solution to this, my goal is to have a Listview when it opens well open another activity. Well actually I got it to be able to open another activity when its click but how do I get it so that each list item will open its own activity? I am terribly sorry if this question is already answered but the links I found doesn't really describe what the code is doing [Yes i am a newbie :)]

this is the code im using

@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条回答
Deceive 欺骗
2楼-- · 2020-01-30 12:06

// Add ArrayList and 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);

// Add ArrayList of Classes:

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

// Click on list item to open Class from ArrayList of Classes:

    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

查看更多
疯言疯语
3楼-- · 2020-01-30 12:11
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);
        }
    }
});
查看更多
Rolldiameter
4楼-- · 2020-01-30 12:15
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;
    }
}
查看更多
叼着烟拽天下
5楼-- · 2020-01-30 12:25

If you know which activity is to be opened when different list items are clicked, then just assign id or tag to the list items.
In the callback of onItemClick, you have a parameter View,
use it to get id or tag to differentiate them and call respective Activity.

查看更多
够拽才男人
6楼-- · 2020-01-30 12:29

If you have some limited number of list you can use switch case here on position

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

        }
});
查看更多
登录 后发表回答