I would like to know how to add a view (RelativeLayout) dynamically in my xml, as records are being found in my BD Mysql ... (My connection is made via the Volley lib)
I would like to know how to add a view (RelativeLayout) dynamically in my xml, as records are being found in my BD Mysql ... (My connection is made via the Volley lib)
To implement the list you will need basically 4 parts:
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, just a Frame with a ListView inside, it's 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 any way you want. This layout will be repeated for each item.
3-) Custom Adapter: 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 gets an object, if its data is not built into an object, I recommend it, it's quieter to treat.
4-) Assigning the Adapter to the ListView: MinhaActivity.java
Inside 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 you do not have much secrets too, once you have received the data through Volley, organize them into a list of objects, an object with the image and the legend, then build your adapter passing (Context, layout of items, Data) , assign this adapter to the list and everything is ready