我想做一个评论栏(一个 EditText),其中一个人的评论将被添加到 SQLite DB 中,评论将显示在 ListView 中。我尝试这样做,但最终导致应用程序崩溃。在加载到模拟器时它说。“不幸的是,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();
}
}