我尝试使用预先填充的数据库在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 Database Broswer。请帮我!谢谢!
答案 0 :(得分:1)
将setOnItemClickListener添加到您的列表中,如下所示。
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
//Write onClick logic here.
}
});
答案 1 :(得分:1)
使用setOnItemClickListener
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView text = ((TextView) view.findViewById(R.id.from));
String from = text.getText().toString();
Toast.makeText(DataListView.this,
from, Toast.LENGTH_SHORT).show();
// your Code here
}
});