PinTelele PinTelele - 4 years ago 114
Android Question

How to get key in a object

I have this code


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv.getItemAtPosition(position);
Log.d(TAG, o.toString());

My goal is when the user click on the listview item i want to log cat the IdNotificacao.

but what i get in logcat is this:

{PrioridadeDescr=Normal, Titulo=Cliente de Testes da CliCloud v3: Erro ao ler RSS de ************, AddDate=02/02/2017, TipoDescr=Plugin RSS, IdNotificacao=5199}

I only want to logcat the IdNotificacao=5199.

Anyone know how to do that?

Answer Source

You are using Object.toString() method in the logcat.

You can simply override the method of the specific class you are using in this list.

Since every class extends Object, congratulation, you have override a method!

public MyClass {

    int IdNotificao;


    public String toString(){
        return "IdNotificao : " + IdNotificao;

Note : If this is to complicate for you, stop learning Android and start by the basic of Java, heritance, inner classes, are extremly used in Android.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download