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

查看:63
本文介绍了如何正确地将数据添加到 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(String, String, String) 是未定义

推荐答案

那是因为您没有与该签名匹配的构造函数.只需制作一个.

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天全站免登陆