0

我正在开发一个 android 应用程序,我想在 ListView 中显示数据库中的一些数据,其中包含标题和副标题。我做了一个扩展 SimpleCursorAdapter 的适配器,它可以工作,但它显示了 2 个第一个字段,没有我在 BindView 方法中选择的内容。我试图找到错误,但我不能,可能这真的很愚蠢,但你自己很难找到错误:-)所以这就是适配器的代码:

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

这是我使用适配器的地方:

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

    }

}

真的很感谢你的帮助。

4

1 回答 1

1

您没有覆盖 的bindView方法SimpleCursorAdapter,而是定义了一个新方法bindview(不是大写!),它在任何地方都没有调用。

因为您的新方法在公共类中是公共的,所以编译器不会警告您它未使用。如果你使用了@Override注解,你的编译器会警告你,你实际上并没有覆盖一个方法......

于 2013-08-30T10:49:21.067 回答