Q:

How to update ListView after updating database?

public class SettingsActivity extends AppCompatActivity {
private Context context;
private DogDatabaseHelper dbHelper;
private ListView mListView;
private ArrayList<String> names = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setContentView(R.layout.settings);
    mListView = (ListView)findViewById(R.id.listforall);
    context = this;

    DogDatabaseHelper dbHelper = new DogDatabaseHelper(getApplicationContext());
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Cursor cursor = db.rawQuery("select name from dog",null);
    int count = cursor.getColumnIndex("name");
    if(cursor.moveToFirst()){

        do{
            names.add(cursor.getString(cursor.getColumnIndex("name")));
        }while (cursor.moveToNext());
        AdapterForNames namesAdapter = new AdapterForNames(this,names);
        namesAdapter.notifyDataSetChanged();
        mListView.setAdapter(namesAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(SettingsActivity.this,SetupActivity.class);
                intent.putExtra("name",names.get(position));
                startActivity(intent);
            }
        });
    }
}

This is the ListView I'm using, but after getting database updated, I can't get my ListView refreshed, I guess i should put some code in restart method?  But not sure what to do. Any help?

A:

Change following code:
public class SettingsActivity extends AppCompatActivity {
private Context context;
private DogDatabaseHelper dbHelper;
private ListView mListView;
private ArrayList<String> names = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setContentView(R.layout.settings);
    mListView = (ListView)findViewById(R.id.listforall);
    context = this;

    DogDatabaseHelper dbHelper = new DogDatabaseHelper(getApplicationContext());
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Cursor cursor = db.rawQuery("select name from dog",null);
    int count = cursor.getColumnIndex("name");
    if(cursor.moveToFirst()){

        do{
            names.add(cursor.getString(cursor.getColumnIndex("name")));
        }while (cursor.moveToNext());
        AdapterForNames namesAdapter = new AdapterForNames(this,names);
        namesAdapter.notifyDataSetChanged();
        mListView.setAdapter(namesAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(SettingsActivity.this,SetupActivity.class);
                intent.putExtra("name",names.get(position));
                startActivity(intent);
            }
        });
    }
}

With below code:
public class SettingsActivity extends AppCompatActivity {
    private Context context;
    private DogDatabaseHelper dbHelper;
    private ListView mListView;
    private ArrayList<String> names = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.settings);
        context = this;
        AdapterForNames namesAdapter = new AdapterForNames(this,names);
        mListView = (ListView)findViewById(R.id.listforall);
        mListView.setAdapter(namesAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(SettingsActivity.this,SetupActivity.class);
                intent.putExtra("name",names.get(position));
                startActivity(intent);
            }
        });

        DogDatabaseHelper dbHelper = new DogDatabaseHelper(getApplicationContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select name from dog",null);
        int count = cursor.getColumnIndex("name");
        if(cursor.moveToFirst()){
            do{
                names.add(cursor.getString(cursor.getColumnIndex("name")));
                namesAdapter.notifyDataSetChanged();
            } while (cursor.moveToNext());
        }
    }

Hope it helps!

