Start Activity from list – launches other activities from list

Activities launcher

public class MainActivity extends ListActivity {
    private class Sample {
        private CharSequence title;
        private Class<? extends Activity> activityClass;
 
        public Sample(int titleResId, Class<? extends Activity> activityClass) {
            this.activityClass = activityClass;
            this.title = getResources().getString(titleResId);
        }
 
        @Override
        public String toString() {
            return title.toString();
        }
    }
 
    private static Sample[] mSamples;
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Instantiate the list of samples.
        mSamples = new Sample[]{
                new Sample(R.string.title_first_app, FirstActivity.class),
                new Sample(R.string.title_second_app, SecondActivity.class),
                new Sample(R.string.title_third_app, ThirdActivity.class),
        };
 
        setListAdapter(new ArrayAdapter<Sample>(this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                mSamples));
    }
 
    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        // Launch the sample associated with this list position.
        startActivity(new Intent(MainActivity.this, mSamples[position].activityClass));
    }
}
This entry was posted in Android Examples Code, Android Studio and tagged . Bookmark the permalink.


Comments are closed.

1,152 views