繁体   English   中英

使用预先填充的数据库或外部数据库在列表视图中添加OnClickListener

[英]Adding OnClickListener in a listview using prepopulated or external database

我正在尝试使用预填充的数据库在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数据库浏览器。 请帮我! 谢谢!

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

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

使用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

        }

    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM