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

时间:2016-08-10 12:27:41

标签: android database listview updating

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,我想我应该在重启方法中添加一些代码?但不知道该怎么做。有什么帮助吗?

4 个答案:

答案 0 :(得分:1)

更改以下代码:

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

希望它有所帮助!

答案 1 :(得分:0)

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

namesAdapter.notifyDataSetChanged();

答案 2 :(得分:0)

你做错了:

在循环中调用以下代码:

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

相反,你应该只在循环外进行三次以下任务:
1.创建适配器,
2.在listview上设置,
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();
  

注意:您只需在设置适配器列表后更新数据源,并通知适配器数据已更新。休息一下   是一个时间过程。

希望它有所帮助!

答案 3 :(得分:0)

如下所示注册广播接收器

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

  IntentFilter filter = new IntentFilter("db_update_action");    
  registerReciever(new dbUpdateReceiver(), filter);
每当新记录添加如下

时,

发送此操作

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