2014-11-09 84 views
0

我試圖在使用預填充數據庫的列表視圖中創建一個Onclicklistener。我不知道如何在這裏添加一些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數據庫Broswer。請幫幫我!謝謝!

+0

您可以使用setOnItemClickListener任何名單。數據庫不是一個例外。 – 2014-11-09 05:44:43

+0

我的回答對你有幫助 – 2014-11-09 06:00:56

回答

1

只需將setOnItemClickListener添加到您的列表中,如下所示。

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

     } 

    }); 
+0

如果我想開始另一個目的,那麼將看到的數據來自ID的內部或其他列?你能告訴我一些如何? – user3398666 2014-11-09 07:12:00

+0

我不明白。你的意思是說你需要去那個具有特定列表ID詳細信息的活動? – 2014-11-09 07:14:50

+0

就是這樣。在我的數據庫表中有6列我已經發布它只列出前三列數據。所以,如果我點擊那個項目,我將被導向到一個頁面,其中3列數據將被顯示。 – user3398666 2014-11-09 07:30:39