【问题标题】:CustomGridView not working, not showing anything [closed]自定义 GridView 不起作用,不显示任何内容 [关闭]
【发布时间】:2018-04-19 14:06:08
【问题描述】:

我正在使用 vooley-mySql 编写 1 个迷你应用程序,现在,我将其转换为 SQLite,但我的 gridview 没有显示任何内容,我不知道为什么,数据加载良好但不显示。 我正在尝试使用 ArrayAdapter 并工作,但我的应用程序需要带有图像的 customAdapter。请帮助我

CustomGridView_Home.java:http://www.paste.org/88161

Fragment_home.java:http://www.paste.org/88162

SQLheper:http://www.paste.org/88163

【问题讨论】:

  • 请在问题中添加您的代码,我怀疑有人会看它!。

标签: android gridview


【解决方案1】:

适配器文件:

public class CustomProductsAdapter extends BaseAdapter {

TextView textView;
ImageView imageView;
String[] title = new String[]{};
String[] image = new String[]{};
private LayoutInflater inflater = null;

public CustomProductsAdapter(Context context, String[] title, String[] image) {

    this.title = title;
    this.image = image;

    inflater = (LayoutInflater) context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return productList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_grid, null);
    textView = (TextView) view.findViewById(R.id.textView);
    imageView = (ImageView) view.findViewById(R.id.imageView);
    return view;
}}

IN Activity 初始化gridview 并设置Adapter Where 标题和图片需要发送为List,Map,Array

GridView products= (GridView) view.findViewById(R.id.products_grid);
                  products.setAdapter(new CustomProductsAdapter(getContext(),title,image);
                  setDynamicHeight(products);

动态高度方法在这里:

public void setDynamicHeight(GridView gridView) {
    ListAdapter gridViewAdapter = gridView.getAdapter();
    if (gridViewAdapter == null) {
    return;
    }

    int totalHeight = 0;
    int items = gridViewAdapter.getCount();
    int rows = 0;

    View listItem = gridViewAdapter.getView(0, null, gridView);
    listItem.measure(0, 0);
    totalHeight = listItem.getMeasuredHeight();

    float x = 1;
    if( items > 4 ){
    x = items / 4;
    if (x >= 2){
    x = x-1;
    }
    rows = (int) (x + 1);
    totalHeight *= rows;
    }

    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = totalHeight;
    gridView.setLayoutParams(params);
    }

活动 xml 中的 Gridview:

<GridView
          android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@+id/products_grid"
                  android:numColumns="4"
                  android:layout_marginTop="@dimen/margin_10"
                  android:layout_marginStart="@dimen/margin_8"
                  android:layout_marginEnd="@dimen/margin_14"
                  android:layout_marginBottom="@dimen/margin_24"
                  />

适配器文件中的网格用于注入

<ImageView
    android:id="@+id/imageView"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginTop="14dp"
    android:layout_marginStart="24dp"
    android:layout_marginBottom="5dp"
    android:src="@mipmap/bus" />
<TextView
    android:id="@+id/textView"
    android:gravity="center"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="14dp"
    android:textSize="@dimen/textsize_12"
    android:textColor="@color/mildblack"
    android:text="Image Name" />

【讨论】:

  • 抱歉多次编辑。 stackoverflow编辑器提示我错误所以延迟,希望你找到解决办法
猜你喜欢
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
相关资源
最近更新 更多