Search code examples
androidlistviewnotifydatasetchanged

notifyDataSetChanged() not working or triggered?


What i'm trying to do:

Inserting value from EditText to database when add button is pressed.

populating the listview with the data's from database.

Problem:

when i click add button it insert the value into the database,but not populating it into listview. when reopen my application,listview refreshed and displays the data's from database.

Here is my code:

public class MainActivity extends AppCompatActivity {

EditText editText;
Button add;
ListView listView;
final DBFunctions db=new DBFunctions(MainActivity.this);
ArrayAdapter<String> adapter;
String[] values;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //xml inflation
    editText= (EditText) findViewById(R.id.editText);
    add= (Button) findViewById(R.id.button);
    listView= (ListView) findViewById(R.id.listView);
    values=db.getAlldata();
    adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,values);
    listView.setAdapter(adapter);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            String name = editText.getText().toString();
            if (!name.equals(""))
            {
                editText.setText("");
                long check = db.insertData(name);
                if (check < 0)
                {
                    Toast.makeText(MainActivity.this, "Error in insert query", Toast.LENGTH_SHORT).show();
                } else
                {
                    adapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this, name + " inserted", Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                editText.setError("this field is empty");
            }

        }

    });
}

did i used adapter.notifyDataSetChanged in correct place.

can anyone give me the solution for this. This will help me alot.

Thanks in advance.


Solution

  • Your adapter is populating the listView with the data it gets from the values array. You are inserting the new data in database but that does not change the values array and hence the adapter does not find anything to change.

    Try changing the values array too. For that using ArrayList instead of Arrays is a better choice.

    public class MainActivity extends AppCompatActivity {
    
    EditText editText; 
    Button add; 
    ListView listView; 
    final DBFunctions db=new DBFunctions(MainActivity.this);
    ArrayAdapter<String> adapter; 
    ArrayList<String> values;
    
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //xml inflation
        editText= (EditText) findViewById(R.id.editText);
        add= (Button) findViewById(R.id.button);
        listView= (ListView) findViewById(R.id.listView);
        values = new ArrayList<String>(Arrays.asList(db.getAlldata()));
        adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,values);
        listView.setAdapter(adapter);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                String name = editText.getText().toString();
                if (!name.equals(""))
                {
                    editText.setText("");
                    long check = db.insertData(name);
                    if (check < 0)
                    {
                        Toast.makeText(MainActivity.this, "Error in insert query", Toast.LENGTH_SHORT).show();
                    } else
                    {
                        values.add(name);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(MainActivity.this, name + " inserted", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    editText.setError("this field is empty");
                }
    
            }
    
        });
    
    
    }