I would like to know how to put data from an array to a Custom GridView, when it is just a matter of filling a simple Gridview it is done like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, miArreglo);
gridView.setAdapter(adapter);
But in this case, I add the cells programmatically using a class Adapter
. Then to generate them I assign them to the gridView that is already created in Layout
:
customgridviewadapter = new CustomGridViewAdapter(getActivity());
gridview.setAdapter(customgridviewadapter);
Now my question is how to add data from an array to gridview
to which I add cells from code?
Since only a adapter
can be assigned to it:
gridview.setAdapter();
UPDATE
I have the activity
GridFragment
:
public class GridFragment extends Fragment {
CustomGridViewAdapter gridViewElement;
private GridView grid;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_gridfragment, container, false);
//Aquí está el arreglo que quiero que se coloque en el gridView
String palabra = "abcdefghijklmno";
String [] preguntaRespuesta = palabra.split("");
grid = (GridView) rootview.findViewById(R.id.gridViewLayout);
grid.setNumColumns(13);
gridViewElement = new CustomGridViewAdapter();
grid.setAdapter(gridViewElement);
return rootview;
}
}
This is CustomGridViewAdapter
:
public class CustomGridViewAdapter extends BaseAdapter
{
Context context;
public CustomGridViewAdapter(Context context)
{
//super(context, 0);
this.context=context;
}
public int getCount()
{
return 169;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
//Cargo un Custom Edittext en cada celda
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_item, parent, false);
EditText editTextCelda = (EditText) row.findViewById(R.id.gridEdittext);
editTextCelda.setGravity(Gravity.TOP | Gravity.LEFT);
final int sdk = Build.VERSION.SDK_INT;
if (editTextCelda.getText().toString().trim().equals(""))
{
if(sdk < Build.VERSION_CODES.JELLY_BEAN) {
editTextCelda.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_edittext));
} else {
editTextCelda.setBackground( context.getResources().getDrawable(R.drawable.rounded_edittext));
}
} else{
editTextCelda.setBackgroundColor(Color.WHITE);
}
return row;
}
}