0

リストビューを使用してデータを表示しようとしましたが、このコードは機能しません。エラーは発生せず、リストデータにデータを表示しません。クラスChatは、アダプターのBeanです。

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;
        }
}

ファイルitem.xmlのレイアウト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>

私を助けてください!!

4

1 に答える 1

1

このリンク: http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/で、必要なものを見つけることができます。

通常、BaseAdapter クラスを拡張するクラスを作成する必要があり、そこで getView メソッドを作成する必要があります。その後、アクティビティ クラスで、リストを設定する必要があります。

于 2012-12-23T14:46:10.510 に答える