gpt4 book ai didi

android - 数据不显示在 gridView Android 中

转载 作者:行者123 更新时间:2023-11-30 01:22:03 25 4
gpt4 key购买 nike

我想加载数据以在 gridView 中显示。

它运行没有任何错误。但是,屏幕仍然是空白的,gridView 也没有显示。

这是我的代码

主 Activity .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"
/>

项目.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"
/>

最佳答案

getCount() 返回 0。

该方法必须返回适配器中的项目数量...类似于:

@Override
public int getCount() {
return this.Values.lenght;
}

当然,您可以添加空检查等...

关于android - 数据不显示在 gridView Android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com