Question

I´m developing an android app and I want to display some data from a database in a ListView with a Title and Subtitle. I did an adapter extending SimpleCursorAdapter and it´s working but it shows the 2 first fields and no what I choose in the method BindView. I´m trying to find the error but I can´t, probably it´s something really stupid but that errors is really hard to find by yourself :-) So that's the code of the adapter:

package com.apeirons.apeironsdatabasemanager;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MyListAdapter extends SimpleCursorAdapter {

    //private Context context;
    LayoutInflater inflater;

    @SuppressWarnings("deprecation")
    public MyListAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, R.layout.consult, c, from, to);
        inflater = LayoutInflater.from(context);
    }


    public void bindview (View view, Context context, Cursor cursor){
        TextView titulo = (TextView)view.findViewById(R.id.Titulo);
        titulo.setText(cursor.getString(cursor.getColumnIndex("name")));

        TextView subtitulo = (TextView)view.findViewById(R.id.SubTitulo);   
        subtitulo.setText(cursor.getString(cursor.getColumnIndex("kind_place")));

        subtitulo.append(cursor.getString(cursor.getColumnIndex("score")));
    }


    @Override 
    public View newView (Context context, Cursor cursor, ViewGroup parent){

        View v = inflater.inflate(R.layout.entrys, parent, false);      
        return v;
    }
}

And here is where I use the adapter:

package com.apeirons.apeironsdatabasemanager;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class Consult extends Activity { 
    private ListView list;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.consult);

        list = (ListView)findViewById(R.id.list);

        this.getIntent();

        ApeironsSQLiteHelper apeironsdb = new ApeironsSQLiteHelper(this, "DBApeirons.DB", null, 1);

        db = apeironsdb.getReadableDatabase();

        String[] campos = new String[]{"_id", "name", "kind_place", "score"};

        Cursor c = db.query("Apeirons", campos, null, null, null, null, null);

        int cuenta = c.getCount();

        c.moveToFirst();

        Log.d("CONSULTA", Integer.toString(cuenta));

        Log.d("CURSOR", c.getString(c.getColumnIndex("score")));

        String[] from = new String[]{"name", "kind_place", "score"};

        int [] to = new int[] {R.id.Titulo, R.id.SubTitulo};

        //@SuppressWarnings("deprecation")
        MyListAdapter myadapter = new MyListAdapter (this, 
                R.layout.entrys, c, from, to);

        list.setAdapter(myadapter);

    }

}

Really thank you for your help.

Was it helpful?

Solution

You are not overriding the bindView method of SimpleCursorAdapter, but defining a new method bindview (not the capitalisation!) which is called nowhere.

Because your new method is public in a public class, you won't be warned by the compiler that it is unused. If you had used the @Override annotation, your compiler would have warned you that you don't actually override a method...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top