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

前端 未结 4 1320
不思量自难忘°
不思量自难忘° 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:10

    public class Menu extends ListActivity {
        String classes[]={"startingPoint","TextPlay","TextPlayPerfectJavaCode","EXAMPLE2","example3","example4"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        ArrayAdapter adapter = new ArrayAdapter(Menu.this, android.R.layout.simple_list_item_1,classes);
        setListAdapter(adapter);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String className=classes[position];
        Class ourClass=null;
        try{
            ourClass=Class.forName("travis.thenewboston.com.thenewboston."+className);
            Intent ourIntent = new Intent(getApplicationContext(), ourClass);//replacing Menu.this - getApplicationContext()
            startActivity(ourIntent);
        }
        catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        finally {
            Toast.makeText(Menu.this, "Clicked at Position: "+Integer.toString(position), Toast.LENGTH_SHORT).show();
        }
    }
    

    }

提交回复
热议问题