Looking Forward
Looking Forward

Reputation: 3585

Add dynamic String in ListView in android

I am able to add strings at run time in ListView. But I am only getting the last string data in the ListView. What is the cause of it? Here is my code.

 for (GraphUser user : users) { 
    ArrayList<String> arr = new ArrayList<String>(Arrays.asList(user.getName()));
                                                      ArrayAdapter<String> str = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,arr); 
                                                      lv.setAdapter(str);
                                                      str.setNotifyOnChange(true);
    }

Upvotes: 0

Views: 811

Answers (3)

Looking Forward
Looking Forward

Reputation: 3585

Finally got the solution...Working like a charm...

ArrayList<String> arr = null;
                                    arr = new ArrayList<String>();

                                    for (GraphUser user : users) {

                                        arr.add(user.getName());

                                    }

                                    ArrayAdapter<String> str = new ArrayAdapter<String>(
                                            getBaseContext(),
                                            android.R.layout.simple_list_item_1,
                                            arr);
                                    lv.setAdapter(str);
                                    str.setNotifyOnChange(true);

Upvotes: 0

bgauryy
bgauryy

Reputation: 416

I think that you should override the Adapter class and use changeList method to change the list :

    public void changeList(ArrayList<String>  objects){
        this.objects = objects;
        Collections.sort(objects);
        notifyDataSetChanged(); 
    }

objects: the new ArrayList

you should override the getView method:

    public View getView(int position, View convertView, ViewGroup parent) {         
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_view_custom_layout, parent, false);
        TextView textView = (TextView)rowView.findViewById(R.id.label);
        textView.setText(objects.get(position));
        }       
        return rowView;
    }

list_view_custom_layout : custom Layout xml file ( contains some layout and a textView: "label" at this case )

worked for me.

Upvotes: 0

kaushal trivedi
kaushal trivedi

Reputation: 3443

Your code should be like this

ArrayList<String> arr = new ArrayList<String>
    for (GraphUser user : users) { 
        (arr.add(user.getName()));

        }

ArrayAdapter<String> str = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,arr); 
                                                              lv.setAdapter(str);
                                                              str.setNotifyOnChange(true);

Upvotes: 1

Related Questions