2012-01-25 62 views
0

如何遍歷這組數據?處理來自SQLite/Android的數據

String[] from = new String[]{AppSQLite.KEY_NAME,AppSQLite.KEY_ICON}; 
int[] to = new int[]{R.id.main_menu_list_item); 

我想將它們傳遞到自定義簡單的遊標適配器和覆蓋getView(),使 的KEY_NAME是文本(帶的setText可能)和

setCompoundDrawablesWithIntrinsicBounds(**KEY_ICON**, 0, 0, 0); 

我getView是這樣的:

public View getView(int pos, View convertView, ViewGroup parent){ 
    View v = convertView; 
    if(v == null){ 
     LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.icon_list_item, null); 
    } 
    this.c.moveToPosition(pos);  
    String name= this.c.getString(this.c.getColumnIndex(AppSQLite.KEY_NAME)); 
    String icon= this.c.getString(this.c.getColumnIndex(AppSQLite.KEY_ICON)); 

    TextView mName= (TextView) v.findViewById(R.id.icon_textView); 
    bTitle.setText(name); 

    mName.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0); 
    return v; 

在我的數據庫的圖標是這種形式:

R.drawable.menu_icon TEXT 

所以我認爲在setCompoundDrawablesWithIntrinsicBounds中存在另一個問題。

回答

0

圖標參數是一個整數,而不是一個字符串。從你的代碼看來,你傳遞的是一個字符串,這會給你一個編譯錯誤。

更新:在數據庫中存儲一些字符串,並寫入邏輯來加載不同的繪圖。

存儲這些,icon_1,icon_2,icon_3

 
if(icon_1){ 
    use R.drawable.icon_1; 
else if(icon_2) 
    use R.drawable.icon_2; 
and so on.... 
+0

是的,我知道,我不知道如何使它發揮作用?我應該以另一種方式存儲圖像還是有像js中的eval這樣的功能? –

+0

我已經更新了我的答案。 –