2016-03-06 117 views
0

我有一個listview和一個SQL數據庫。在我的SQL數據庫中,有一個title和一個content字段。我在列表視圖上顯示titles。現在這就是我想要實現的:當我點擊標題時,這應該將我傳遞給另一個活動,並且在此活動中,我希望在edittext中看到相對content。對於我可憐的英語感到抱歉。從setOnItemClickListener獲取數據並傳遞另一個活動

這是我的代碼。 DB CLASS。

public class NoteAlDatabase extends SQLiteOpenHelper { 


private static final String VERITABANI_ISMI = "veritabanim2.db"; 
private static final int VERITABANI_VERSION = 1; 
private static final String TABLO_ISMI = "noteAlTablosu"; 


public static final String ID = "_id"; 
public static final String NOTEBASLIK= "noteTitle"; 
public static final String NOTEICERIK = "noteContent"; 


final Context c; 
private SQLiteDatabase db; 

public NoteAlDatabase(Context context) { 
    super(context, VERITABANI_ISMI, null, VERITABANI_VERSION); 
    this.c = context; 
} 



@Override 
public void onCreate(SQLiteDatabase db) { 
    String tablo_olustur = "CREATE TABLE " + TABLO_ISMI + 
      " ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ 
      NOTEBASLIK + " TEXT, " + 
      NOTEICERIK + " TEXT);"; 

    db.execSQL(tablo_olustur); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXIST " + TABLO_ISMI); 
    onCreate(db); 

} 

public NoteAlDatabase abrirBaseDeDatos() throws SQLException { 
    NoteAlDatabase noteAlDatabase = new NoteAlDatabase(c); 
    noteAlDatabase.getWritableDatabase(); 
    return this; 
} 


public long addReminder(NoteAlModel noteAl) { 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues cv = new ContentValues(); 
    cv.put(ID, noteAl.getNoteAlID()); 
    cv.put(NOTEBASLIK, noteAl.getNoteAlBaslik()); 
    cv.put(NOTEICERIK, noteAl.getNoteAlIcerik()); 
    // tarih'te eklenecek /long cinsinden 

    long id = db.insert(TABLO_ISMI,null,cv); 
    db.close(); 

    return id; 
} 




public List<NoteAlModel> AllData() { 

    SQLiteDatabase db = this.getReadableDatabase(); 

    String[] sutunlar = new String[]{ID,NOTEBASLIK,NOTEICERIK}; 

    Cursor c =db.query(TABLO_ISMI, sutunlar,null,null,null,null,null); 

    int idsirano = c.getColumnIndex(ID); 
    int basliksirano = c.getColumnIndex(NOTEBASLIK); 
    int iceriksirano = c.getColumnIndex(NOTEICERIK); 

    List<NoteAlModel> liste = new ArrayList<NoteAlModel>(); 

    for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){ 

     NoteAlModel noteAlModel = new NoteAlModel(); 

     noteAlModel.setNoteAlID(c.getString(idsirano)); 
     noteAlModel.setNoteAlBaslik(c.getString(basliksirano)); 
     noteAlModel.setNoteAlIcerik(c.getString(iceriksirano)); 

     liste.add(noteAlModel); 
    } 

    db.close(); 

    return liste; 

} 


public void Sil(){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLO_ISMI, null, null); 
} 

ListViewActivity:

 public class NoteListele extends AppCompatActivity { 
    ListView listView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_note_listele); 

    Button btn = (Button) findViewById(R.id.btnYeniNotKaydet); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(NoteListele.this, NoteAlActivity.class); 
      startActivity(intent); 
     } 
    }); 


    try { 
     displayListview(); 
    } catch (Exception e) { 
     Toast.makeText(NoteListele.this, "Listelenecek veri bulunmamakta", Toast.LENGTH_SHORT).show(); 
    } 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 
    }); 

} 



    public void displayListview() { 
    NoteAlDatabase db = new NoteAlDatabase(getApplicationContext()); 
    List<NoteAlModel> liste = new ArrayList<NoteAlModel>(); 
    liste = db.AllData(); 
    ArrayAdapter arrayAdapter = new ArrayAdapter<NoteAlModel>(this, android.R.layout.simple_list_item_1, liste); 
    listView.setAdapter(arrayAdapter); 
} 

public void deleteAllData(View view) { 
    NoteAlDatabase db = new NoteAlDatabase(getApplicationContext()); 
    db.Sil(); 
    //displayListview(); 
} 

GETSET型號:

public class NoteAlModel { 

private String noteAlID; 
private String noteAlBaslik; 
private String noteAlIcerik; 

public NoteAlModel() { 
} 


public NoteAlModel(String noteAlID, String noteAlBaslik, String noteAlIcerik) { 
    this.noteAlID = noteAlID; 
    this.noteAlBaslik = noteAlBaslik; 
    this.noteAlIcerik = noteAlIcerik; 
} 

public NoteAlModel(String noteAlBaslik, String noteAlIcerik) { 
    this.noteAlBaslik = noteAlBaslik; 
    this.noteAlIcerik = noteAlIcerik; 
} 

public String getNoteAlID() { 
    return noteAlID; 
} 

public void setNoteAlID(String noteAlID) { 
    this.noteAlID = noteAlID; 
} 

public String getNoteAlBaslik() { 
    return noteAlBaslik; 
} 

public void setNoteAlBaslik(String noteAlBaslik) { 
    this.noteAlBaslik = noteAlBaslik; 
} 

public String getNoteAlIcerik() { 
    return noteAlIcerik; 
} 

public void setNoteAlIcerik(String noteAlIcerik) { 
    this.noteAlIcerik = noteAlIcerik; 
} 

logcat的

03-06 20:16:46.095 4886-4886/reminderplus.reminder2 E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: reminderplus.reminder2, PID: 4886 
                    java.lang.NoSuchMethodError: No virtual method AllData()Landroid/database/Cursor; in class Lreminderplus/reminder2/veritabani/NoteAlDatabase; or its super classes (declaration of 'reminderplus.reminder2.veritabani.NoteAlDatabase' appears in /data/data/reminderplus.reminder2/files/instant-run/dex/slice-slice_9_4376acd355cb0a9e536cff445d1b4b60f3d0940d-classes.dex) 
                     at reminderplus.reminder2.noteal.NoteListele.onCreate(NoteListele.java:46) 
                     at android.app.Activity.performCreate(Activity.java:6237) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

回答

0

您必須將Cursor轉換爲List<NoteAlModel>。您可以使用CursorSimpleCursorAdapter

在這種情況下,您AllData方法看起來像:

public Cursor AllData() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String[] sutunlar = new String[]{ID,NOTEBASLIK,NOTEICERIK}; 
    Cursor c = db.query(TABLO_ISMI, sutunlar,null,null,null,null,null); 
    db.close(); 
    return c; 
    } 

然後創建一個SimpleCursorAdapter並將其設置爲你的ListView

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
    android.R.layout.simple_list_item_1, 
    db.AllData(), 
    new String[] { NoteAlDatabase.NOTEBASLIK }, 
    new int[] { android.R.id.text1 }); 
listView.setAdapter(scAdapter); 

之後,你有你的「標題」一個ListView現在讓我們管理您的setOnItemClickListener以在新的活動中打開「內容」。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       String content = ((SimpleCursorAdapter)parent.getAdapter()). 
         getCursor().getString(NoteAlDatabase.NOTEICERIK); 
       Intent intent = new Intent(getApplicationContext(), ContentActivity.class); 
       intent.putExtra("PARAM", content); 
       startActivity(intent); 
      } 
     }); 

現在你可以得到ContentActivity的onCreate方法內容

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_content); 

    //... 
    String content = getIntent().getExtras().getString("PARAM", ""); 
    // show content in some `TextView` 
} 
+0

'了java.lang.RuntimeException:無法啓動活動ComponentInfo {reminderplus.reminder2/reminderplus.reminder2.noteal.NoteListele }:java.lang.NullPointerException:嘗試調用空對象引用虛擬方法'android.database.Cursor reminderplus.reminder2.veritabani.NoteAlDatabase.AllData()'我得到這個錯誤,當我點擊listviewdisplay活動。 – Kaan

+0

請提供上面的完整logcat錯誤。看來,你在空對象上調用AllData()。你之前創建了NoteAlDatabase數據庫對象嗎? – yital9

+0

我加了logcat。 – Kaan

相關問題