2012-10-10 60 views
1

先生,我正在使用BaseAdapter從我的sqlite數據庫中編寫一個listview的代碼。我搜索了很多,但找不到像我寫的任何代碼。所以請幫助我。 我有一個表MONEY它有四個字段ID,FIRSTNAME,MONEY,DESCRIPTION。 我得到這行錯誤:text1.setText(list1 [position]);ListView使用BaseAdapter

package com.anurag.main; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class Money extends Activity { 
ArrayList list1; 
TextView text1, text2, text3, text4; 
ImageView iv; 
ListView mList; 
SQLiteDatabase db; 
private static String DBNAME = "MainDB.db"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.money); 
    mList = (ListView) findViewById(R.id.mList); 
    list1 = new ArrayList(); 
    try { 
     db = openOrCreateDatabase(DBNAME, MODE_WORLD_READABLE, null); 
     Cursor c = db.rawQuery("select * from MONEY", null); 
     while (c.moveToNext()) { 
      list1.add(c.getString(1)); 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 
    mList.setAdapter(new MyArrayAdapter(getApplicationContext())); 

} 

class MyArrayAdapter extends BaseAdapter { 
    Context context; 

    public MyArrayAdapter(Context c) { 
     context = c; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.custom1, null); 
     text1 = (TextView) v.findViewById(R.id.text1); 
     text1.setText(list1[position]);**// Problem in this line** 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 
} 

其實我從我的數據庫填充數據,並希望顯示在列表視圖。請讓我知道如果我寫錯了代碼。 感謝

+0

text1.setText(list1的[位置] ); 此行顯示錯誤 – Fool

+0

什麼是您的日誌貓發佈的錯誤 –

+0

這行代碼顯示此錯誤:表達式的類型必須是數組類型,但它解析爲ArrayList。 那麼正確的代碼應該放在這裏。 – Fool

回答

1

你試圖改變你的

text1.setText(list1[position]); 

text1.setText(list1.get(position)); 
+1

是的,很好的catch,'list1'是一個'ArrayList',不是一個普通的數組。 –

+0

是的我改變了代碼,但它顯示:令牌上的語法錯誤「(」,這個令牌 – Fool

+3

text1.setText(list1.get(position))之後的預期表達式;嘗試像這樣 – Santosh

2

更改線路

View v = inflater.inflate(R.layout.custom1, null); 

convertView = inflater.inflate(R.layout.custom1, null); 

並改變return nullreturn convertView

所以:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.custom1, null); 
     text1 = (TextView) v.findViewById(R.id.text1); 
     text1.setText(list1[position]);**// Problem in this line** 
     // TODO Auto-generated method stub 
     return convertView; 
    } 

另外更改getCount將方法返回數組的大小,所以:

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return list1.size(); 
    } 
相關問題