How do I alphabetize my list?

Asked

Viewed 2,809 times

1

I have a ListView and I’d like to alphabetize the names of the clients. I urge you to be as specific as possible because I don’t have much experience.

  import static android.R.id.list;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import br.gestaoBd.BancoDeDados.ClienteDao;
  import br.gestaoBd.Beans.Cliente;
  import br.gestaoBd.listaadapters.ClienteAdapter;
  import java.util.ArrayList;
  import java.util.Collections;


  public class ListClientes extends Activity implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

ListView lista;
ArrayList<Cliente> clientes;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_clientes);

    lista = (ListView) findViewById(R.id.listview);

    lista.setOnItemLongClickListener(this);
    lista.setOnItemClickListener(this);


    atualizar(null);
}

public void atualizar(View view) {
    ClienteDao cliDao = new ClienteDao();

    clientes = cliDao.getListagem();
    lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

}

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Intent cadClienteIntent = new Intent(this, CadCliente.class);
    cadClienteIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadClienteIntent);
    return true;
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent cadPedidoIntent = new Intent(this, CadPedido.class);
    cadPedidoIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadPedidoIntent);

}

}

My Client Adapter:

  import android.content.Context;
  import android.util.Log;
  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 br.gestaoBd.Beans.Cliente;
  import br.gestaoBd.Mask;
  import br.gestaoBd.R;
  import java.util.List;

  public class ClienteAdapter extends BaseAdapter {  

private Context context;
private List<Cliente> clientes;

public ClienteAdapter(Context context, List<Cliente> clientes) {
    this.context = context;
    this.clientes = clientes;
}

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

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

public long getItemId(int position) {
    return clientes.get(position).getId();
}  


public View getView(int position, View convertView, ViewGroup parent) {
    Cliente cliente = clientes.get(position);

    LayoutInflater layout = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = layout.inflate(R.layout.linhacli, null);

    Log.i("AULA", "Montou:" + cliente.getNome());
    Log.e("ERRO", "Valor da variavel estava nullo!");

    ImageView imgImageView = (ImageView) view.findViewById(R.id.imageView1);
    imgImageView.setImageResource(R.drawable.sem_foto);

    TextView edNome = (TextView) view.findViewById(R.id.textView1);
    edNome.setText(cliente.getNome());

    TextView lblTelefone = (TextView) view.findViewById(R.id.textView2);
    lblTelefone.setText(cliente.getTelefone());


    return view;
}
}

my list

 import static android.R.id.list;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import br.gestaoBd.BancoDeDados.ClienteDao;
 import br.gestaoBd.Beans.Cliente;
 import br.gestaoBd.listaadapters.ClienteAdapter;
 import java.util.ArrayList;
 import java.util.Collections;


 public class ListClientes extends Activity implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

ListView lista;
ArrayList<Cliente> clientes;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_clientes);

    lista = (ListView) findViewById(R.id.listview);

    lista.setOnItemLongClickListener(this);
    lista.setOnItemClickListener(this);


    atualizar(null);
}

public void atualizar(View view) {
    ClienteDao cliDao = new ClienteDao();

    clientes = cliDao.getListagem();
    lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

}

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Intent cadClienteIntent = new Intent(this, CadCliente.class);
    cadClienteIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadClienteIntent);
    return true;
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent cadPedidoIntent = new Intent(this, CadPedido.class);
    cadPedidoIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadPedidoIntent);


}

}
  • 2

    Wouldn’t it be easier to order your Customer Arraylist and have them organized? You can do this using Comparator, see this answer on Soen

1 answer

5


To sort the list we will use the method sort class Collections.

This method requires two parameters:

  1. The list that will be ordered.

  2. The interface Comparator, which will have the ordering logic.

To implement this interface, we have to inform the type of object we will work, in this case the Cliente.

new Comparator<Cliente>()

This object must be of the same type as that reported in the List.

To compare String, we use the method compareTo.

This is because String implements Comparable.

To compare int , we follow the following logic:

If the first integer is smaller than the second, we return -1 (or any negative).

If the first integer is greater than the second, we return 1 (or any positive).

If they are equal, then we return 0.

Your code must look like this:

 clientes = cliDao.getListagem();
    Collections.sort(clientes, new Comparator<Cliente>() {
            @Override
            public int compare(Cliente o1, Cliente o2) {
                return o1.getNome()).compareTo(o2.getNome());
            }

        });

 lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

If you want to sort in various parts of your code this list, we can directly implement the interface Comparable in the Cliente.

So it will not be necessary to repeat the Comparator

It would look something like this:

customer class:

public class Cliente implements Comparable<Cliente>{

public int compareTo(Cliente outroCliente) {
      return o1.getNome()).compareTo(o2.getNome());
  }
}

Your list:

 clientes = cliDao.getListagem();
 Collections.sort(clientes);
 lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

Another way would be your method getListage() of DAO already return ordered through the ORDER BY.

Any questions, we are available!

  • I also think it’s simpler to order the arraylist, I just think your answer was wrong in not explaining what is being done. AP asked "to be as specific as possible because I don’t have much experience".

  • 1

    You’re right @Renan ! I edited the answer ! Thanks for the tip!

Browser other questions tagged

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