1

先生,我正在使用 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;
    }

}
}

实际上我正在从我的数据库中填充数据并希望在列表视图中显示。如果我写错了代码,请告诉我。谢谢

4

2 回答 2

2

换行

View v = inflater.inflate(R.layout.custom1, null);

convertView = inflater.inflate(R.layout.custom1, null);

并更改return nullreturn convertView

所以:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        convertView = 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 convertView;
    }

还要更改您的 getCount 方法以返回数组的大小,因此:

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list1.size();
    }
于 2012-10-10T11:52:56.860 回答
1

你试过改变你的

text1.setText(list1[position]);

text1.setText(list1.get(position));
于 2012-10-10T11:41:40.477 回答