How to add a view dynamically to android as registered in mysql

Asked

Viewed 412 times

1

I would like to know how to add a view (Relativelayout) dynamically in my xml, as records are found in my Mysql BD... (My connection is made through Volley lib)

  • mahenrocha, what is your specific question ? You already receive the data and want to know how popular ?

  • So... I can pull the mysql data through the lib Volley only q I want q to each received record it load a relative layout of small size inside a scrollview to show a photo and a caption below. As if it were a gallery of images with captions of the records of the bank. I could understand?

  • It is possible yes and it is not difficult, however, by own experience, it is not a good idea to add several Relatives Layout directly in the container, each View will allocate right from the start what will need memory, so you will easily see yourself with memory errors. The right way would be to create one ListView or RecyclerView containing a Relative and inside this relative have the image and the Textview for the caption. This way, only load what is visible + 2.

  • Could you show me some link q teach this? or else if you can post some simple solution for me know the best way to treat it?

  • I posted an example

1 answer

0


To implement the list you will need basically 4 parts:

  1. The layout that will have the list
  2. The layout of each item in the list
  3. A custom adapter
  4. Assign this adapter to your list by passing a list of the data you brought from Volley

1-) Layout that will have the list: main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_verde">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/minha_lista">
    </ListView>

</FrameLayout>

No secret here, only a Frame with a Listview inside, it is important to assign an ID to it

2-)Layout of each Item: list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/minhaImagem"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/minhaLegenda"
android:layout_below="@id/minhaImagem"
/>
</RelativeLayout>

Here I did not worry about aesthetics, then you should format it the way you want. This layout will be repeated for each item.

3-) Custom Adaptor: myAdapter.java

public class myAdapter extends ArrayAdapter<SeuObjeto> {

    public Context context;
    public List<SeuObjeto> lista;

    public myAdapter(Context context, int resource, List<SeuObjeto> objects) {
        super(context, resource, objects);

        this.context = context;
        this.lista= objects;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
// Primeiro checa se a view ainda não foi inflada, se não infla
        if (convertView == null) {
// Inflando a nova View
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }
// Now we can fill the layout with the right values
        TextView txtLegenda= (TextView) convertView.findViewById(R.id.minhaLegenda);

       ImageView img = (ImageView) convertView.findViewById(R.id.minhaImagem);

        SeuObjeto seuObjeto = lista.get(position);

        txtLegenda.setText(seuObjeto.getLegenda());

        img.setImageBitmap(seuObjeto.getImagem()); // <------- Aqui você deve verificar como você está recebendo a imagem, e como vai atribuí-la à ImageView



        return convertView;
    }



}

Here we extend an Arrayadapter, it receives an object, if its data is not built on an object, I recommend, it is more quiet to treat.

4-) Listview Adapter Assignment: Minhaactivity.java

Inside of your onCreate

        ListView listView = (ListView) findViewById(R.id.minha_lista);
        List<SeuObjeto> minhaLista = meusDados; // <---------- Aqui não sei como você recebe os dados, se usa o Volley com Gson, provavelmente você facilmente terá essa lista com os objetos.
        myAdapter adapter = new myAdapter (this,R.layout.list_item,minhaLista);
        listView.setAdapter(adapter);

Here there is not much secret also, after receiving the data through Volley, organize them in a list of objects, an object with the image and the caption, then build your adapter passing (Context, layout of the items, Data), assign this Adapter to the list and everything ready

  • wonder... just one more question... to work with grids and put pictures? what I need to modify?

  • @mahenrocha inside that layout list_item you can work as you like, you can exchange Relativelayout for Grid, Table or anything else, it will be repeated on each Adapter item. To place the image you need to know how you receive it from the webservice, but just assign it to Imageview according to the method you are saving in the Object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.