Show data in GridView

3

I'm trying to make a simple application where a GridView is shown, but the application does not show the GridView . What am I doing wrong?

public class AdapterCategory extends BaseAdapter{

    protected Activity activity;
    protected ArrayList<Categoriaç> item;

    public AdapterCategory(Activity activity, ArrayList<Categoriaç> item){
        this.activity=activity;
        this.item=item;
    }
    @Override
    public int getCount(){
        return item.size();
    }
    public void clear(){
        item.clear();
    }
    public void addAdll(ArrayList<Categoriaç>item){
        for(int i=0; i < item.size(); i++){
            item.add(item.get(i));
        }
    }
    @Override
    public Object getItem(int arg0){
        return item.get(arg0);
    }
    @Override
    public long getItemId(int position){
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.objeto, null);
        }
        Categoriaç dir = item.get(position);

        TextView description = (TextView) v.findViewById(R.id.texto);
        description.setText(dir.getTexto());

        ImageView imagen = (ImageView) v.findViewById(R.id.ivCalendar);
        imagen.setImageDrawable(dir.getImagen());

        return v;
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ArrayList<Categoriaç> Categoria = new ArrayList<Categoriaç>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv = (GridView) findViewById(R.id.gridView1);

        AdapterCategory adapter = new AdapterCategory(this, Categoria);

        gv.setAdapter(adapter);
    }
}   
public class Categoriaç {

    private String texto;
    private Drawable imagen;

    public Categoriaç(){
        super();
    }   
    public Categoriaç(String texto, Drawable imagen){
        super();
        this.texto=texto;
        this.imagen=imagen;
    }   
    public void setTexto(String texto) {
        this.texto = texto;
    }   
    public void setImagen(Drawable imagen) {
        this.imagen = imagen;
    }   
    public String getTexto() {
        return texto;
    }   
    public Drawable getImagen() {
        return imagen;
    }
}

The layout activity_main :

<GridView
    android:id="@+id/gridView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

</GridView>

And the layout object :

<ImageView
    android:id="@+id/ivCalendar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/calendar"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="31dp"
    android:layout_marginStart="31dp" />

<TextView
    android:id="@+id/texto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Imagen"
    android:layout_marginLeft="40dp"
    android:layout_marginStart="40dp"
    android:layout_below="@+id/ivCalendar"
    android:layout_alignLeft="@+id/ivCalendar"
    android:layout_alignStart="@+id/ivCalendar" />

    
asked by J.Carmona 31.01.2017 в 12:41
source

1 answer

1

Everything is correct, the problem is that the ArrayList that you send to add the elements to AdapterCategory is empty, you will not add any element if this ArrayList does not contain data.

If you review, you are stating a ArrayList which is empty, instances the Adapter with this data and then assign it to GridView .

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        ArrayList<Categoriaç> Categoria = new ArrayList<Categoriaç>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv = (GridView) findViewById(R.id.gridView1);

        AdapterCategory adapter = new AdapterCategory(this, Categoria);

        gv.setAdapter(adapter);
    }

As a test, create a ArrayList of objects containing text (obviously missing the images), example:

...
...
...
    ArrayList<Categoriaç> categoria = new ArrayList<Categoriaç>();
    Categoriaç cat = new Categoriaç();
    cat.setTexto("Java");
    categoria.add(cat);
    cat = new Categoriaç();
    cat.setTexto("C++");
    categoria.add(cat);
    cat = new Categoriaç();
    cat.setTexto("Python");
    categoria.add(cat);
    cat = new Categoriaç();
    cat.setTexto("C#");
    categoria.add(cat);
    cat = new Categoriaç();
    cat.setTexto("Swift");
    categoria.add(cat);
    cat = new Categoriaç();
    cat.setTexto("C");
    categoria.add(cat);

   AdapterCategory adapter = new AdapterCategory(this, categoria);

   gv.setAdapter(adapter);

This will display your GridView with data.

I do not advise adding the character ç in the name of the Object class:

Categoriaç
  

Class naming rule : The names of the class, it should be   nouns, uppercase and lowercase with the first letter of each   word in capital letters. Try to keep their names of simple classes   and descriptive. Use complete words, avoid using acronyms and   abbreviations (unless the abbreviation used is much more   wide than the long form, examples such as the URL or   HTML).

    
answered by 31.01.2017 / 17:49
source