2012-12-23 72 views
0

我嘗試使用listview顯示數據,但此代碼不起作用。它不會產生錯誤,也不會顯示列表數據中的數據。類聊天是適配器豆如何列表視圖

Chat[] chat_obj=new Chat[cur.getCount()]; 
int i=0; 
while(cur.moveToNext()){ 
    chat_obj[i]=new Chat(); 
    chat_obj[i].content=cur.getString(3);  
    i++; 
} 
ListView ll = (ListView)findViewById(R.id.contain); 
listChat adapter=new listChat(this, chat_obj); 
ll.setAdapter(adapter); 

類聊天:

public class Chat { 
    String content; 
    String created_time; 
} 

類listChat的適配器文件

public class listChat extends ArrayAdapter<Chat>{ 
     private final Chat[] chat_obj; 
     private final Activity MyContext; 

     public listChat(Activity context, Chat[] obj){ 
     super(context, R.layout.item); 
     this.chat_obj=obj; 
     this.MyContext=context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     LayoutInflater inflater = MyContext.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.item, null, true); 
     TextView content=(TextView)rowView.findViewById(R.id.content); 
     content.setText("xxxxxxxxxxx"); 
     return rowView; 
     } 
} 

佈局XML項目item.xml

<?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" > 
     <TextView android:id="@+id/content" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="xxx"/> 
    </LinearLayout> 

請幫助我!

回答