首次打开应用程序时未显示数据库

时间:2016-06-30 17:11:08

标签: android sqlite

我制作了一个可以保存笔记的android应用程序。笔记确实得到了保存。但是,当应用程序打开时,仅显示示例注释<f:viewParam>。按Akhilesh Chobey(编辑注释活动)

中的后退按钮会显示所有其他注释

MainActivity:

Main2Activity

Main2Activity:

public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;

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

    notesListView = (ListView) findViewById(R.id.notesListView);
    notesArrayList = new ArrayList<String>();

    if(Main2Activity.myDb != null) {
        notesArrayList.clear();
        Cursor res = Main2Activity.myDb.getData();
        if (res.getCount() == 0) {
            Log.i("Error", "error");
            return;
        }
        while (res.moveToNext()) {
            notesArrayList.add(res.getString(res.getColumnIndex("text")));
        }
    }

    notesArrayList.add("Akhilesh Chobey");
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
    notesListView.setAdapter(adapter);

    notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
            intent.putExtra("notePosition", i);
            startActivity(intent);
        }
    });
}
}

数据库助手类:

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    editNote = (EditText) findViewById(R.id.noteEditText);

    myDb = new DatabaseOperations(Main2Activity.this);

    Intent intent = getIntent();
    position = intent.getIntExtra("notePosition", -1);
    if(position != -1){

        editNote.setText(MainActivity.notesArrayList.get(position));

    }
    editNote.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    if (myDb != null) {
        boolean isInserted = myDb.insertData(editNote.getText().toString());        


        MainActivity.notesArrayList.set(position, String.valueOf(charSequence));
        MainActivity.adapter.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:1)

在您的Main2Activity类(可怕的命名BTW)中

static DatabaseOperations myDb;

变量在程序开头没有赋值,所以DatabaseOperations== null,所以if条件

if(Main2Activity.myDb != null) {
    notesArrayList.clear();
    Cursor res = Main2Activity.myDb.getData();
    if (res.getCount() == 0) {
        Log.i("Error", "error");
        return;
    }
    while (res.moveToNext()) {
        notesArrayList.add(res.getString(res.getColumnIndex("text")));
    }
}

将不会被执行,因此不会将数据加载到应用程序中。 当你从M2A班回来时, onCreate再次被调用,但这次是DatabaseOperations myDb != NULL,因为myDb被赋予了一个值,

myDb = new DatabaseOperations(Main2Activity.this);  所以MainActivity类中的if条件变为true。

您需要做什么:找到一种方法,使myDb变量在程序开始时不为空。