使用EditText到SQLite数据库中的文本更新ListView

时间:2013-04-03 07:24:00

标签: android sqlite listview android-edittext

我想制作一个评论栏(一个EditText),其中一个评论将添加到SQLite DB中,评论将显示在ListView中。我试过这样做,但我最终得到了一个崩溃的应用程序。在装载到Emulater时,它说。 “不幸的是,SQLproj已经停止了”。我正在嘲笑我的所作所为。请帮帮我。我好几个小时都坚持了

我的activity_main.xml(布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <EditText 
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Please Share Your Valuable Reviews"
        />
   <Button 
       android:id="@+id/button"
       android:text="Submit"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />



    <ListView 
         android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

    </LinearLayout>

我的Pojo课程

package com.example.sqlproj;

public class Comment {

    public long id;
    public String comment;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    @Override
    public String toString()
    {
        return comment;
    }

}

我的SQLiteHelper类:

package com.example.sqlproj;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {

    public  static final String Table_Name="Reviews";
    public  static final String DB_Name="Reviews";
    public  static final int DB_Version=1;
    public  static final String Column_Id="_id";
    public  static final String Column_Name="Comments";
    public  static final String DB_Create="create table" +Table_Name+ "(" +Column_Id+ "integer primary key autoincrement," +Column_Name+"text not null);";

    public MySQLiteHelper(Context context) {
        super(context, DB_Name, null, DB_Version);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DB_Create);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }


}

申请类:

package com.example.sqlproj;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CommentsDataSource {

    public SQLiteDatabase database;
    public MySQLiteHelper dbhelper;
    public String[] allcolumns= {dbhelper.Column_Id,dbhelper.Column_Name};

    public CommentsDataSource(Context context)
    {
        dbhelper= new MySQLiteHelper(context);
    }

    public void open()
    {
        database = dbhelper.getWritableDatabase();
    }

    public void close()
    {
        dbhelper.close();
    }

    public Comment createComment(String comment)
    {
        ContentValues cv = new ContentValues();
        cv.put(dbhelper.Column_Name, comment);
        long id= database.insert(dbhelper.Table_Name, null, cv); 
        Cursor cursor = database.query(dbhelper.Table_Name, allcolumns, null, null, null, null, null);
        cursor.moveToFirst();
        Comment newcomment =  commentSetter(cursor);
        return newcomment;
    }

    public Comment commentSetter(Cursor cursor)
    {
        Comment comment = new Comment();
        comment.setId(cursor.getLong(0));
        comment.setComment(cursor.getString(1));
        return comment;

    }

    public List<Comment> getallComments()
    {
        List<Comment> comments = new ArrayList<Comment>();
        Cursor cursor = database.query(dbhelper.Table_Name, allcolumns, null, null, null, null, null);
        cursor.moveToFirst();
        while(!cursor.isAfterLast())
        {
            Comment comment = commentSetter(cursor);
              comments.add(comment);
              cursor.moveToNext();
        }
        cursor.close();
        return comments;
    }


}

主要活动类别

package com.example.sqlproj;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class MainActivity extends ListActivity {

    public CommentsDataSource datasource;


     @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datasource = new CommentsDataSource(this);
        datasource.open();
        List<Comment> comments= datasource.getallComments();
        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
                android.R.layout.simple_list_item_1, comments);
        setListAdapter(adapter);
     }

     public void onClick(View view)
     { 
         Comment comment=null;
         ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
         EditText edittext = (EditText) findViewById(R.id.edittext);
         String com=edittext.getText().toString();
         datasource.createComment(com);
         adapter.add(comment);
         adapter.notifyDataSetChanged();
     }


     @Override
      protected void onResume() {
        datasource.open();
        super.onResume();
      }

      @Override
      protected void onPause() {
        datasource.close();
        super.onPause();
      }

    } 

2 个答案:

答案 0 :(得分:0)

在MySQLiteHelper中

解决这个问题:

public  static final String DB_Create="create table" +Table_Name+ "(" +Column_Id+ "integer primary key autoincrement," +Column_Name+"text not null);";

到此:

public  static final String DB_Create = "create table " + Table_Name + "(" + Column_Id + " integer primary key autoincrement, " + Column_Name + " text not null " + );";

答案 1 :(得分:0)

在下面的方法中添加此onCreate(db)。因为没有调用onCreate(db) 您的数据库未创建。因为当您使用SQLiteOpenHelper时,首先调用onUpgrade方法。

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
       onCreate(db); // add this line
    }