更新数据库后如何更新ListView?

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

这是我正在使用的 ListView,但是在更新数据库后,我无法刷新我的 ListView,我想我应该在重启方法中加入一些代码?但不确定该怎么做。有帮助吗?

只需使用新数据(ArrayList 名称)更新您的列表并执行此代码

namesAdapter.notifyDataSetChanged();

你做错了:

循环调用以下代码:

AdapterForNames namesAdapter = new AdapterForNames(this,names);
namesAdapter.notifyDataSetChanged();
mListView.setAdapter(namesAdapter);

相反,您应该在循环外仅执行一次以下三项任务:
1. 创建适配器,
2. 将其设置为列表视图,
3. setOnItemClickListener,
所以移动到循环外的两行以下:

AdapterForNames namesAdapter = new AdapterForNames(this,names);
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);
            }
        });

在循环内部你应该调用:

namesAdapter.notifyDataSetChanged();

Note: You only need to update the datasource after you set the Adapter on list, and notify the adapter that data is updated. Rest all is one time process.

希望对您有所帮助!

像下面这样注册广播接收器

public class dbUpdateReceiver extends BroadcastReceiver {    
        @Override
        public void onReceive(Context context, Intent intent) {    
            names.add("newly added name");
            namesAdapter.notifyDataSetChanged();
        }
    }

</code></p> <pre><code> IntentFilter filter = new IntentFilter("db_update_action"); registerReciever(new dbUpdateReceiver(), filter);

每当添加如下新记录时发送此操作

Intent intent=new Intent("db_update_action");
sendBroadCast(intent);

更改以下代码:

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

使用以下代码:

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

希望对您有所帮助!