我试图设置一个帐户活动以作为 onItemClick 的结果显示,并将帐户信息存储在我希望根据在列表视图中单击的相应帐户名称显示的 SQLite 数据库中。但是,当我尝试从数据库中选择一行,遍历它,并将其作为 ArrayList 返回时,我收到一个错误,我想几乎是说没有创建 Arraylist。我似乎找不到问题所在。
这是我创建 getRow() 方法的数据访问对象类,我认为这是问题所在。
`package com.example.hardwoodassistant;
import java.util.ArrayList;
import java.util.List;
import com.example.hardwoodassistant.Comment;
import com.example.hardwoodassistant.MySQLiteHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class CommentsDataSource {
// Database fields
private static SQLiteDatabase database;
private MySQLiteHelper db;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.KEY_NAME, MySQLiteHelper.KEY_STREET, MySQLiteHelper.KEY_CITY,
MySQLiteHelper.KEY_PHONE, MySQLiteHelper.KEY_EMAIL};
public CommentsDataSource(Context ctx) {
db = MySQLiteHelper.getInstance(ctx);
}
public void open() throws SQLException {
database = db.getWritableDatabase();
}
public void close() {
db.close();
}
public Comment createComment(String name, String _name, String street, String _street, String city, String _city,
String phone, String _phone, String email, String _email) {
ContentValues values = new ContentValues();
values.put(name, _name);
values.put(street, _street);
values.put(city, _city);
values.put(phone, _phone);
values.put(email, _email);
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, new String[] {MySQLiteHelper.COLUMN_ID, name},
MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
public void deleteComment(Comment comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return comments;
}
public List<Comment> getNames() {
List<Comment> names = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, new String[] {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.KEY_NAME},
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
names.add(comment);
cursor.moveToNext();
}
cursor.close();
return names;
}
public List<Comment> getRow(int id) {
List<Comment> row = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, new String[] {MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.KEY_NAME, MySQLiteHelper.KEY_STREET, MySQLiteHelper.KEY_CITY, MySQLiteHelper.KEY_PHONE,
MySQLiteHelper.KEY_EMAIL}, MySQLiteHelper.COLUMN_ID + " =" + (id-1),null,null,null, null );
while (cursor.moveToNext()) {
Comment comment = cursorToComment(cursor);
row.add(comment);
}
cursor.close();
return row;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}`
这是显示个人帐户信息的帐户活动。
package com.example.hardwoodassistant;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Customer_account extends Activity {
private CommentsDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_details);
datasource = new CommentsDataSource(this);
datasource.open();
TextView name = (TextView) findViewById(R.id.textView1);
TextView address = (TextView) findViewById(R.id.textView2);
TextView phone = (TextView) findViewById(R.id.textView3);
TextView email = (TextView) findViewById(R.id.textView4);
int position = getIntent().getExtras().getInt("position");
name.setText(datasource.getRow(position).get(0).toString());
address.setText(datasource.getRow(position).get(1).toString() + " " + datasource.getRow(position).get(2).toString());
phone.setText(datasource.getRow(position).get(3).toString());
email.setText(datasource.getRow(position).get(4).toString());
}
}
这是向 onitemclick 显示帐户的列表活动。
package com.example.hardwoodassistant;
import java.util.List;
import com.example.hardwoodassistant.Comment;
import com.example.hardwoodassistant.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Customer_Home extends ListActivity {
private CommentsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_home);
ListView lv = getListView();
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> names = datasource.getNames();
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, names);
setListAdapter(adapter);
}
public void onClick(View view) {
switch(view.getId()) {
case R.id.add:
Intent toBidInfo = new Intent(Customer_Home.this, Bid_customerinfo.class);
toBidInfo.putExtra("isFromAccounts", true);
startActivity(toBidInfo);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(Customer_Home.this, Customer_account.class);
intent.putExtra("position", position);
startActivity(intent);
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
这是日志猫
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread.access$700(ActivityThread.java:151)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.os.Looper.loop(Looper.java:137)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread.main(ActivityThread.java:5293)
11-14 10:41:57.136: E/AndroidRuntime(9457): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 10:41:57.136: E/AndroidRuntime(9457): at java.lang.reflect.Method.invoke(Method.java:511)
11-14 10:41:57.136: E/AndroidRuntime(9457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-14 10:41:57.136: E/AndroidRuntime(9457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-14 10:41:57.136: E/AndroidRuntime(9457): at dalvik.system.NativeStart.main(Native Method)
11-14 10:41:57.136: E/AndroidRuntime(9457): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-14 10:41:57.136: E/AndroidRuntime(9457): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-14 10:41:57.136: E/AndroidRuntime(9457): at java.util.ArrayList.get(ArrayList.java:304)
11-14 10:41:57.136: E/AndroidRuntime(9457): at com.example.hardwoodassistant.Customer_account.onCreate(Customer_account.java:26)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.Activity.performCreate(Activity.java:5250)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
11-14 10:41:57.136: E/AndroidRuntime(9457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
编辑:抱歉,还有一些其他活动涉及我试图保持简短,但这是我为数据库创建评论的活动。
package com.example.hardwoodassistant;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class Bid_verify extends Activity {
private CommentsDataSource datasource;
//boolean isFromAccounts = getIntent().getExtras().getBoolean("isFromAccounts");
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bid_verify);
datasource = new CommentsDataSource(this);
datasource.open();
final ArrayList<Account> accounts = new ArrayList<Account>();
TextView name = (TextView) findViewById(R.id.textView1);
TextView street = (TextView) findViewById(R.id.TextView01);
TextView city = (TextView) findViewById(R.id.TextView02);
TextView phone = (TextView) findViewById(R.id.TextView04);
TextView email = (TextView) findViewById(R.id.TextView03);
name.setText(getIntent().getExtras().getString("name"));
street.setText(getIntent().getExtras().getString("street"));
city.setText(getIntent().getExtras().getString("city"));
phone.setText(getIntent().getExtras().getString("phone"));
email.setText(getIntent().getExtras().getString("email"));
System.out.println(name.getText());
Button next = (Button) findViewById(R.id.button5);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if (isFromAccounts) {
Comment comment = null;
accounts.add(new Account(getIntent().getExtras().getString("name"),
getIntent().getExtras().getString("street"),
getIntent().getExtras().getString("city"),
getIntent().getExtras().getString("phone"),
getIntent().getExtras().getString("email")));
System.out.println(accounts.size());
datasource.createComment("name", getIntent().
getExtras().getString("name"), "street", getIntent().
getExtras().getString("street"), "city", getIntent().
getExtras().getString("city"), "phone", getIntent().
getExtras().getString("phone"), "email", getIntent().
getExtras().getString("email"));
Intent toAccounts = new Intent(Bid_verify.this, Customer_account.class);
startActivity(toAccounts);
}
//else {
//startActivity(new Intent(Bid_verify.this, Bid_jobtype.class));
//}
});
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
SQLiteHelper 类
package com.example.hardwoodassistant;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public final class MySQLiteHelper extends SQLiteOpenHelper {
private static MySQLiteHelper sInstance = null;
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_STREET = "street";
public static final String KEY_CITY = "city";
public static final String KEY_PHONE = "phone";
public static final String KEY_EMAIL = "email";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME
+ " TEXT," + KEY_STREET + " TEXT," + KEY_CITY + " TEXT," + KEY_PHONE + " TEXT,"
+ KEY_EMAIL + " TEXT" + ")";
private MySQLiteHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
public static MySQLiteHelper getInstance(Context ctx) {
if(sInstance == null) {
sInstance = new MySQLiteHelper(ctx.getApplicationContext());
}
return sInstance;
}
public static String getName() {
return DATABASE_NAME;
}
@Override
public void onCreate(SQLiteDatabase database) {
if(database == null);
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}