After clicking on any ListActivity item, the first record is always loaded

1

Good people, I'm having a problem loading a record from a ListActivity .

When I click on any item in ListActivity , my application should open the registration screen so that I can edit some data. So far, okay .. the problem is that it always loads the first item of ListActivity . It does not matter if there are 100 items, after clicking on any one, always the first one is loaded.

If anyone has any idea of what is being done wrong (or is not being done) and can help there ..

If you need any other information or code, just say.

Thank you!

// File ConsClient.java

@Override
protected void onListItemClick(ListView list, View v, int position, long id) {
    super.onListItemClick(list, v, position, id);

    TextView tvCliente = (TextView) findViewById(R.id.tvCodigo);
    TextView tvNome = (TextView) findViewById(R.id.tvNomeLinha);
    TextView tvCnpj = (TextView) findViewById(R.id.tvCNPJLinha);
    TextView tvEmail = (TextView) findViewById(R.id.tvEmail);

    Intent it = new Intent(this, CadCliente.class);  
    Bundle params = new Bundle();

    String codigoCli = tvCliente.getText().toString();
    String nomeCli = tvNome.getText().toString();
    String cnpjCli = tvCnpj.getText().toString();
    String emailCli = tvEmail.getText().toString();


    params.putString("codigoCli", codigoCli);
    params.putString("nomeCli", nomeCli);
    params.putString("cnpjCli", cnpjCli);
    params.putString("emailClie", emailCli);


    it.putExtras(params);
    startActivity(it);

}

//

public class CadCliente extends Activity{
EditText etNome;
EditText etEmail;
EditText etCodigo;
EditText etCNPJ;

ClienteDAO clienteDAO;
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.cadcliente);
    clienteDAO = new ClienteDAO(this);

    etCodigo = (EditText) findViewById(R.id.etCodCliente);
    etNome = (EditText) findViewById(R.id.etRazaoNome);
    etTelefone = (EditText) findViewById(R.id.etTelefone);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etCNPJ = (EditText) findViewById(R.id.etCpfCnpj);

    //Testes
    Intent intent = getIntent();

    Bundle params = intent.getExtras();  

    if(params!=null)
    {   
     String mostraCodigo = params.getString("codigoCli");
     String mostraNome = params.getString("nomeCli");
     String mostraCNPJ = params.getString("cnpjCli");
     String mostraEmail = params.getString("emailCli");

     etCodigo.setText(mostraCodigo);
     etNome.setText(mostraNome);
     etCNPJ.setText(mostraCNPJ);
     etEmail.setText(mostraEmail);
    }

    //fimTestes

    Button btCancelar = (Button) findViewById(R.id.btCancelarCadCli);
    btCancelar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            finish();
        }
    });


    Button btSalvar = (Button) findViewById(R.id.btSalvarCadCli);
    btSalvar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Variáveis
            String codigo = "";
            String nome = "";
            String email = "";
            String cnpj = "";

            codigo = etCodigo.getText().toString();
            nome = etNome.getText().toString();
            email = etEmail.getText().toString();
            cnpj = etCNPJ.getText().toString();

            //Cria o cliente
            Cliente cliente = new Cliente();

            //Abre a conexão com o banco de dados
            clienteDAO.open();

            //Salva o cliente
            //clienteDAO.novoCliente(cliente);
            clienteDAO.atualizarCliente(cliente);

            //Fecha a conexão com o banco de dados
            clienteDAO.close();

            finish();
        }
    });
}
}

// ClientAdapter file.

package br.sysandroid.dao;

import java.util.List; 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import br.sysandroid.R.id;
import br.sysandroid.R.layout;
import br.sysandroid.model.Cliente;

public class ClienteAdapter extends BaseAdapter {

private Context context;

private List<Cliente> clientes;
private LayoutInflater inflater;

public ClienteAdapter(Context context, List<Cliente> clientes) {
    super();
    this.context = context;
    this.clientes = clientes;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void notifyDataSetChanged() {
    try {
        super.notifyDataSetChanged();
    } catch (Exception e) {
        trace("Erro: " + e.getMessage());
    }
}

private void trace(String msg) {
    toast(msg);
}

public void toast(String msg) {
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

public int getCount() {
    return clientes.size();
}

public void remove(final Cliente cliente) {
    this.clientes.remove(cliente);
}

public void add(final Cliente clientes) {
    this.clientes.add((Cliente) clientes);
}

public Object getItem(int position) {
    return clientes.get(position);
}

public long getItemId(int position) {
    return position;
}

static class ViewHolder {
    public TextView tvCodigo;
    public TextView tvNome;
    public TextView tvTelefone;
    public TextView tvEmail;
    public TextView tvCNPJ;
    }

public View getView(int position, View convertView, ViewGroup viewGroup) {
    try {

        Cliente cliente = clientes.get(position);

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(layout.linha_cliente, null);

            holder = new ViewHolder();
            holder.tvCodigo = (TextView) convertView.findViewById(id.tvCodigo);
            holder.tvNome = (TextView) convertView.findViewById(id.tvNomeLinha);
            holder.tvEmail = (TextView) convertView.findViewById(id.tvEmail);
            holder.tvCNPJ = (TextView) convertView.findViewById(id.tvCNPJLinha);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvCodigo.setText(String.valueOf(cliente.getId()));
        holder.tvNome.setText(cliente.getNome());
        holder.tvEmail.setText(cliente.getEmail());
        holder.tvCNPJ.setText(cliente.getCnpj());


        return convertView;
    } catch (Exception e) {
        trace("Erro: " + e.getMessage());
    }

    return convertView;
}

}

    
asked by anonymous 18.08.2014 / 15:07

2 answers

1

The reason for always getting the first item is related to how you are retrieving this selected item.

Instead of getting the item by the value of View , grab the selected item directly from your Adapter . As below:

Cliente cliente = (Cliente) getListAdapter().getItem(position);

Intent it = new Intent(this, CadCliente.class);  
Bundle params = new Bundle();

// os param.putString com o cliente

This will even summarize your code.

    
18.08.2014 / 16:53
1

Perhaps making the findViewById in the View that was clicked will work:

TextView tvCliente = (TextView) v.findViewById(R.id.tvCodigo);
TextView tvNome = (TextView) v.findViewById(R.id.tvNomeLinha);
TextView tvCnpj = (TextView) v.findViewById(R.id.tvCNPJLinha);
TextView tvEmail = (TextView) v.findViewById(R.id.tvEmail);
    
18.08.2014 / 16:47