Use a GridView

1

You see, I'm doing an exercise in which I have a TextView and a GridView with a series of elements taken from a vector. When I choose something from the GridView, it is copied into the TextView.

activity_main code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.pcx.celda.MainActivity">

    <TextView
        android:id="@+id/seleccion"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ABD8F9"
        android:text="Nombre"
        android:textColor="#000066" />

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#328DF9"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="8dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        />

</LinearLayout>

MainActivity Code:

package com.example.pcx.celda;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
    public String[] provincias= {"Almería", "Cádiz", "Córdoba", "Granada", "Huelva", "Jaen", "Malaga", "Sevilla"};

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter adaptador=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,provincias);
        GridView grid=findViewById(R.id.grid);
        grid.setAdapter(adaptador);
        grid.setOnItemClickListener(this);
}

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
        TextView selection= findViewById(R.id.seleccion);
        selection.setText(provincias[position]);
    }
}

But something goes wrong. When I look at examples, it seems that instead of "support_simple_spinner_dropdown_item" I should put a note to an xml file (in the example they call it "cell.xml"), but I have no idea where that file comes from.

    
asked by Miguel Alparez 23.01.2018 в 15:04
source

2 answers

0

It seems that I have solved it. The error actually resided in that the textView covered the GridView and that's why I did not see the objects. When I manage to place them by reducing their width, now I see the icons of the names and they work as I wanted.

    
answered by 23.01.2018 в 15:08
-1

The problem is that the TextView has the property defined:

<TextView
    ....
    android:layout_height="match_parent"
    ...

Having defined: android:layout_height="match_parent" , which will cause this view to cover the entire screen, therefore the lower elements will not be displayed, in this case the Grid.

You have 2 options to correct this:

You can define a fixed height, for example.

  android:layout_height="200dp"

Or you can for example assign a weight of 1 to each element so that the space is uniform:

<TextView
    android:id="@+id/seleccion"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ABD8F9"
    android:text="Nombre"
    android:textColor="#000066"
    android:layout_weight="1"/>

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#328DF9"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="8dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:layout_weight="1"
    />

It would look like this:

    
answered by 23.01.2018 в 18:11