1

I have a class called CreateCommentLists, that extends BaseAdapter. How do I set the BaseAdapter class to my ListView? My current class is suppose to take comments, Usernames, and Numbers and place them into seperate textViews but this class does not work. Do I need to call the class or set the ListView to the class? I am not sure how to activate the class.

     class CreateCommentLists extends BaseAdapter{
    Context ctx_invitation;
    String[] listComments;
    String[] listNumbers;
    String[] listUsernames;


    public CreateCommentLists(Context ctx_invitation, String[] comments, String[] Numbers, String[] usernames)
    {
        super();
        this.ctx_invitation = ctx_invitation;
        listComments = comments;
        listNumbers = Numbers;
        listUsernames = usernames;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = null;
        try
        {
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
            v = li.inflate(R.layout.list_item, null);

            TextView commentView = (TextView)v.findViewById(R.id.listComment);
            TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
            TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);


            commentView.setText(listComments[position]);
            NumbersView.setText(listNumbers[position]);
            usernamesView.setText(listUsernames[position]);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return v;
    }
4

3 回答 3

0

创建 CreateCommentLists 的对象“obj”,并在列表视图上调用 listView.setAdadpter(obj)

于 2013-07-15T23:54:07.720 回答
0

像这样:

CreateCommentLists adaper = new CreateCommentLists(context, comments, numbers, usernames);
myListView.setAdapter(adapter):
于 2013-07-15T23:54:32.650 回答
0

在您的活动中,您首先创建一个适配器类的新实例,如下所示:

CreateCommentLists mycmlist = new CreateCommentLists(this, <your comments array>, <your Numbers array>, <your usernames array>);

然后你必须像这样将它设置为你的 ListView:

lstComments = (ListView)findViewById(<lstcomments ID>);
lstComments.setAdapter(mycmlist);

希望这可以帮助...

于 2013-07-15T23:55:55.800 回答