如何为点击的每个项目打开新活动,而不是仅将消息显示为Toast
消息?
当我点击该项目时,它应该在新活动中打开一个新活动或一个片段。这是我的代码:
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;
}
}
答案 0 :(得分:0)
您可以使用意图打开新活动。
@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;
}
}
类似的东西。