Unable to get Listview to refresh from Database

前端 未结 2 728
予麋鹿
予麋鹿 2020-12-18 14:39

No matter what I seem to try and do regarding this listview and trying to get it to dynamically update whenever I hit a button it doesn\'t work and always throws a NPE.

相关标签:
2条回答
  • 2020-12-18 14:58

    Once you have set the adapter you dont need to do .setListAdapter(). Declare you adapter as global variable in your class (as you did at the beginning), and then just call adapter.notifyDataSetChanged() at point where there is change in the adapter data. If it doesnt work, try reinstantiating the adapter on and on, and then calling notifyDataSetChanged().

    In the try remove the type definition of the adapter, you already did public SimpleCursorAdapter ladapter; just feed it with data in the try.

    0 讨论(0)
  • 2020-12-18 14:59
        import android.app.ListActivity;
        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.SimpleCursorAdapter;
        import android.widget.Toast;
    
        public class pills extends ListActivity {
    
        public static final String C_MED_NAME = "med_name";
        public SimpleCursorAdapter ladapter;
        Context context;
        Cursor cur;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pills);
            context=this;
            try {
    
                    DatabaseHelper DBHelper= new DatabaseHelper(this);
                    SQLiteDatabase db = DBHelper.getReadableDatabase();
    
    
                    cur = db.query("Medicines", null, null, null, null, null, null);
                    startManagingCursor(cur);
    
                    String [] columns = new String[] {C_MED_NAME};
                    int [] to = new int[] {R.id.meditem};
    
                    ladapter = new SimpleCursorAdapter(this, R.layout.meditem, cur, columns, to);
    
                    this.setListAdapter(ladapter);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
    
            Button saveMed = (Button) findViewById(R.id.pills_newmedsubmit);
            saveMed.setOnClickListener(new OnClickListener() {
    
    
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    final EditText newMed = (EditText) findViewById(R.id.pills_newmedtxtbox);
    
                    if (newMed.getText().toString().equals("")) {
    
        //                    Context context = getApplicationContext();
                            CharSequence text = "Please type the name of a new prescription item.";
                            int duration = Toast.LENGTH_SHORT;
    
                            Toast toast= Toast.makeText(context, text, duration);
                            toast.show();
                    }
                    else
                    {
    
                            String medName = newMed.getText().toString();
    
                            insertNewPrescriptionItem(medName);
    
                            EditText clearTextBox = (EditText) findViewById(R.id.pills_newmedtxtbox);
                            clearTextBox.setText("");
    
        //                  Context context = getApplicationContext();
                            CharSequence text = "Saved";
                            int duration = Toast.LENGTH_SHORT;
    
                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show();
    
    
                    }
    
                refresh();
    
                }
    
    
            });}
    
            private void insertNewPrescriptionItem (String medName){
    
                DatabaseHelper DbHelper = new DatabaseHelper(this);
    
                SQLiteDatabase db = DbHelper.getWritableDatabase();
    
                ContentValues cv = new ContentValues();
    
                    cv.put(DatabaseHelper.C_MED_NAME, medName);
    
                    db.insert(DatabaseHelper.TABLE_NAME, DatabaseHelper.C_MED_NAME, cv);
                    db.close();
                    }
    
    
        }
    public void refresh(){
    cur = db.query("Medicines", null, null, null, null, null, null);
    ladapter.notifyDataSetChanged();
    }
    

    try this, i think your context is null thats why it is happening so

    0 讨论(0)
提交回复
热议问题