如何正确数据添加到ArrayList中 - Android电子 [英] How to properly add data to ArrayList - Android

查看:406
本文介绍了如何正确数据添加到ArrayList中 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的扩展arrayAdapter。我试图变量添加到列表中,但我不完全知道如何做到这一点。这是我目前的ArrayAdapter类。

I have a class the extends arrayAdapter. I am trying to add variables to the list but I am not sure exactly how to do so. Here is my current ArrayAdapter class.

class Item {
                String username;
                String number;
                String content;

            }         
             class ListAdapter extends ArrayAdapter<Item> {    
                public ListAdapter(Context context, int textViewResourceId) {
                    super(context, textViewResourceId);
                    // TODO Auto-generated constructor stub
                }    
                private List<Item> items;    
                public ListAdapter(Context context, int resource, List<Item> items)      
                   {    
                    super(context, resource, items);    
                    this.items = items;    
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    View v = convertView;

                    if (v == null) {

                        LayoutInflater vi;
                        vi = LayoutInflater.from(getContext());
                        v = vi.inflate(R.layout.list_item, null);

                    }

                    final Item p = items.get(position);

                    if (p != 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(p.content);
                        NumbersView.setText(p.number);
                        usernamesView.setText(p.username);





                    }

                    return v;

                }
             }



                ListView yourListView = (ListView) findViewById(android.R.id.list);


                ListAdapter customAdapter = new ListAdapter(DashboardActivity.this, R.layout.list_item, tempList);

                yourListView .setAdapter(customAdapter);

下面是我的数据添加到ArrayList

Here is where I add Data to the ArrayList,

final List <Item> tempList = new ArrayList <Item>();


        String username = json2.getString(KEY_USERNAME);
        String number = json2.getString(KEY_NUMBER);
        String content = json2.getString(KEY_COMMENT);

       //The Error occurs here
        tempList.add ( new Item (username, number,content));

该错误说构造ClipData.Item(字符串,字符串,字符串)是
 未定义

推荐答案

那是因为你没有相匹配的签名的构造函数。简单地做一个。

That's because you don't have a constructor that matches that signature. Simply make one.

class Item {

     String username;
     String number;
     String content;

     public Item (String username, String number, String content){
     this.username = username;
     this.number = number;
     this.content = content;
     }

}

这篇关于如何正确数据添加到ArrayList中 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆