How can i start a different activity on item click from a custom listview?

前端 未结 4 1286
不思量自难忘°
不思量自难忘° 2021-01-14 13:03

i followed numerous tuitorials like http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html Also followed the questions asked here How to make custom

4条回答
  •  遥遥无期
    2021-01-14 13:22

    By doing this way you don't have to write all the switch case for activity.

    public class MainActivity extends Activity {
      ListView list;
      String[] web = {
              "Notifications",
              "School",
              "What's Hot",
              "Tell a friend",
              "Hit us up",
              "Settings",
              "About & Help"
      };
      String[] s1 = {
              "Notifications",
              "School",
              "Whats_hot",
              "Tellafriend",
              "Hitusup",
              "Settings",
              "AboutHelp"
      };
    
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomList adapter = new
        CustomList(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view,
                                        int position, long id) {
                    String a=s1[position];
                    Class a1= null;
                    try {
                         a1 =    Class.forName("."+a);
                    } catch (ClassNotFoundException e) {
                         e.printStackTrace();
                    }
                    startActivity(new Intent(MainActivity.this,a1));
    
                }
            @SuppressWarnings("unused")
            public void onClick(View v){ 
            };
         });
      }
    
    }
    

    Don't forget to put point '.' immediately after your package name.

提交回复
热议问题