我有一个listview和一个SQL数据库。在我的SQL数据库中,有一个title
和一个content
字段。我在列表视图上显示titles
。现在这就是我想要实现的:当我点击标题时,这应该将我传递给另一个活动,在这个活动中我想在edittext中看到相对content
。抱歉我的英语很差。
这是我的代码。 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 :(得分:0)
您无法将Cursor
转换为List<NoteAlModel>
。您可以将Cursor
与SimpleCursorAdapter
一起使用。
在这种情况下,您的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
与你的&#34;标题&#34;现在让我们管理您的setOnItemClickListener
以打开内容&#34;在新的活动中。
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`
}