0

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);
    }
7
  • move the adapter code outside the for loop. Commented Jul 6, 2013 at 12:08
  • It showing me error that create varialble arr.. Commented Jul 6, 2013 at 12:09
  • arr is within the scope of for. declare it outside the for loop Commented Jul 6, 2013 at 12:10
  • But how it is possible? Commented Jul 6, 2013 at 12:12
  • what is not possible? Commented Jul 6, 2013 at 12:14

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

You for loop to only fetch data,Setting adapter that much time only gives you costly memory operation.
ArrayList<String> arr=null; for (GraphUser user : users) { arr = new ArrayList<String>(); 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); Same problem...Only one data
What i am suggesting is,assign memory to your list outside your for loop and use for loop to add data only.You can also put log in for loop to check if you are getting number of data or not.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.