Add interaction in listview!

2

I have a problem with my listview . I'm listing all the cars the user has, but when he clicks I want to get the information from that car to process, except that in the tutorials I see the guys did a listview using a class that extended from < in> arrayadapter and they use an array to get the information, unlike what I have.

I'd like a tip on how to do it. My class where it has the listing:

package comviewzonazul.google.httpssites.zonazul.carro.gui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import comviewzonazul.google.httpssites.zonazul.R;
import comviewzonazul.google.httpssites.zonazul.cliente.dominio.Cliente;
import comviewzonazul.google.httpssites.zonazul.cliente.gui.PrincipalClienteActivity;
import comviewzonazul.google.httpssites.zonazul.cliente.negocio.ClienteNegocio;
import comviewzonazul.google.httpssites.zonazul.infraestrutura.DatabaseHelper;
import comviewzonazul.google.httpssites.zonazul.usuario.negocio.UsuarioNegocio;

public class GerenciarCarroActivity extends AppCompatActivity {
    SQLiteDatabase database;
    Cursor cursor;
    SimpleCursorAdapter ad;
    ListView lista;
    String login;
    private DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gerenciar_carro);
        SharedPreferences preferences = getSharedPreferences("LoginActivityPreferences", MODE_PRIVATE);
        login             = preferences.getString("LOGIN", null);
        databaseHelper = new DatabaseHelper(getApplicationContext());
        buscarDados();
        Listagem();
    }
    public int buscarIdCliente(){
        UsuarioNegocio usuarioNegocio = new UsuarioNegocio(getApplicationContext());
        int id_usuario = usuarioNegocio.pegarId(login);
        ClienteNegocio clienteNegocio = new ClienteNegocio(getApplicationContext());
        Cliente cliente = clienteNegocio.retornaCliente(id_usuario);
        return cliente.getId();
    }

    private SQLiteDatabase getDatabase(){
        if (database == null){
            database = databaseHelper.getWritableDatabase();
        }
        return database;
    }

    public void buscarDados(){
        cursor = getDatabase().rawQuery("SELECT A." + DatabaseHelper.Carros.PLACA + ", A." + DatabaseHelper.Carros.MODELO + ", A." + DatabaseHelper.Compra.ID + ", A."
                + DatabaseHelper.Carros.FABRICANTE + ", A." + DatabaseHelper.Carros.COR +  " FROM " + DatabaseHelper.Carros.TABELA_CARROS
                + " as A INNER JOIN " + DatabaseHelper.CarroCliente.TABELA_CARROCLIENTE +
                " as B ON A." + DatabaseHelper.Carros.ID + " = B." + DatabaseHelper.CarroCliente.ID_CARRO
                + " WHERE B." + DatabaseHelper.CarroCliente.ID_CLIENTE + " = ?", new String[] {Integer.toString(buscarIdCliente())});

        if(cursor!=null){
            cursor.moveToFirst();
        }
    }

    public void Listagem(){
        lista = (ListView)findViewById(R.id.listView_car);
        String[] from = { DatabaseHelper.Carros.PLACA, DatabaseHelper.Carros.FABRICANTE, DatabaseHelper.Carros.MODELO, DatabaseHelper.Carros.COR};
        int [] to = {R.id.placa, R.id.fabricante, R.id.modelo, R.id.cor};
        ad = new SimpleCursorAdapter(getApplicationContext(), R.layout.listar_carro,cursor,from,to);
        lista.setAdapter(ad);
    }

    public void remover(View view){
        Toast.makeText(this.getApplicationContext(), DatabaseHelper.Carros.PLACA, Toast.LENGTH_SHORT).show();

    }

    public void adicionar(View view){
        Intent intent = new Intent();
        intent.setClass(this, AdicionarCarro.class);
        startActivity(intent);
        finish();

    }


    public void voltar(View view) {
        Intent intent = new Intent();
        intent.setClass(this, PrincipalClienteActivity.class);
        startActivity(intent);
        finish();
    }
}
    
asked by anonymous 15.08.2017 / 15:44

1 answer

1

Right, you have to have pre-defined values to do what you want. For example, if you click the value of a Ferrari in your listview, does it query a list or vector that brings the information they have based on the parameter "Ferrari" understood? And the adaptations made, are custom ListViews, where you can add more texts or even images in your listview.

In your case let's say you could do something like this:

  lista.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            String item = ((TextView)view).getText().toString();

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

        }
    });

Place this code snippet on your onCreate in the last line, which when you click will appear a Toast with the item clicked.

    
15.08.2017 / 15:47