2012-02-27 41 views
1

我在努力學習SQLite。我想將來自數據庫(TextView)的信息放到可點擊的ListView(我仍然不知道可點擊的ListView)。我可以得到一些關於如何從我的數據庫獲取數據並將其放在可點擊的ListView上的幫助嗎?下面是一個教程網站的代碼,我想有一個ListView如何從我的數據庫中獲取數據並將其放置在可單擊的ListView上?

Button ViewBack; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sqlview); 

    ViewBack=(Button) findViewById(R.id.bVBack); 
    TextView tv = (TextView) findViewById(R.id.tvSQLinfo); 
    ViewBack.setOnClickListener(this); 

    HotOrNot info = new HotOrNot(this); 
    info.open(); 
    String data = info.geData(); 
    info.close(); 
    tv.setText(data); 

} 
@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    finish(); 

} 

sqlview.xml

<ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" >  
<TextView 
    android:id="@+id/tvSQLinfo" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="10dp" 
    android:text="get info from db" /> 
</ScrollView> 

起初,我試圖把這樣的:

ListView lv = (ListView) findViewById(R.id.listMe); 
ListAdapter listAD = new ListAdapter(this,R.id.listMe, tv); 
lv.setAdapter(listAD); 

,但我得到一個錯誤,無法實例化數據類型ListAdapter,所以我嘗試更換SimpleCursorAdapternew ListAdapter(this,R.id.listMe,tv)

ListAdapter listAD = new SimpleCursorAdapter (this,R.id.listMe,tv, null, null); 

,但它再次給我一個錯誤,它表明我改投地Cursor所以,我改變了這一切,直到它並沒有給我任何錯誤。當我試圖運行它時,它不起作用。這裏的HotOrNotClass

public static final String KEY_ROWID= "_id"; 
public static final String KEY_NAME= "persons_name"; 
public static final String KEY_HOTNESS= "presons_hotness"; 

private static final String DATABASE_NAME= "HotOrNotdb"; 
public static final String DATABASE_TABLE= "peopleTable"; 
private static final int DATABASE_VERSION= 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper{ 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 


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

     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_NAME + " TEXT NOT NULL, " + 
       KEY_HOTNESS + " TEXT NOT NULL);" 
     ); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
      db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE); 
      onCreate(db); 
    } 

} 
public HotOrNot(Context c){ 
    ourContext=c; 
} 

public HotOrNot open() throws SQLiteException{ 
ourHelper = new DbHelper(ourContext); 
ourDatabase =ourHelper.getWritableDatabase(); 

return this; } 

public void close(){ 
    ourHelper.close(); 
} 

public long createEntry(String name, String hotness) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_HOTNESS, hotness); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 

} 

public String geData() { 
    // TODO Auto-generated method stub 
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS}; 
    Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result =""; 
    int iRow=c.getColumnIndex(KEY_ROWID); 
    int iName=c.getColumnIndex(KEY_NAME); 
    int iHotness=c.getColumnIndex(KEY_HOTNESS); 

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){ 
     result=result + c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness)+"\n"; 
    } 
    return result; 

} 
public String getName(long l) throws SQLiteException{ 
    // TODO Auto-generated method stub 
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS}; 
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null); 
    if (c!=null){ 
     c.moveToFirst(); 

     String name=c.getString(1); 
     return name; 
    } 
    return null; 
} 

public String getHotness(long l)throws SQLiteException { 
    // TODO Auto-generated method stub 
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS}; 
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null); 
    if (c!=null){ 
     c.moveToFirst(); 

     String hotness=c.getString(2); 
     return hotness; 
    } 
    return null; 
} 
+0

您能否顯示HotOrNot類的源代碼以及您在嘗試運行時遇到的錯誤? – 2012-02-27 14:24:15

+0

我只是把上面的代碼,當我把它放在這裏看起來凌亂 – 2012-02-27 14:36:49

+0

你有什麼想法如何把這個在列表視圖? – 2012-02-27 15:10:34

回答

3

getData()方法返回一個大String所有的結果。我不認爲這是你想要什麼,你的列表中,因此修改getData()方法,所以你可以返回的String列表代表你的數據庫記錄:

public ArrayList<String> geData() { 
    // TODO Auto-generated method stub 
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS}; 
    Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    ArrayList<String> result = new ArrayList<String>(); 
    int iRow=c.getColumnIndex(KEY_ROWID); 
    int iName=c.getColumnIndex(KEY_NAME); 
    int iHotness=c.getColumnIndex(KEY_HOTNESS); 

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){ 
     result.add(c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness)); 
    } 
    return result; 
} 

爲了顯示一個列表,你會必須把ListView元素在你的Activity佈局(sqlview),所以你可以把這樣的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/bVBack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Something" /> 

    <ListView 
     android:id="@+id/listMe" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

然後在您的onCreate()方法綁定ListViewAdapter

//... 
ListView lv = (ListView) findViewById(R.id.listMe); 
HotOrNot info = new HotOrNot(this); 
info.open(); 
ArrayList<String> data = info.geData(); 
info.close(); 
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item1, data)); 
+0

當我第一次測試它時,它給了我一個錯誤「無法啓動活動ComponentInfo」,所以我搜索了網絡,據說我只需要清理我的項目。非常感謝你回答我的問題,我欠你一個。 – 2012-02-27 15:45:26

相關問題