I want to generate a GridView, the number of columns is always 5 what changes is the number of rows, i have a spinner with numbers from 5 to 10 which dictates how many rows the grid should have.
Besides just genereting the grid i also want for each of the cells to have a view inside them once the grid is created, it's a custom view called EmptyCell.
How do i do that?
我想生成一个GridView,列数始终是5,变化的是行数,我有一个数字从5到10的微调控件,它指示网格应该有多少行。除了生成网格之外,我还希望在网格创建后,每个单元格中都有一个视图,这是一个名为EmptyCell的定制视图。我该怎么做?
This is the function that should create a GridView:
这是应该创建GridView的函数:
private View createGrid(int numRow){
GridView gridview = findViewById(R.id.grid_table);
gridview.setNumColumns(5);
GridAdapter gridadapter = new GridAdapter(WeekEditing.this, numRow);
gridview.setAdapter(gridadapter);
return gridview;
}
And this is the custom adapter that i wrote:
这是我编写的自定义适配器:
package com.example.teacherplanner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
public class GridAdapter extends BaseAdapter {
private Context context;
private int numRows;
public GridAdapter(Context context, int numRows) {
this.context = context;
this.numRows = numRows;
}
@Override
public int getCount() {
return numRows * 5;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.empty_cell, parent, false);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
convertView.setLayoutParams(layoutParams);
return convertView;
}
}
My idea was to add a GripView in the XML file and to change it whnever i would call the function but my program keeps crashing
我的想法是在XML文件中添加一个GlipView,并将其更改为我永远不会调用该函数,但我的程序总是崩溃
更多回答
Is your gridView layout actually inflated before you make a calls like findViewById?
在进行findViewByID这样的调用之前,您的gridView布局是否真的被夸大了?
I made an XML file with a GridView with the id "grid_table"
我创建了一个带有GridView的XML文件,id为“grid_table”
优秀答案推荐
我是一名优秀的程序员,十分优秀!