gpt4 book ai didi

java - 使用预填充或外部数据库在 ListView 中添加 OnClickListener

转载 作者:行者123 更新时间:2023-12-02 05:17:09 26 4
gpt4 key购买 nike

我正在尝试使用预填充数据库在 ListView 中构建 Onclicklistener。我不知道如何在其中添加一些 onclicklistener 。有人可以告诉我怎么做吗?谢谢!

DataListView.java

package com.example.thetrial;

import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DataListView extends Activity {
DBHelper dbhelper;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] from = new String[] { "_id", "comm", "desc" };
int[] to = new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 };
dbhelper = new DBHelper(this);
try {
dbhelper.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Cursor c = dbhelper.getData();

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list, c, from, to);

ListView list = (ListView) findViewById(R.id.ListView1);

list.setAdapter(adapter);
}

}

这是我的 DBHelper.java

package com.example.thetrial;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

private static String DB_NAME = "trialeleventh";
private static int DB_Version = 2;
private SQLiteDatabase db;
private final Context context;
private String DB_PATH;

public DBHelper(Context context) {
super(context, DB_NAME, null, DB_Version);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}

public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();
if (dbExist) {
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
} else {
SQLiteDatabase db = this.getWritableDatabase();
if (db.isOpen()){
db.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
} else {
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
}

private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}

private void copyDataBase() throws IOException {

InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public Cursor getData() {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
onUpgrade(db, DB_Version, 2);
Cursor c = db.rawQuery("SELECT * FROM linux_comm", null);
return c;
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d ("onUpgrade first log", Integer.toString(db.getVersion()));

if (oldVersion == 1) {

DB_Version = 2;
db.setVersion(2);
Log.d ("onUpgrade second log", Integer.toString(db.getVersion()));

}

else {
Log.d("onUpgrade", "else-clause: Already upgraded!");
}
}
}

我正在使用 SQLite 数据库浏览器。请帮我!谢谢!

最佳答案

只需将OnItemClickListener 设置为您的列表,如下所示。

list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
//Write onClick logic here.
}
});

关于java - 使用预填充或外部数据库在 ListView 中添加 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com