我遇到了将数据保存到表格的问题。我写了一个简单的程序,只要用户点击按钮就可以保存数据库的条目,但是我遇到了问题。它不是保存预定义字符串中的信息,而是始终保存包名称,如com.example.animedb@3424,@之后的数字始终不同。我试着看看调试器,但在我看来,一切都应该工作正常,但它没有:(。保存/删除过程工作正常,因为我解释但它只是保存“不同”的数据,我无法弄清楚为什么感谢一些帮助,提前谢谢。
package com.example.animedb;
import java.util.List;
import android.os.Bundle;
import android.view.Menu;
import java.util.List;
import java.util.Random;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
HandleData hData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hData = new HandleData(this);
hData.open();
List<Entry> newList = hData.getAllEntrys();
ArrayAdapter<Entry> adapter = new ArrayAdapter<Entry>(this,
android.R.layout.simple_list_item_1, newList);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Entry> adapter = (ArrayAdapter<Entry>) getListAdapter();
Entry entry = null;
switch (view.getId()){
case R.id.add:
String[] anime = new String[]{"One piece", "DBZ", "Elfen Lied", "LOGH", "Bepop"};
String [] episodes = new String[]{ "26", "13", "15", "55", "33"};
int nextInt = new Random().nextInt(4);
entry = hData.addEntry(anime[nextInt], episodes[nextInt]);
adapter.add(entry);
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
entry = (Entry) getListAdapter().getItem(0);
hData.deleteEntry(entry);
adapter.remove(entry);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
hData.open();
super.onResume();
}
@Override
protected void onPause() {
hData.close();
super.onPause();
}
}
-------------------------------------
package com.example.animedb;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class HandleData {
private DBHelper dbHelper;
private SQLiteDatabase database;
String comuns[] = { DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME, DBHelper.COLUMN_EPISODES };
public HandleData(Context context){
dbHelper = new DBHelper(context);
}
public void open() throws SQLException{
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public Entry addEntry(String name, String episode){
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME, name);
values.put(DBHelper.COLUMN_EPISODES, episode);
long insertId = database.insert(DBHelper.TABLE_NAME, null, values);
Cursor cursor = database.query(DBHelper.TABLE_NAME, comuns, DBHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Entry comment = cursortocomment(cursor);
cursor.close();
return comment;
}
public void deleteEntry(Entry entry){
long id = entry.getId();
database.delete(DBHelper.TABLE_NAME, DBHelper.COLUMN_ID + " = " + id, null);
}
public List<Entry> getAllEntrys(){
List<Entry> entrys = new ArrayList<Entry>();
Cursor cursor = database.query(DBHelper.TABLE_NAME, comuns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
Entry entry = cursortocomment(cursor);
entrys.add(entry);
cursor.moveToNext();
}
cursor.close();
return entrys;
}
private Entry cursortocomment(Cursor cursor) {
Entry entry = new Entry();
entry.setId(cursor.getLong(0));
entry.setName(cursor.getString(1));
entry.setEpisodes(cursor.getString(2));
return entry;
}
}
--------------------------------
package com.example.animedb;
public class Entry {
private long id;
private String name;
private String episodes;
public Entry(){
}
public Entry(int id, String name, String episodes){
this.id = id;
this.name = name;
this.episodes = episodes;
}
public Entry(String name, String episodes){
this.name = name;
this.episodes = episodes;
}
public long getId() {
return id;
}
public void setId(long l) {
this.id = l;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEpisodes() {
return episodes;
}
public void setEpisodes(String episodes) {
this.episodes = episodes;
}
}
----------------------
package com.example.animedb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "animedatabase";
public static final String TABLE_NAME = "anime";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_EPISODES = "episodes";
public static final String CREATE_TABLE = "Create table " + TABLE_NAME + " ( " + COLUMN_ID + " integer primary key autoincrement , " +
COLUMN_NAME + " text not null, " + COLUMN_EPISODES + " text not null); ";
public DBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
}