コメント バー (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();
}
}