listview-android studiod 从数据库读取数据失败

问题描述

android studiod 从数据库读取数据失败

从数据库读取数据到listview,但是不知为什么,在没添加数据进数据库的时候,能进activity,但是一旦添加进去以后就开始报错了
DBhelper.java
public class DBHelper extends SQLiteOpenHelper{
private final static String DATABASE_NAME = "demo.db";
private final static int DATABASE_VERSION = 1;

private final static String TABLE_NAME = "request_list";
public final static String  REQUEST_ID = "request_id";
public final static String REPORTER_NAME = "reporter_name";
public final static String S_DESCRIPTION = "s_description";
public final static String L_DESCRIPTION = "l_description";
public final static String DEPARTMENT = "department";
public final static String CATEGORY = "category";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.e("Database operations", "Database created/opened");

}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE " + TABLE_NAME +
            " (" + REQUEST_ID     + " INTEGER primary key autoincrement, " +
            ""    + REPORTER_NAME + " text, " +
            ""    + S_DESCRIPTION + " text, " +
            ""    + L_DESCRIPTION + " text, " +
            ""    + DEPARTMENT     + " text, " +
            ""    + CATEGORY       + " text);";
    db.execSQL(sql);
    Log.e("Database operations", "Table created/opened");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(sql);
    onCreate(db);
}

public Cursor select() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db
            .query(TABLE_NAME, null, null, null, null, null, null);
    return cursor;
}

public  Cursor getInformation(SQLiteDatabase db)
{
    Cursor cursor;
    String[] projection = {REPORTER_NAME,S_DESCRIPTION,L_DESCRIPTION,DEPARTMENT,CATEGORY};

    cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null);
    return  cursor;
}
//增加操作
public long insert(String s_description,String name,String department, String category,String l_description)
{
    SQLiteDatabase db = this.getWritableDatabase();

/* ContentValues */
ContentValues cv = new ContentValues();
cv.put(S_DESCRIPTION, s_description);
cv.put(L_DESCRIPTION,l_description);
cv.put(REPORTER_NAME,name);
cv.put(DEPARTMENT,department);
cv.put(CATEGORY,category);

    long row = db.insert(TABLE_NAME, null, cv);
    return row;
}
//删除操作
public void delete(int id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String where = REQUEST_ID + " = ?";
    String[] whereValue ={ Integer.toString(id) };
    db.delete(TABLE_NAME, where, whereValue);
}
//修改操作
public void update(int id, String s_description,String name,String department, String category,String l_description)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String where = REQUEST_ID + " = ?";
    String[] whereValue = { Integer.toString(id) };

    ContentValues cv = new ContentValues();
    cv.put(S_DESCRIPTION, s_description);
    cv.put(L_DESCRIPTION,l_description);
    cv.put(REPORTER_NAME,name);
    cv.put(DEPARTMENT,department);
    cv.put(CATEGORY,category);
    db.update(TABLE_NAME, cv, where, whereValue);
}

}

DataListActivity.java
public class DataListActivity extends ActionBarActivity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
DBHelper dbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;

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

    listView = (ListView) findViewById(R.id.list_view);

    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.reporter_my_request);
    listView.setAdapter(listDataAdapter);

    dbHelper = new DBHelper(getApplicationContext());
    sqLiteDatabase = dbHelper.getReadableDatabase();
    cursor = dbHelper.getInformation(sqLiteDatabase);
    if(cursor.moveToFirst())
    {
        do {
            String name,title,department,category,description;
            name = cursor.getString(0);
            title= cursor.getString(1);
            department=cursor.getString(2);
            category=cursor.getString(3);
            description=cursor.getString(4);

            DataProvider dataProvider=new DataProvider(name,title,department,category,description);

            listDataAdapter.add(dataProvider);

        }while (cursor.moveToNext());

        }
    dbHelper.close();

    }

}

    DataProvider.java
    public class DataProvider {

private String name;
private String title;
private String department;
private String category;
private String description;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public DataProvider(String name,String title,String department,String category,String description){
    this.name= name;
    this.title = title;
    this.department= department;
    this.category= category;
    this.description = description;

}

}

ListDataAdapter.java
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);

}

static class LayoutHandler{

    TextView NAME,TITLE,DEPARTMENT,CATEGORY,DESCRIPTION;
}
@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutHandler layoutHandler;
    if(row == null){
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.reporter_my_request,parent,false);
        layoutHandler = new LayoutHandler();
        layoutHandler.NAME = (TextView)row.findViewById(R.id.text_name);
        layoutHandler.TITLE = (TextView)row.findViewById(R.id.text_title);
        layoutHandler.DEPARTMENT = (TextView)row.findViewById(R.id.text_department);
        layoutHandler.CATEGORY = (TextView)row.findViewById(R.id.text_category);

        row.setTag(layoutHandler);

    }
    else
    {
        layoutHandler = (LayoutHandler)row.getTag();

    }
    DataProvider dataProvider = (DataProvider)this.getItem(position);
    layoutHandler.NAME.setText(dataProvider.getName());
    layoutHandler.TITLE.setText(dataProvider.getTitle());
    layoutHandler.DEPARTMENT.setText(dataProvider.getDepartment());
    layoutHandler.CATEGORY.setText(dataProvider.getCategory());
    layoutHandler.DESCRIPTION.setText(dataProvider.getDescription());

    return row;
}

}

解决方案

至少把报错的信息贴出来啊,你这么一大段代码贴上来怎么帮你看

时间: 2024-09-15 11:43:52

listview-android studiod 从数据库读取数据失败的相关文章

模块化-Android画图表怎么读取数据

问题描述 Android画图表怎么读取数据 安卓可以用AChartEngine画各种图表,比如折线图.饼图.柱状图等,但是网上找到的程序都是直接把数据写到程序里,如何能够模块化,使得程度能够读取数据,直接画出饼图或柱状图,求高人指点???!!! 解决方案 你做一个提供数据源的模块就行了,比如从数据库或者http加载数据

基于echarts+ajax数据库读取数据并返回前端

1.echarts的官网上的demo,都是直接写死的随机数据,没有和数据库的交互,所以就自己写了一下,ok,我们开始一步一步走一遍整个流程吧. 就以官网最简单的那个小demo来做修改吧.官网上的小demo的效果图如下:(很熟悉,有没有) 2.按照echarts的使用方法新建一个echarts.html文件.为ECharts准备一个具备大小(宽高)的Dom(讲的有点细,熟悉的朋友直接跳过) <!DOCTYPE html><head>     <meta charset=&quo

MapX从数据库读取数据形成新图层(C#)

数据|数据库 MapX从数据库读取数据形成新图层(C#) 2005-5-10在C#中实现MapX从数据库读取数据形成新图层分为两个问题:1.         MapX从数据库读取数据形成新图层:2.         将DataTable转换为ADO的Recordset.这里的第二个问题是由第一个问题引起的,因为MapX是一个COM控件,而且它只支持ADO的数据访问方式,而C#编程时一般会使用ADO.NET方式,为此需要在两种方式之间做一下转换.(当然也可以在C#中使用ADO方式,但这毕竟是落后的

struts-bean定义出错页面读取数据失败

问题描述 bean定义出错页面读取数据失败 刚开始提示Book错误.根据eclipse提示添加Book类以后没有进行任何操作,再运行,页面可以跳转,但是提示b.name b.price b.num出错,并且页面跳转成功时获取值失败正确获取值应该显示为请问怎么改才对? 解决方案 调好了 不用单独新建Book类,在BookAction.java.里面直接定义class Book{}就可以了,程序运行成功 解决方案二: 还是不对,还是错的 大神们 求指教啊

求一个能从数据库读取数据并显示到坐标图上的java程序

问题描述 求一个能从数据库读取数据并显示到坐标图上的java程序 新手学java,但是现在急需一个能从数据库读取数据并显示到坐标图上的java程序,希望各位能够给一点帮助,谢谢了 解决方案 http://www.open-open.com/lib/view/open1337915511824.htmlhttp://blog.csdn.net/sjf0115/article/details/7059407 解决方案二:

highcharts如何从SQL Server数据库读取数据生成饼状图

问题描述 highcharts如何从SQL Server数据库读取数据生成饼状图 highcharts如何从SQL Server数据库读取数据生成饼状图 解决方案 这个也是我想关注的问题

数据库读取数据数组格式转换

问题描述 数据库读取数据数组格式转换 array(5) { [0]=> array(2) { ["id"]=> string(1) "1" ["user_name"]=> string(5) "fuwe" } [1]=> array(2) { ["id"]=> string(1) "2" ["user_name"]=> string

分组 排列-从数据库读取数据,到模板输出时按照字段catname的首字母顺序进行分组排列怎么实现?

问题描述 从数据库读取数据,到模板输出时按照字段catname的首字母顺序进行分组排列怎么实现? 如: A-E 阿伟点金 曹氏点金 曹向阳 F-J 何昆宗 海啸 黄建超 韩烨 金界传奇 K-O 刘宁波 刘云灵 李兴淼 李生论 零点拓 P-T 乔振华 孙建发 石勇 U-Z 吴量 沃伦点 小张论金

从数据库读取数据后将其输出成html标签的三种方法_javascript技巧

也许很多人从数据库中读的数据是不需要数据成html标签的,但是也许有一天你们会发现当我们需要输出成html标签时编译器却自动帮我们输出成字符串了这是我们可以这样来 方法1: 最常用的方法,使用JS或JQ JQ: $("#div").html("<span>我是HTML代码</span>"); JS: var dobj=document.getElementById("div"); dobj.innerHTML = &quo