How to properly add data to ArrayList - Android

≯℡__Kan透↙ 提交于 2020-01-06 14:46:08

问题


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

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

The Error says The constructor ClipData.Item(String, String, String) is undefined


回答1:


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

}


来源:https://stackoverflow.com/questions/18388629/how-to-properly-add-data-to-arraylist-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!