[英]Android - GridView with a custom adapter doesn't work
I've created a Custom Adapter for ListView and GridView. 我为ListView和GridView创建了一个自定义适配器。 It's all the same, I just want one tab with List, other with GridView.
都是一样的,我只想要一个带有List的选项卡,另一个带有GridView的选项卡。 I can set the data to ListView on Fragment1 but I can not do the same for GridView on Fragment2 (Gives a Null Pointer Exception).
我可以将数据设置为Fragment1上的ListView,但不能对Fragment2上的GridView进行相同的操作(给出空指针异常)。
Codes are below : 代码如下:
Fragment2.java : Fragment2.java:
public class FragmentTab2 extends Fragment {
GridView gridView;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
gridView = (GridView) rootView.findViewById(R.id.GridView1);
return rootView;
}
public GridView getGridView() {
return gridView;
}
}
CustomAdapter.java : CustomAdapter.java:
public class CustomAdapter extends BaseAdapter {
private ArrayList<ListData> data;
private Context context;
CustomAdapter(Context context, ArrayList<ListData> data) {
this.context = context;
this.data = data;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, null);
}
ImageView image = (ImageView) rowView.findViewById(R.id.listLogo);
TextView nameVideo = (TextView) rowView.findViewById(R.id.label);
TextView link = (TextView) rowView.findViewById(R.id.link);
// Get individual object from ArrayList<ListData> and set ListView items
ListData temp_data = data.get(position);
image.setImageBitmap(temp_data.getImage());
nameVideo.setText(temp_data.getName());
link.setText(temp_data.getLink());
return rowView;
}
}
And the main 和主要
ListView lw = fragmentTab1.getListView();
lw.setAdapter(new CustomAdapter(this, dataArray));
GridView gr = fragmentTab2.getGridView();
//gr.setAdapter(new CustomAdapter(this, dataArray));
The commented part is giving the Null Exception, everything else is fine. 注释部分给出了Null异常,其他一切都很好。 Any ideas?
有任何想法吗? Thanks.
谢谢。
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragmentTab2);
ft.addToBackStack(null);
ft.commit();
It looks like fragmentTab2.getGridView();
看起来像
fragmentTab2.getGridView();
is returning null. 返回null。
Make sure the GridView in your fragmenttab2
actually has the id GridView1
. 确保
fragmenttab2
的GridView实际上具有ID GridView1
。
我认为dataArray的问题可能是它的第一个适配器消耗的,尝试在网格中设置单独的数组并检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.