简体   繁体   中英

how to open activity by item click instead of toast?

How do I open a new activity for each item clicked instead of just displaying the message in as a Toast message?

When I click the item, it should open a new activity or a fragment in a new activity. Here is my code:

public List<Product> getProductList() {

    productList = new ArrayList<>();
    productList.add(new Product(R.drawable.icon_android, "Title 1", "This is description 1"));
    productList.add(new Product(R.drawable.icon_android, "Title 2", "This is description 2"));
    productList.add(new Product(R.drawable.icon_android, "Title 3", "This is description 3"));
    productList.add(new Product(R.drawable.icon_android, "Title 4", "This is description 4"));
    productList.add(new Product(R.drawable.icon_android, "Title 5", "This is description 5"));
    productList.add(new Product(R.drawable.icon_android, "Title 6", "This is description 6"));
    productList.add(new Product(R.drawable.icon_android, "Title 7", "This is description 7"));
    productList.add(new Product(R.drawable.icon_android, "Title 8", "This is description 8"));
    productList.add(new Product(R.drawable.icon_android, "Title 9", "This is description 9"));
    productList.add(new Product(R.drawable.icon_android, "Title 10", "This is description 10"));
    return productList;
}

    AdapterView.OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {

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

            //Do any thing when user click to item

            Toast.makeText(getApplicationContext(), productList.get(position).getTitle() + " - " + productList.get(position).getDescription(), Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.item_menu_1:
                if(VIEW_MODE_LISTVIEW == currentViewMode) {
                    currentViewMode = VIEW_MODE_GRIDVIEW;
                } else {
                    currentViewMode = VIEW_MODE_LISTVIEW;
                }
                //Switch view
                switchView();
                //Save view mode in share reference
                SharedPreferences sharedPreferences = getSharedPreferences("ViewMode", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("currentViewMode", currentViewMode);
                editor.commit();

                break;
        }
        return true;
    }
}

You can use intent to open new activity.

     @Override 
    public void onItemClick(View view, int position) {
        switch(position){
        case 0:
            startActivity(new Intent(MainActivity.this, A.class));
            break;
        case 1:
            startActivity(new Intent(MainActivity.this, B.class));
            break;
        default:
            break;  
        }
    }

Something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM