如何将类设置为ListView的BaseAdapter - Android

时间:2013-07-15 23:48:22

标签: android class listview baseadapter

我有一个名为CreateCommentLists的类,它扩展了BaseAdapter。如何将BaseAdapter类设置为ListView?我当前的类假设是将注释,用户名和数字放在单独的textViews中,但这个类不起作用。我是否需要调用该类或将ListView设置为类?我不确定如何激活课程。

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

像这样:

CreateCommentLists adaper = new CreateCommentLists(context, comments, numbers, usernames);
myListView.setAdapter(adapter):

答案 2 :(得分:0)

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

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

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

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

希望这会有所帮助......