我无法在Gridview中实现Onclick方法,这是我的类,错误显示无法解析符号Toast?任何的想法?如果有必要,我可以显示名为CustomAdapter的适配器。
编辑:当我登场时,没有任何消息
public class Tab3School extends Fragment {
Context context;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.grid_item, container, false);
GridView gridview = (GridView)v.findViewById(R.id.gridview);
List<ItemNoticiasColegio> allItems = getAllItemObject();
CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems);
gridview.setAdapter(customAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position)
{
case 0:
Toast.makeText(context, "FDSFASDF", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "FDSFASDF", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(context, "FDSFASDF", Toast.LENGTH_SHORT).show();
break;
}
}
});
return v;
}
private List<ItemNoticiasColegio> getAllItemObject(){
List<ItemNoticiasColegio> items = new ArrayList<>();
items.add(new ItemNoticiasColegio(R.drawable.alkor,"Dip It Low", "Christina Milian"));
items.add(new ItemNoticiasColegio(R.drawable.alkor,"sdfasd", "gfgdfsfdg"));
items.add(new ItemNoticiasColegio(R.drawable.alkor,"hola", "adios"));
items.add(new ItemNoticiasColegio(R.drawable.padres,"marcos", "yo"));
return items;
}
}
我修正了将这些行添加到grid_layout
中 android:focusable="false"
android:focusableInTouchMode="false"
答案 0 :(得分:1)
您的结构中的错误,请查看评论
public class Tab3School extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.grid_item, container, false);
GridView gridview = (GridView)v.findViewById(R.id.gridview);
List<ItemNoticiasColegio> allItems = getAllItemObject();
CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems);
gridview.setAdapter(customAdapter);
// move gridview before return view
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
return v;
// return view; remove this mean no thing
}
private List<ItemNoticiasColegio> getAllItemObject(){
List<ItemNoticiasColegio> items = new ArrayList<>();
items.add(new ItemNoticiasColegio(R.drawable.alkor,"Dip It Low", "Christina Milian"));
items.add(new ItemNoticiasColegio(R.drawable.alkor,"sdfasd", "gfgdfsfdg"));
items.add(new ItemNoticiasColegio(R.drawable.alkor,"hola", "adios"));
items.add(new ItemNoticiasColegio(R.drawable.padres,"marcos", "yo"));
return items;
}
}