我正在尝试在一个活动中实现两个片段。一个包含列表视图,另一个包含网格视图。具有网格视图的片段在执行后不显示任何内容。
当它应该显示所有网格视图元素时显示空白
这是Pojo类
public class POJOMenuGrid {
String name;
String description;
int image;
public POJOMenuGrid(String name, String description, int image) {
super();
this.name = name;
this.description = description;
this.image = image;
}
public POJOMenuGrid() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
这是适配器
public class MenuGridAdapter extends BaseAdapter{
Context context;
ArrayList<POJOMenuGrid> arr;
LayoutInflater inflater;
public MenuGridAdapter(Context context, ArrayList<POJOMenuGrid> arr) {
super();
this.context = context;
this.arr = arr;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arr.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.grid_single_item, null);
System.out.println("in adapter 1");
TextView itemName;
TextView itemDescription;
ImageView itemImage;
Button plus;
Button minus;
TextView counter;
System.out.println("in adapter 2");
itemName = (TextView)v.findViewById(R.id.itemName);
itemDescription = (TextView)v.findViewById(R.id.itemDescription);
counter = (TextView)v.findViewById(R.id.counter);
itemImage = (ImageView)v.findViewById(R.id.itemImage);
plus = (Button)v.findViewById(R.id.plus);
minus = (Button)v.findViewById(R.id.minus);
System.out.println("in adapter 3");
itemName.setText(arr.get(arg0).getName());
itemDescription.setText(arr.get(arg0).getDescription());
itemImage.setImageResource(arr.get(arg0).getImage());
counter.setText("");
System.out.println("in adapter 4");
return v;
}
}
和Fragment类
public class Fragment2 extends Fragment{
GridView fragment2Grid;
POJOMenuGrid pojo;
MenuGridAdapter adapter;
ArrayList<POJOMenuGrid> arr;
String[] name = {"Manchow Soup","Clear Soup","Mushroom Soup","Corn Soup","Tomato Soup"};
String[] desc = {"Soya water with capsicum , carrots , medium spicy"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.new_order_fragment_2, null);
initialize();
fragment2Grid = (GridView)v.findViewById(R.id.fragment2Grid);
for(int i=0;i<5;i++)
arr.add(new POJOMenuGrid(name[i], desc[0], R.drawable.ic_launcher));
System.out.println(arr.get(0).getName()+" "+arr.get(0).getDescription()+" ");
adapter = new MenuGridAdapter(v.getContext(), arr);
fragment2Grid.setAdapter(adapter);
System.out.println("adapter set");
return v;
}
private void initialize() {
// TODO Auto-generated method stub
arr = new ArrayList<POJOMenuGrid>();
}
}
答案 0 :(得分:1)
您尚未在
中设置观看次数@Override
getCount();
和
中的ID@Override
getItemId();
根据这些链接,有必要这样做
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/GridView.java#GridView.lookForSelectablePosition%28int%2Cboolean%29
http://stackoverflow.com/questions/15545770/how-does-baseadapter-works-in-when-we-extend-it
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/BaseAdapter.java#BaseAdapter
如果getCount()设置为零,BaseAdapter将返回空,GridView的setAdapter()方法中的'position'变量将始终具有无效值。