2016-03-22 39 views
0

您好,我使用SQL在Android上創建了一個應用程序。當我想設置光標時,我遇到了問題。我不知道我做錯了什麼,因爲我的第一個程序使用光標Array列表。感謝幫助。我該如何修復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; 
} 

}

目錄下載:

enter image description here

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

有什麼不對?你確切的問題是什麼,期望和你有什麼......? – yennsarah

+0

我有錯誤:java.lang.NullPointerException。 – Iorwet

+0

哪裏?發佈你的logcat。 – yennsarah

回答

0

查看here瞭解詳細步驟。

需要初始化你的數據庫,如:

SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

纔可以讀取任何東西。

+0

和getReadableDatabase()一起做什麼? – Iorwet

+0

繼官方教程之後(按照上面的步驟鏈接!),你需要一個[數據庫幫助器類](http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html),它會爲你提供一個數據庫。 – yennsarah

+0

我添加了我所有的ThoughtManager代碼,你能再看一次嗎?我會非常gratefull – Iorwet