嗨我使用SQL创建了一个关于android的应用程序。我想设置光标时遇到问题。我不知道我做错了什么,因为我的第一个程序使用带光标的Array列表。谢谢你的帮助。
public class ThoughtManager {
private Thought thought;
private ThoughtDbHelper dbHelper;
private Cursor cursor;
private Context context;
SQLiteDatabase db;
private ContentValues contentValues;
private ArrayList<Thought> allThoughts;
int iTitle;
int iDetail;
int iTime;
int iDate;
int iReason;
int iComment;
private String[] columns=new String[]{ThoughtDbHelper.KEY,
ThoughtDbHelper.TITLE,
ThoughtDbHelper.DETAIL,
ThoughtDbHelper.TIME,
ThoughtDbHelper.DATE,
ThoughtDbHelper.REASON,
ThoughtDbHelper.COMMENT}; // Tablica wypisywanych elementow
public ThoughtManager(Context context)
{
this.context = context; // CO tu dokładnie robimy????
dbHelper=new ThoughtDbHelper(context);
contentValues=new ContentValues();
}
public ThoughtManager openForWrite() // Gdy chcemy zapisywac cos w bazie nowego
{
db=dbHelper.getWritableDatabase();
return this;
}
public ThoughtManager openForRead() // Gdy chcemy odczytywac dane z bazy
{
db=dbHelper.getReadableDatabase();
return this;
}
public void close()
{
dbHelper.close(); // Bezpieczne zamykanie aplikacji
}
public long createThought(Thought thought) // TWORZE NOWY ELEMENT W BAZIE DANYCH!!!
{
contentValues.put(ThoughtDbHelper.TITLE, thought.getTitle());
contentValues.put(ThoughtDbHelper.DETAIL, thought.getDetails());
contentValues.put(ThoughtDbHelper.TIME, thought.getTime());
contentValues.put(ThoughtDbHelper.DATE, thought.getDate());
contentValues.put(ThoughtDbHelper.REASON, thought.getReason());
contentValues.put(ThoughtDbHelper.COMMENT, thought.getComment());
return db.insert(ThoughtDbHelper.TABLE_NAME, null, contentValues);
}
public int updateThought(Thought thought) // Edytuje element W BAZIE DANYCH!!!
{
contentValues.put(ThoughtDbHelper.TITLE, thought.getTitle());
contentValues.put(ThoughtDbHelper.DETAIL, thought.getDetails());
contentValues.put(ThoughtDbHelper.TIME, thought.getTime());
contentValues.put(ThoughtDbHelper.DATE, thought.getDate());
contentValues.put(ThoughtDbHelper.REASON, thought.getReason());
contentValues.put(ThoughtDbHelper.COMMENT, thought.getComment());
return db.update(ThoughtDbHelper.TABLE_NAME, contentValues, ThoughtDbHelper.TITLE + "=" + thought.getTitle(), null);
}
public int deleteThought(Thought thought) // Usuwa element W BAZIE DANYCH!!!
{
return db.delete(ThoughtDbHelper.TABLE_NAME, ThoughtDbHelper.TITLE + "=" + thought.getTitle(), null);
}
public ArrayList<Thought> getAllThoughts() // Pobieramy wszystkie elementy do tablicy
{
//Cursor cursor=db.query(ThoughtDbHelper.TABLE_NAME, columns, null,null,null,null,null);
//cursor.moveToFirst();
cursor = db.query(ThoughtDbHelper.TABLE_NAME, new String[]{ThoughtDbHelper.KEY,
ThoughtDbHelper.TITLE,
ThoughtDbHelper.DETAIL,
ThoughtDbHelper.TIME,
ThoughtDbHelper.DATE,
ThoughtDbHelper.REASON,
ThoughtDbHelper.COMMENT}, (String)null, (String[])null, (String)null, (String)null, (String)null);
iTitle = cursor.getColumnIndex(ThoughtDbHelper.TITLE);
iDetail=cursor.getColumnIndex(ThoughtDbHelper.DETAIL);
iTime=cursor.getColumnIndex(ThoughtDbHelper.TIME);
iDate=cursor.getColumnIndex(ThoughtDbHelper.DATE);
iReason=cursor.getColumnIndex(ThoughtDbHelper.REASON);
iComment=cursor.getColumnIndex(ThoughtDbHelper.COMMENT);
// W petli wpisuje kolejno wszystkie dane do kolejnych tablic w zlaeznosic ile mamy elemetow
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) // (zacznij od poczatku ; rózny(!) od ostatniego ; idz co jeden
{
thought=new Thought(cursor.getString(iTitle), // pobieram tytul
cursor.getString(iDetail), // poberam opis
cursor.getString(iReason), // pobierma czas etc...
cursor.getString(iComment),
cursor.getString(iDate),
cursor.getString(iTime));
allThoughts.add(thought);
}
return allThoughts;
}
public Thought getThought(Thought thought)
{
cursor=db.query(ThoughtDbHelper.TABLE_NAME, columns, null, null, null, null, null);
iTitle=cursor.getColumnIndex(ThoughtDbHelper.TITLE);
iDetail=cursor.getColumnIndex(ThoughtDbHelper.DETAIL);
iTime=cursor.getColumnIndex(ThoughtDbHelper.TIME);
iDate=cursor.getColumnIndex(ThoughtDbHelper.DATE);
iReason=cursor.getColumnIndex(ThoughtDbHelper.REASON);
iComment=cursor.getColumnIndex(ThoughtDbHelper.COMMENT);
if(cursor!=null) // Jezeli mamy jakies dane
{
cursor.moveToFirst(); // przerzuc kursor do poczatku, by odczytywac dane od 1
thought=new Thought(cursor.getString(iTitle), // pobieram tytul
cursor.getString(iDetail), // posberam opis
cursor.getString(iReason), // pobierma czas etc...
cursor.getString(iComment),
cursor.getString(iDate),
cursor.getString(iTime));
allThoughts.add(thought);
}
return thought;
}
}
目录下载:
ThoughtDbHelper:
public class ThoughtDbHelper extends SQLiteOpenHelper{
//Zmienne do mojej bazy danych
static final String TITLE="title";
static final String DETAIL="detail";
static final String REASON="reason";
static final String COMMENT="comment";
static final String KEY="_id";
static final String DATE="date";
static final String TIME="time";
static final String TABLE_NAME="thought_tbl";
static final String DATABASE_NAME="thought_db.sql"; // nasza baza danych
static final int DATABASE_VERSION = 1; // wesja naszej bazy danych
private String QUERY_STRING="CREATE TABLE " + TABLE_NAME + " ( "+KEY
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "+ TITLE
+ " TEXT NOT NULL, " + DETAIL + " TEXT NOT NULL , " + TIME
+ " TEXT, " + DATE + " TEXT, " + REASON + " TEXT, " + COMMENT
+ " TEXT )";
public ThoughtDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(QUERY_STRING);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXIST"+TABLE_NAME);
onCreate(db);
}
}
观点思考:在那里,我想查看我保存的想法
public class ViewThought extends ListActivity {
ThoughtManager manager;
ArrayList<Thought> allThoughts;
ArrayList<String> allTitles;
ListView listView;
Thought thought;
String titlePosition;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.view_thought);
manager=new ThoughtManager(this);
allTitles = new ArrayList<String>();
listView = getListView();
//listView.setBackgroundResource();
listView.setPadding(10, 20, 10, 15);
listView.setFooterDividersEnabled(true);
listView.setHeaderDividersEnabled(true);
d
allThoughts = manager.getAllThoughts();
// Pobieram wszystkie zadania/notatki
for (Thought thought : allThoughts) // od d ostatniego elemetnu
{
allTitles.add(thought.getTitle());
}
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,allTitles));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
/* titlePosition = allTitles.get(position);
//thought=manager.getThought(titlePosition);
dialog=new Dialog(ViewThought.this);
dialog.setContentView(R.layout.view_details);
// 1 sposob
//dialog.setTitle("Thought Detail view");
//2 sposob
TextView tvTime,tvDetail,tvReason,tvComment;
tvComment=(TextView) dialog.findViewById(R.id.tvViewComment);
tvDetail=(TextView) dialog.findViewById(R.id.tvViewDetails);
tvReason=(TextView) dialog.findViewById(R.id.tvViewReason);
tvTime=(TextView) dialog.findViewById(R.id.tvViewTime);
dialog.setTitle(thought.getTitle());
tvComment.setText(thought.getComment());
tvDetail.setText(thought.getDetails());
tvReason.setText(thought.getReason());
tvTime.setText("Created on"+thought.getDate()+"At "+thought.getTime());
dialog.show();
*/
}
}
答案 0 :(得分:0)