我正在开发一个 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);
}
}
真的很感谢你的帮助。