Fortunately, and thanks to the help of the community, I am solving the problems that I had in my application.
Now I'm transferring another one of them, that no matter how much I've read and followed manuals and videos, I can't solve it either.
I have a query that I make to the database, and from it I get the "Number", the "Name", and the digits that identify the image of each member that I have in the "dragables" app folder
The images are saved with the format "a" + "Nº" (This number is the one I get in the query)
This is the query class:
public class ListItemSocios
{
ConexionSQLServer miConexion = new ConexionSQLServer();
public List<Map<String,String>> getlist()
{
List<Map<String,String>> data = null;
data = new ArrayList<Map<String,String>>();
try
{
Statement stn = miConexion.metodoConexionBD().createStatement();
ResultSet rst = stn.executeQuery("select f_ConsumerNO, f_ConsumerName, f_CardNO from t_b_Consumer");
while (rst.next())
{
Map<String,String> dtname = new HashMap<String, String>();
dtname.put("Numero", rst.getString("f_ConsumerNO"));
dtname.put("Nombre", rst.getString("f_ConsumerName"));
dtname.put("Foto", "a" + rst.getString("f_CardNO"));
data.add(dtname);
}
rst.close();
stn.close();
}
catch (Exception ee)
{
ee.printStackTrace();
}
return data;
}
}
From here I call the above class and display the data in the list:
public void metodoLlenarListaSocios() {
ListView list_S = (ListView) findViewById(R.id.lista_Socios);
List<Map<String, String>> MyDataList = null;
ListItemSocios MyData = new ListItemSocios();
MyDataList = MyData.getlist();
// definimos las imagenes que cargara el listview
String[] nFotos = {"Foto"};
int[] resId = new int[nFotos.length];
for (int i = 0; i < nFotos.length; i++) {
resId[i] = getResources().getIdentifier("a" + nFotos[i], "drawable", getPackageName());
}
String[] FromV = {"Numero", "Nombre", "Foto"};
int[] Tow = {R.id.edt_socio_numero, R.id.edt_socio_nombre, R.id.img_socio_imagen};
ad = new SimpleAdapter(PaginaListaS.this, MyDataList, R.layout.vista_socio, FromV, Tow);
list_S.setAdapter(ad);
}
The data shows me well, but the image has no way and I don't know how to do it. I use a SimpleCursorAdapter, and in some answer I heard, that it would have to be with a CustomCursorAdapter, but I don't know how to do it.
This is the code for the custom view:
<?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="wrap_content"
android:background="@color/black"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="114dp"
android:layout_height="134dp"
android:orientation="vertical">
<ImageView
android:id="@+id/img_socio_imagen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:orientation="vertical">
<TextView
android:id="@+id/txt_socio_numero"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Socio"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/edt_socio_numero"
android:layout_width="match_parent"
android:layout_height="35dp"
android:textColor="@color/amarillo"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_socio_nombre"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Usuario"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/edt_socio_nombre"
android:layout_width="match_parent"
android:layout_height="38dp"
android:textColor="@color/amarillo"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/white"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
You can call me useless, but I'm sorry to say, I don't get it. This thing about ArrayList, Map, adapter and Lists is beyond me.
I don't know how to apply the code you gave me. I have MyDataList which is supposed to be returned by MyData.getlist();. And then I have your code, but I don't know how to link one to the other.
public void metodoLlenarListaSocios() {
ListView list_S = (ListView) findViewById(R.id.lista_Socios);
List<Map<String, String>> MyDataList = null;
ListItemSocios MyData = new ListItemSocios();
MyDataList = MyData.getlist();
for (int i = 0; i < MyDataList.size(); i++) {
Map<String, String> socio = MyDataList.get(i);
int resId = getResources().getIdentifier("a" + socio.get("Foto"), "drawable", getPackageName());
socio.put("Foto", Integer.toString(resId));
}
String[] FromV = {"Numero", "Nombre", "Foto"};
int[] Tow = {R.id.edt_socio_numero, R.id.edt_socio_nombre, R.id.img_socio_imagen};
ad = new SimpleAdapter(PaginaListaS.this, MyDataList, R.layout.vista_socio, FromV, Tow);
list_S.setAdapter(ad);
}
I honestly don't understand how this code is supposed to define the images that the listview will load
Things you should notice:
"Foto"
for
will only be executed once. So your code can be reduced to this:which is ultimately the same as
Also you never use the variable again
resId
.The solution (which I assume is what you tried to do) is to modify the list that you are going to pass to the adapter. You can do it like this
EDIT
In the method
getlist()
you already added"a"
in front of the photo number. Doing it again you get an invalid id. Removing the extra "a" should work