How to divide an element of a ListView with hashmap?

0
public class ClientesActivity extends AppCompatActivity {

  private Movil_DBHelper moviDb=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_clientes);

      moviDb=Movil_DBHelper.getInstance(this);

      ListView view = (ListView) findViewById(R.id.ListViewClientes);

      ArrayList<String> list = new ArrayList<>();

      Cursor c=moviDb.SelectClientes();

      HashMap<String,String> hashMap=new HashMap<String, String>();

      while (c.moveToNext()) {
        hashMap.put("nombre",c.getString(c.getColumnIndex("PERSONA_NOMBRE")));
        hashMap.put("cedula",c.getString(c.getColumnIndex("PERSONA_CEDULA")));
        hashMap.put("codigo",c.getString(c.getColumnIndex("PERSONA_NUMERO")));

        list.add(hashMap.get("nombre")+hashMap.get("cedula")+hashMap.get("codigo"));  
      }

      ArrayAdapter<String> Adaptador= new ArrayAdapter< String>(this,android.R.layout.simple_list_item_1,list);

      view.setAdapter(Adaptador);
   }
}

What I'm looking for is that it looks like the following image

    
asked by JEFERSON PAUL QUINTANA VARGAS 01.09.2017 в 16:12
source

1 answer

0

To achieve this, create a custom ListView containing 3 TextView.

ClientsActivity

public class ClientesActivity extends AppCompatActivity {

  private Movil_DBHelper moviDb=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_clientes);

      moviDb=Movil_DBHelper.getInstance(this);

      ListView view = (ListView) findViewById(R.id.ListViewClientes);

      ArrayList<Datos> list = new ArrayList<>();

      Cursor c = moviDb.SelectClientes();

      HashMap<String,String> hashMap=new HashMap<String, String>();

      while (c.moveToNext()) {
        hashMap.put("nombre",c.getString(c.getColumnIndex("PERSONA_NOMBRE")));
        hashMap.put("cedula",c.getString(c.getColumnIndex("PERSONA_CEDULA")));
        hashMap.put("codigo",c.getString(c.getColumnIndex("PERSONA_NUMERO")));

        list.add(new Datos(hashMap.get("nombre"), hashMap.get("cedula"), hashMap.get("codigo")));  
      }

      ArrayAdapter Adaptador = new Adaptador(this, R.layout.item, list);

      view.setAdapter(Adaptador);
   }
}

Data

public class Datos {

    private String nombre;
    private String cedula;
    private String numero;

    public Datos(String nombre, String cedula, String numero) {

        this.nombre = nombre;
        this.cedula = cedula;
        this.numero = numero;

    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getCedula() {
        return cedula;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }
}

Adapter

public class Adaptador extends ArrayAdapter {

    Context context;
    int resource;
    ArrayList<Datos> datos;

    public Adaptador(Context context, int resource, ArrayList<Datos> list) {
        super(context, resource, datos);
        this.context = context;
        this.resource = resource;
        this.datos = datos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(resource, parent, false);

        TextView tvNombre = (TextView) convertView.findViewById(R.id.nombre);
        TextView tvCedula = (TextView) convertView.findViewById(R.id.cedula);
        TextView tvNumero = (TextView) convertView.findViewById(R.id.numero);

        String nombre = datos.get(position).getNombre();
        String cedula = datos.get(position).getCedula();
        String numero = datos.get(position).getNumero();

        tvNombre.setText(nombre);
        tvCedula.setText(cedula);
        tvNumero.setText(numero);

        return convertView;
    }
}

Item

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

    <TextView
        android:id="@+id/nombre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="nombre"
        android:textSize="18sp"
        android:textColor="@android:color/black"/>

    <TextView
        android:id="@+id/cedula"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="cedula"
        android:textSize="18sp"
        android:textColor="@android:color/black"/>

    <TextView
        android:id="@+id/numero"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="cedula"
        android:textSize="18sp"
        android:textColor="@android:color/black"/>

</LinearLayout>
    
answered by 01.09.2017 / 18:08
source