有人可以幫我,我寫這個程序,但它不工作,我沒有找到解決辦法。我的程序To_do列表不起作用,因爲SQLite的
1錯誤:
java.lang.illegalstateexception could not read row 0 col 1 from cursor window. Make sure the cursor is initialized correctly before accessing data from it
第二個錯誤:
android.database.sqlite.SQLite Exception: near "=": syntax error (code 1): while compiling: SELECT ZAHL1 FROM Verlauf WHERE ID =
package com.example.to_doliste2;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivityT extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t);
datasource = new VerlaufDataSource(this);
}
private VerlaufDataSource datasource;
List<Entry> AufgabenListe = new ArrayList<Entry>();
public void VerlaufKlick(View view)
{
try
{
String zahl1;
EditText Feld1 = (EditText)findViewById(R.id.editText1);
if (Feld1.getText().toString().length() == 0)
{
return;
}
zahl1 = (Feld1.getText().toString());
Feld1.setText(String.valueOf(zahl1));
try
{
datasource.open();
datasource.createEntry(zahl1);
datasource.close();
}
catch (Exception ex)
{
Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
}
AufgabenListe.clear();
try
{
datasource.open();
AufgabenListe = datasource.getAllEntries();
datasource.close();
}
catch (Exception ex)
{
Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
}
ArrayAdapter<Entry> adapterVerlauf = new ArrayAdapter<Entry>(MainActivityT.this, android.R.layout.simple_list_item_1, AufgabenListe);
ListView lVerlauf = (ListView)findViewById(R.id.listView1);
lVerlauf.setAdapter(adapterVerlauf);
}
catch (Exception ex)
{
Toast.makeText(this,ex.toString(),Toast.LENGTH_LONG).show();
}
}
}
package com.example.to_doliste2;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class MySQLHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "verlaufAufgaben.db";
public static final int DATABASE_VERSION = 1;
private static final String TABLE_CREATE_VERLAUF = ""
+"create table VERLAUF ("
+" ID integer primary key, "
+" Zahl1 int) ";
public MySQLHelper(Context context)
{
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(TABLE_CREATE_VERLAUF);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(MySQLHelper.class.getName(),
"Upgrading database from version " + oldVersion + "to "
+ newVersion + ", which all destroy all old data");
db.execSQL("DROP TABLE IF EXISTS SCANITEM");
onCreate(db);
}
}
package com.example.to_doliste2;
public class Entry {
private int zahl1;
public int getZahl1()
{
return zahl1;
}
public void setZahl1(int zahl1)
{
this.zahl1 = zahl1;
}
@Override
public String toString()
{
return String.format("%d", zahl1);
}
}
package com.example.to_doliste2;
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 VerlaufDataSource {
private SQLiteDatabase database;
private MySQLHelper dbHelper;
private String[] allColumns = {"ZAHL1"};
public VerlaufDataSource(Context context)
{
dbHelper = new MySQLHelper(context);
}
public void open() throws SQLException
{
database = dbHelper.getWritableDatabase();
}
public void close()
{
dbHelper.close();
}
public Entry createEntry(String zahl1)
{
ContentValues values = new ContentValues();
values.put("ZAHL1", zahl1);
long insertId = database.insert("Verlauf", null, values);
Cursor cursor = database.query("Verlauf", allColumns, "ID = " + insertId, null, null, null, null);
cursor.moveToFirst();
return cursorToEntry(cursor);
}
protected List<Entry> getAllEntries()
{
List<Entry> EntriesList = new ArrayList<Entry>();
EntriesList = new ArrayList<Entry>();
Cursor cursor = database.query("Verlauf", allColumns, "ID =" , null, null, null, null, null);
cursor.moveToFirst();
if(cursor.getCount() == 0) return EntriesList;
while (cursor.isAfterLast()==false)
{
Entry entry = cursorToEntry(cursor);
EntriesList.add(entry);
cursor.moveToNext();
}
cursor.close();
return EntriesList;
}
private Entry cursorToEntry(Cursor cursor)
{
Entry entry = new Entry();
entry.setZahl1(cursor.getInt(1));
return entry;
}
}