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

Asked

Viewed 68 times

1

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

When I click on some item of ListActivity, my application must open the registration screen for me to edit some data. So far, so good.. the problem is that it always loads the first item of ListActivity. No matter if there are 100 items, after clicking on any one, always the first one is loaded.

If anyone has any idea 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 very much!

//Conscliente.java file

@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);

}

//Aquivo Cadcliente.java

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();
        }
    });
}
}

//Clienteadapter 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;
}

}

  • Where is the Adapter?

  • @Igorronner - Added. Thank you!

  • @emauelsm you are using a Listactivity?

  • @Luídne - That’s right. I don’t know where my head was to write Listview. Thanks for the help!

2 answers

1

Maybe making the findViewById in the clicked View works:

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);

1


The reason to always pick up the first item is related as you are recovering that selected item.

Instead of getting the item by the value of View Grab the selected item directly from your Adapter. How to follow:

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

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

// os param.putString com o cliente

That’ll even sum up your code.

  • I got Aki. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.