Enter fix to a Custom GridView

4

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;

    }


}
    
asked by x4mp73r 07.06.2016 в 23:13
source

2 answers

0

Response to the last comment :

  

Well, thanks before everything, in fact that had proved the same, but   I would like a certain number of cells to be generated, for example   100 and that the array data is entered and occupy only the cells   correspond, since in that way only the cells are generated   depending on the size of the arrangement: / I do not know if there will be a way to do   that, generate x number of cells and put the arrangement and leaving empty   the rest, that is, if 100 cells are generated and the array only has   10, that the 90 remaining cells appear to me.

We are going to add one more parameter to the constructor of your adapter that will be a ArrayList of Celdas that will have an attribute called arreglo and fill in the gridview with its data.

Cell:

   public class Celda{

        private String arreglo;

        public Celda(String arreglo) {
            this.arreglo = arreglo;
        }

        public void setArreglo(String arreglo) {
            this.arreglo = arreglo;
        }

        public String getArreglo() {
            return arreglo;
        }
    }

Cell ArrayList

ArrayList<Celda> alCeldas = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        String arreglo = "";
        alCeldas.add(new Celda(arreglo));
    }
customgridviewadapter = new CustomGridViewAdapter(getActivity(), alCeldas);
        gridview.setAdapter(customgridviewadapter);

Adapter:

public class CustomGridViewAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Celda> alCeldas;
    public CustomGridViewAdapter(Context context, ArrayList<Celda> alCeldas)
    {
        //super(context, 0);
        this.context=context;
        this.alCeldas= alCeldas;

    }

public int getCount()
{
    return alCeldas.size();
}

@Override
public Celda getItem(int position) {
    return alCeldas.get(position);
}

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

public void insetarArreglo(String arreglo, int posicionArreglo){
   alCeldas.get(posicionArreglo).setArreglo("arreglo");
   this.notifyDataSetChanged();
}

@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);
        editTextCelda.setText(alCeldas.get(position).getArreglo());// con esta linea rellenamos los EditText del gridview con los datos del ArrayList

        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;
    }

}

Insert an array:

If you notice the adapter has a method called insertarArreglo where you have to put the position of the cell where you want to insert the array:

customgridviewadapter.insertarArreglo("miArreglo", posicionDelaCelda); 

That's it, Greetings.

    
answered by 08.06.2016 / 10:34
source
1

The way you should do this is:

modify the array, send it to Adapter , in this case CustomGridViewAdapter() and rerun:

   .notifyDataSetChanged();

I think it's the only way, to have a dynamic data array.

    
answered by 08.06.2016 в 00:59