先生,我正在使用 BaseAdapter 从我的 sqlite 数据库中为 listview 编写代码。我搜索了很多,但找不到像我写的任何代码。所以请帮助我。我有一个表 MONEY,它有四个字段 ID,FIRSTNAME,MONEY,DESCRIPTION。我在这一行出错: text1.setText(list1[position]);
package com.anurag.main;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Money extends Activity {
ArrayList list1;
TextView text1, text2, text3, text4;
ImageView iv;
ListView mList;
SQLiteDatabase db;
private static String DBNAME = "MainDB.db";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.money);
mList = (ListView) findViewById(R.id.mList);
list1 = new ArrayList();
try {
db = openOrCreateDatabase(DBNAME, MODE_WORLD_READABLE, null);
Cursor c = db.rawQuery("select * from MONEY", null);
while (c.moveToNext()) {
list1.add(c.getString(1));
}
} catch (Exception e) {
// TODO: handle exception
}
mList.setAdapter(new MyArrayAdapter(getApplicationContext()));
}
class MyArrayAdapter extends BaseAdapter {
Context context;
public MyArrayAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom1, null);
text1 = (TextView) v.findViewById(R.id.text1);
text1.setText(list1[position]);**// Problem in this line**
// TODO Auto-generated method stub
return null;
}
}
}
实际上我正在从我的数据库中填充数据并希望在列表视图中显示。如果我写错了代码,请告诉我。谢谢