我想加载数据以在gridView中显示。
它运行没有任何错误。但是,屏幕保持空白,并且gridView不会显示。
这是我的代码
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment f = new fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_id,f).commit();
}
fragment.java
public class fragment extends Fragment {
public final String [] Names={"Ahmed","Ahmed1","Ahmed2","Ahmed3","Ahmed4","Ahmed5","Ahmed6","Ahmed7","Ahmed8"};
public final int [] Images={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h};
private CustomAdapter c;
private GridView g;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment, container, false);
c= new CustomAdapter(getActivity().getApplicationContext(),Names,Images);
g=(GridView) view.findViewById(R.id.gridview);
g.setAdapter(c);
g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "You Clicked at " + Names[+position], Toast.LENGTH_SHORT).show();
}
});
return view;
}
自定义适配器
public class CustomAdapter extends BaseAdapter {
private Context context;
private String[] Values;
private int[] imageIds;
public CustomAdapter(Context c, String[] Values, int[] imageIds) {
this.context = c;
this.Values = Values;
this.imageIds = imageIds;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View grid;
if(convertView==null) {
grid= new View (context);
grid = inflater.inflate(R.layout.item, null);
TextView textView = (TextView) grid.findViewById(R.id.itemName);
ImageView imageView = (ImageView) grid.findViewById(R.id.itemImage);
textView.setText(Values[position]);
imageView.setImageResource(imageIds[position]);
}
else {
grid = (View) convertView;
}
return grid;
}
}
fragment_fragment.xml
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gridview"
/>
item.xml
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/itemImage"
android:src="@drawable/a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="item"
android:id="@+id/itemName"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
/>
答案 0 :(得分:1)
getCount()
返回0.
该方法必须返回适配器中的项目数量...类似于:
@Override
public int getCount() {
return this.Values.lenght;
}
当然,您可以添加空检查等...