Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh ListView from ArrayAdapter

I'm using ArrayAdapter to bind my data from my ArrayListto my ListView and i use an AlertDialogto insert data into my Arraylist. My problem is that i'm unable to refresh my ListView after the changes done.

Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config_hidden);
    listView=(ListView) findViewById(R.id.hiddenList);
    xmlFileManager=new XmlFileManager(this);
    addNumber=(Button) findViewById(R.id.addNum);

    addNumber.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
            final View alertDialogView = factory.inflate(R.layout.add_number, null);
            AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
            adb.setView(alertDialogView);
            adb.setTitle(R.string.dialog_title);
            adb.setIcon(R.drawable.phone);

            final AlertDialog alertDialog = adb.create();

            adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                    String number = numberToAdd.getText().toString();
                    if(number.length()>0){
                        xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                        adapter.setNotifyOnChange(true);
                        adapter.notifyDataSetChanged();
                    }
                } });

            adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                } });
            adb.show();
        }
    });
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
    adapter.notifyDataSetChanged();
    adapter.setNotifyOnChange(true);
    listView.setAdapter(adapter);
}
like image 276
113408 Avatar asked Aug 02 '12 21:08

113408


People also ask

How to Refresh ListView Android studio?

To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView. Also note that the method notifyDataSetChanged() has to be called on UI thread.

How do you refresh an ArrayList?

To update or set an element or object at a given index of Java ArrayList, use ArrayList. set() method.

What is notify data set changed?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What is an ArrayAdapter?

ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView.


2 Answers

You are instantiating a new adapter each time. What you have to do is put the line where you instantiate the adapter before the click listener, and in the click listener modify that adapter and call notifyDataSetChanged() on it. You of course have to add some setters to your adapter in order to modify the data.

Has to look similar to this:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_hidden);
listView=(ListView) findViewById(R.id.hiddenList);

//instantiate the adapter (just one time)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());

//assign the adapter to the listview
listView.setAdapter(adapter);

xmlFileManager=new XmlFileManager(this);
addNumber=(Button) findViewById(R.id.addNum);

addNumber.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
        final View alertDialogView = factory.inflate(R.layout.add_number, null);
        AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
        adb.setView(alertDialogView);
        adb.setTitle(R.string.dialog_title);
        adb.setIcon(R.drawable.phone);

        final AlertDialog alertDialog = adb.create();



        adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                String number = numberToAdd.getText().toString();
                if(number.length()>0){
                    xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                    //adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                    //adapter.setNotifyOnChange(true);

                    //set the changed data
                    adapter.setData(xmlFileManager.getHiddenNumbers());

                    //notify that the model changed
                    adapter.notifyDataSetChanged();
                }
            } });

        adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            } });
        adb.show();
    }
});

//adapter.notifyDataSetChanged();
//adapter.setNotifyOnChange(true);
like image 59
User Avatar answered Oct 11 '22 05:10

User


If you have a small ArrayList, to force update a ListView when the ArrayList changes you can use this code snippet:

ArrayAdapter.clear();
ArrayAdapter.addAll(ArrayList);
ArrayAdapter.notifyDataSetChanged();
like image 43
Miguel Risco-Castillo Avatar answered Oct 11 '22 03:10

Miguel Risco-Castillo