How to add ImageView to ListView

0

I've been reading and apparently you have to create an Adapter, but the truth is that I do not clarify much, someone could make an example or guide me something else? If you need more information or something else, tell me. Thanks!

I leave my code here

MainActivity:

public class MyActivity extends ActionBarActivity {

    Button btnAgregarPersona;
    ListView lista;
    SQLControlador dbconeccion;
    TextView tv_miemID, tv_miemNombre, tv_miemFecha;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        dbconeccion = new SQLControlador(this);
        dbconeccion.abrirBaseDeDatos();
        btnAgregarPersona = (Button) findViewById(R.id.btnAgregarPersona);
        lista = (ListView) findViewById(R.id.listViewPersonas);

        //acción del boton agregar persona
        btnAgregarPersona.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent iagregar = new Intent(MyActivity.this, AgregarPersona.class);
                startActivity(iagregar);
            }
        });

        Cursor cursor = dbconeccion.leerDatos();

        String[] from = new String[]{
                DBhelper.PERSONA_ID,
                DBhelper.PERSONA_NOMBRE,
                DBhelper.PERSONA_FECHA
        };
        int[] to = new int[]{
                R.id.persona_id,
                R.id.persona_nombre,
                R.id.persona_fecha
        };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                MyActivity.this, R.layout.formato_fila, cursor, from, to);

        adapter.notifyDataSetChanged();
        lista.setAdapter(adapter);

        // acción cuando hacemos click en item para poder modificarlo o eliminarlo
        lista.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                tv_miemID = (TextView) view.findViewById(R.id.persona_id);
                tv_miemNombre = (TextView) view.findViewById(R.id.persona_nombre);
                tv_miemFecha = (TextView) view.findViewById(R.id.persona_fecha);

                String aux_personaId = tv_miemID.getText().toString();
                String aux_personaNombre = tv_miemNombre.getText().toString();
                String aux_personaFecha = tv_miemFecha.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(), ModificarPersona.class);
                modify_intent.putExtra("personaId", aux_personaId);
                modify_intent.putExtra("personaNombre", aux_personaNombre);
                modify_intent.putExtra("personaFecha", aux_personaFecha);
                startActivity(modify_intent);
            }
        });
    }

    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <Button
        android:id="@+id/btnAgregarPersona"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Agregar Cumpleaños" />

    <ListView
        android:id="@+id/listViewPersonas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="2dp">
    </ListView>

</LinearLayout>

format_file.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/persona_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#00000000" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/persona_nombre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:textSize="17sp"
            android:textStyle="bold"
            android:text="Nombre"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/persona_fecha"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:textSize="17sp"
            android:textStyle="bold"
            android:text="Fecha"
            android:layout_weight="1" />

    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#3f51b5"
        android:id="@+id/separador1"
        android:layout_marginTop="10dp" />


</LinearLayout>
    
asked by UserNameYo 31.12.2016 в 01:37
source

1 answer

1

We are not here to solve problems, but only to guide or try to solve errors. You asked for a practical example to guide you. In google there are thousands of tutorials. The listview theme is very recurrent in StackOverFlowEs. I recently did a example of en listview on Android and it will be useful for your application. You can adapt.

This example shows a listview with texts and images. Also the way to optimize and that it does not take long to show the results when scrolling.

    
answered by 31.12.2016 / 15:00
source