Java ListView没有';t出现在我的应用程序的主活动中(附加图像)

Java ListView没有';t出现在我的应用程序的主活动中(附加图像),java,android,listview,Java,Android,Listview,在我将便笺保存到Android应用程序中后,便笺的便笺(或列表视图)不会出现在main活动中。我的应用程序的main活动类是: package com.twitter.i_droidi.mynotes; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.ActionBarActiv

在我将便笺保存到Android应用程序中后,便笺的便笺(或列表视图)不会出现在main活动中。我的应用程序的main活动类是:

package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

    ListView lv;
    NotesDataSource nDS;
    List<NotesModel> notesList;

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

        nDS = new NotesDataSource(this);
        lv = (ListView) findViewById(R.id.lv);

        nDS.open();
        notesList = nDS.getAllNotes();
        nDS.close();

        String[] notes = new String[notesList.size()];

        for (int i = 0; i < notesList.size(); i++) {
            notes[i] = notesList.get(i).getTitle();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
                android.R.id.text1, notes);
        lv.setAdapter(adapter);

        registerForContextMenu(lv);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent nView = new Intent(this, Second.class);
        nView.putExtra("id", notesList.get(position).getId()); // Check...!!!
        startActivity(nView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_delete, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:
                nDS.open();
                nDS.deleteNote(lv.getId()); // Check...!!!
                nDS.close();
                Toast nDelete = Toast.makeText(this, "Deleted.", Toast.LENGTH_LONG);
                nDelete.show();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mainMenuNewNote:
                Intent nNote = new Intent(this, Second.class);
                startActivity(nNote);
                return true;

            case R.id.mainMenuAbout:
                AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
                aboutDialog.setTitle("About the app");
                aboutDialog.setMessage("The Simplest app for notes!\n\n" +
                        "Developed by: Abdulaziz\n" +
                        "Twitter: @i_Droidi\n" +
                        "Telegram: MrGlitch\n\n" +
                        "Special Thanks to who tested the app before upload it on Play Store and to who use it now! :)");
                aboutDialog.setIcon(R.mipmap.ic_launcher);
                aboutDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface aboutDialog, int witch) {
                        // Do Not Do Anything.
                    }
                });

                aboutDialog.show();
                return true;

            case R.id.mainMenuExit:
                AlertDialog.Builder exDialog = new AlertDialog.Builder(this);
                exDialog.setTitle("Exit?");
                exDialog.setMessage("Are you sure to exit?");
                exDialog.setIcon(R.mipmap.ic_launcher);
                exDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface exDialog, int which) {
                        finish();
                    }
                });
                exDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface exDialog, int which) {
                        // Do Not Do Anything.
                    }
                });

                exDialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>

</LinearLayout>
package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class Second extends ActionBarActivity {

    NotesDataSource nDS;
    EditText noteTitle;
    EditText noteBody;
    int id;
    DB db;

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

        Intent in = getIntent();
        id = in.getIntExtra("id", 0);

        noteTitle = (EditText) findViewById(R.id.note_title);
        noteBody = (EditText) findViewById(R.id.note);
        nDS = new NotesDataSource(this);

        nDS.open();
        NotesModel note = nDS.getNote(id);
        nDS.close();

        noteTitle.setText(note.getTitle());
        noteBody.setText(note.getBody());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.secondMenuSave:
                if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
                    nDS.open();
                    nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
                    nDS.close();
                    Toast nSave = Toast.makeText(this, "Saved.", Toast.LENGTH_LONG);
                    nSave.show();
                    finish();
                } else {
                    Toast notSave = Toast.makeText(this, "The title and content of the note CANNOT be empty!", Toast.LENGTH_LONG);
                    notSave.show();
                }
                return true;

            case R.id.secondMenuBack:
                AlertDialog.Builder baDialog = new AlertDialog.Builder(this);
                baDialog.setTitle("Back?");
                baDialog.setMessage("Do you want to back to the main page before saving the note?");
                baDialog.setIcon(R.mipmap.ic_launcher);
                baDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface baDialog, int which) {
                        finish();
                    }
                });
                baDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface baDialog, int which) {
                        // Do Not Do Anything.
                    }
                });

                baDialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
package com.twitter.i_droidi.mynotes;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class NotesDataSource {

    DB myDB;
    SQLiteDatabase sql;

    String[] getAllColumns = new String[]{DB.ID, DB.TITLE, DB.BODY};

    public NotesDataSource(Context context) {
        myDB = new DB(context);
    }

    public void open() {
        try {
            sql = myDB.getWritableDatabase();
        } catch (Exception ex) {
            Log.d("Error in your database!", ex.getMessage());
        }
    }

    public void close() {
        sql.close();
    }

    public void createNote(String title, String body) {
        ContentValues note = new ContentValues();
        note.put(myDB.TITLE, title);
        note.put(myDB.BODY, body);
        sql.insert(myDB.TABLE_NAME, null, note);
    }

    public NotesModel getNote(int id) {
        NotesModel note = new NotesModel();

        Cursor cursor = sql.rawQuery("SELECT * FROM " + DB.TABLE_NAME + " WHERE " + DB.ID + " = ?", new String[]{id + ""});

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            note.setId(cursor.getInt(0));
            note.setTitle(cursor.getString(1));
            note.setBody(cursor.getString(2));
            cursor.close();
        }
        return note;
    }

    public void updateNote(int id, String title, String body) {
        ContentValues note = new ContentValues();
        note.put(myDB.TITLE, title);
        note.put(myDB.BODY, body);
        sql.update(myDB.TABLE_NAME, note, myDB.ID + " = " + id, null);
    }

    public void deleteNote(Object id) {
        sql.delete(myDB.TABLE_NAME, myDB.ID + " = " + id, null);
    }

    public List<NotesModel> getAllNotes() {
        List<NotesModel> notesList = new ArrayList<NotesModel>();

        Cursor cursor = sql.query(myDB.TABLE_NAME, getAllColumns, null, null, null, null, null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            NotesModel notes = new NotesModel();
            notes.setId(cursor.getInt(0));
            notes.setTitle(cursor.getString(1));
            notes.setBody(cursor.getString(2));

            notesList.add(notes);
            cursor.moveToNext();
        }

        cursor.close();
        return notesList;
    }
}
package com.twitter.i_droidi.mynotes;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB extends SQLiteOpenHelper {

    private static final String DB_NAME = "MyNotes";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "MyNotes";
    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String BODY = "body";

    private static final String DB_CREATE = "create table " + TABLE_NAME + " (" + ID + " integer primary key autoincrement, " +
            TITLE + " text not null, " + BODY + " text not null)";

    public DB(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
我的应用程序的NotesDataSource类是:

package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

    ListView lv;
    NotesDataSource nDS;
    List<NotesModel> notesList;

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

        nDS = new NotesDataSource(this);
        lv = (ListView) findViewById(R.id.lv);

        nDS.open();
        notesList = nDS.getAllNotes();
        nDS.close();

        String[] notes = new String[notesList.size()];

        for (int i = 0; i < notesList.size(); i++) {
            notes[i] = notesList.get(i).getTitle();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
                android.R.id.text1, notes);
        lv.setAdapter(adapter);

        registerForContextMenu(lv);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent nView = new Intent(this, Second.class);
        nView.putExtra("id", notesList.get(position).getId()); // Check...!!!
        startActivity(nView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_delete, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:
                nDS.open();
                nDS.deleteNote(lv.getId()); // Check...!!!
                nDS.close();
                Toast nDelete = Toast.makeText(this, "Deleted.", Toast.LENGTH_LONG);
                nDelete.show();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mainMenuNewNote:
                Intent nNote = new Intent(this, Second.class);
                startActivity(nNote);
                return true;

            case R.id.mainMenuAbout:
                AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
                aboutDialog.setTitle("About the app");
                aboutDialog.setMessage("The Simplest app for notes!\n\n" +
                        "Developed by: Abdulaziz\n" +
                        "Twitter: @i_Droidi\n" +
                        "Telegram: MrGlitch\n\n" +
                        "Special Thanks to who tested the app before upload it on Play Store and to who use it now! :)");
                aboutDialog.setIcon(R.mipmap.ic_launcher);
                aboutDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface aboutDialog, int witch) {
                        // Do Not Do Anything.
                    }
                });

                aboutDialog.show();
                return true;

            case R.id.mainMenuExit:
                AlertDialog.Builder exDialog = new AlertDialog.Builder(this);
                exDialog.setTitle("Exit?");
                exDialog.setMessage("Are you sure to exit?");
                exDialog.setIcon(R.mipmap.ic_launcher);
                exDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface exDialog, int which) {
                        finish();
                    }
                });
                exDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface exDialog, int which) {
                        // Do Not Do Anything.
                    }
                });

                exDialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>

</LinearLayout>
package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class Second extends ActionBarActivity {

    NotesDataSource nDS;
    EditText noteTitle;
    EditText noteBody;
    int id;
    DB db;

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

        Intent in = getIntent();
        id = in.getIntExtra("id", 0);

        noteTitle = (EditText) findViewById(R.id.note_title);
        noteBody = (EditText) findViewById(R.id.note);
        nDS = new NotesDataSource(this);

        nDS.open();
        NotesModel note = nDS.getNote(id);
        nDS.close();

        noteTitle.setText(note.getTitle());
        noteBody.setText(note.getBody());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.secondMenuSave:
                if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
                    nDS.open();
                    nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
                    nDS.close();
                    Toast nSave = Toast.makeText(this, "Saved.", Toast.LENGTH_LONG);
                    nSave.show();
                    finish();
                } else {
                    Toast notSave = Toast.makeText(this, "The title and content of the note CANNOT be empty!", Toast.LENGTH_LONG);
                    notSave.show();
                }
                return true;

            case R.id.secondMenuBack:
                AlertDialog.Builder baDialog = new AlertDialog.Builder(this);
                baDialog.setTitle("Back?");
                baDialog.setMessage("Do you want to back to the main page before saving the note?");
                baDialog.setIcon(R.mipmap.ic_launcher);
                baDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface baDialog, int which) {
                        finish();
                    }
                });
                baDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface baDialog, int which) {
                        // Do Not Do Anything.
                    }
                });

                baDialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
package com.twitter.i_droidi.mynotes;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class NotesDataSource {

    DB myDB;
    SQLiteDatabase sql;

    String[] getAllColumns = new String[]{DB.ID, DB.TITLE, DB.BODY};

    public NotesDataSource(Context context) {
        myDB = new DB(context);
    }

    public void open() {
        try {
            sql = myDB.getWritableDatabase();
        } catch (Exception ex) {
            Log.d("Error in your database!", ex.getMessage());
        }
    }

    public void close() {
        sql.close();
    }

    public void createNote(String title, String body) {
        ContentValues note = new ContentValues();
        note.put(myDB.TITLE, title);
        note.put(myDB.BODY, body);
        sql.insert(myDB.TABLE_NAME, null, note);
    }

    public NotesModel getNote(int id) {
        NotesModel note = new NotesModel();

        Cursor cursor = sql.rawQuery("SELECT * FROM " + DB.TABLE_NAME + " WHERE " + DB.ID + " = ?", new String[]{id + ""});

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            note.setId(cursor.getInt(0));
            note.setTitle(cursor.getString(1));
            note.setBody(cursor.getString(2));
            cursor.close();
        }
        return note;
    }

    public void updateNote(int id, String title, String body) {
        ContentValues note = new ContentValues();
        note.put(myDB.TITLE, title);
        note.put(myDB.BODY, body);
        sql.update(myDB.TABLE_NAME, note, myDB.ID + " = " + id, null);
    }

    public void deleteNote(Object id) {
        sql.delete(myDB.TABLE_NAME, myDB.ID + " = " + id, null);
    }

    public List<NotesModel> getAllNotes() {
        List<NotesModel> notesList = new ArrayList<NotesModel>();

        Cursor cursor = sql.query(myDB.TABLE_NAME, getAllColumns, null, null, null, null, null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            NotesModel notes = new NotesModel();
            notes.setId(cursor.getInt(0));
            notes.setTitle(cursor.getString(1));
            notes.setBody(cursor.getString(2));

            notesList.add(notes);
            cursor.moveToNext();
        }

        cursor.close();
        return notesList;
    }
}
package com.twitter.i_droidi.mynotes;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB extends SQLiteOpenHelper {

    private static final String DB_NAME = "MyNotes";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "MyNotes";
    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String BODY = "body";

    private static final String DB_CREATE = "create table " + TABLE_NAME + " (" + ID + " integer primary key autoincrement, " +
            TITLE + " text not null, " + BODY + " text not null)";

    public DB(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
我的应用程序(MainActivity)的屏幕截图,保存便条后(没有显示!):

我怎么能解决这个问题

或者任何人都能写出正确的代码


谢谢您的帮助。

问题是onCreate在生命周期中只调用一次。当您切换活动时,主活动将被隐藏并暂停。返回活动后,onCreate不会再次启动

重写onStart方法并在其中设置列表适配器。它将在onCreate()之后以及返回视图时立即调用。以下是Android的生命周期:


此答案假设
第二个
活动的实现为某个全局模型添加了一个新注释,并且此模型的状态反映在您在
主活动
中创建的
nDS
对象中(因为
onCreate()
MainActivity
通常在您
finish()时不会被调用

你一定要测试一下上述假设是否正确。如果不是,您可以使用某种形式的设计模式来存储数据

为了将更改反映到
列表视图中
main活动中修改/添加以下代码

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

ArrayAdapter<String> adapter;
NotesDataSource nDS;
List < NotesModel > notesList;

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

    nDS = new NotesDataSource(this);
    ListView lv = (ListView) findViewById(R.id.lv);

    nDS.open();
    notesList = nDS.getAllNotes();
    nDS.close();

    String[] notes = new String[notesList.size()];

    for (int i = 0; i < notesList.size(); i++) {
        notes[i] = notesList.get(i).getTitle();
    }

    adapter = new ArrayAdapter < String > (MainActivity.this, android.R.layout.simple_list_item_1,
    android.R.id.text1, notes);
    lv.setAdapter(adapter);

    registerForContextMenu(lv);
    lv.setOnItemClickListener(this);
}



@Override
protected void onResume() {
    super.onResume();

    nDS.open();
    notesList = nDS.getAllNotes();
    nDS.close();

    String[] notes = new String[notesList.size()];

    for (int i = 0; i < notesList.size(); i++) {
        notes[i] = notesList.get(i).getTitle();
    }

    adapter.clear();
    adapter.addAll(notes);
    adapter.notifyDataSetChanged();
}

// No changes in other methods ...

您可以找到编写自定义适配器的良好教程。

我认为您的代码中存在两个问题

1) 第一次替换

public List<NotesModel> getAllNotes() {
        List<NotesModel> notesList = new ArrayList<NotesModel>();

        Cursor cursor = sql.query(myDB.TABLE_NAME, getAllColumns, null, null, null, null, null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            NotesModel notes = new NotesModel();
            notes.setId(cursor.getInt(0));
            notes.setTitle(cursor.getString(1));
            notes.setBody(cursor.getString(2));

            notesList.add(notes);
            cursor.moveToNext();
        }

        cursor.close();
        return notesList;
    }
添加注释后,返回MainActivity,如下所示----



不要使用finish,因为它只会结束当前任务,并在堆栈中删除上一个屏幕而不刷新

我不确定是否完全理解正在发生的事情,但我认为在添加注释后,您需要在listview适配器上调用
notifyDataSetChanged()
。好的,让我看看是否理解。在笔记列表中键入/添加笔记。然后,当您完成并将其“保存”后,返回到主屏幕时,便笺不在那里。对吗?它是否在您的注释数组中(
notes[i]=notesList.get(i.getTitle();
)?你看到了吗?如果是这样的话,那么我认为仍然可以简单地使用新的数据集(notes[])更新listview适配器。将其保存到列表后,在保存listview的适配器上调用
notifyDataSetChanged()
。是否检查notes数组是否确实包含数据?添加此行后出现相同问题
adapter.notifyDataSetChanged()发布
NotesDataSource的代码
同样的问题!可能我在使用
onStart
方法时出错了。你能修改我的代码并在里面写下那个方法吗?!这个答案绝对不正确-将
适配器
分配到
onStart()
中的列表是错误的。那么,我可以使用
onResume()
方法并在其中添加适配器吗?!谢谢你帮助我!还是一样的问题!我将用第二个类更新帖子。并且,请查看所有类中的(检查…!!!)注释,也许您会发现一个与我们的主要问题相关的错误。@MrGlitch,我假设
DB
class子类
SQLiteOpenHelper
,但我没有看到任何向SQLite DB添加新注释的代码。。。你在什么地方调用了
NotesDataSource.createNote()
吗?我用
DB
类更新了帖子,请检查一下。@MrGlitch,我没有看到对
NotesDataSource.createNote()方法的调用。您检查过您的SQLite数据库是否包含任何信息吗?我添加了这一行
nDS.createNote(notetTitle.getText().toString(),noteBody.getText().toString())在第二个类内
case R.id.secondMenuSave:
使用
nDS.open()打开数据库后但还是一样的问题!使用
nDS.close()关闭数据库可能有问题?!请检查一下。谢谢,谢谢,伙计!问题解决了!还有一个小问题,如果你能解决的话。。我怎样才能从数据库和列表中删除注释?!请参阅
onContextItemSelected
方法中的MainActivity类。虽然OP确认此答案解决了他的问题,但我不会向任何人推荐此方法。首先,使用
rawQuery()
而不是
query()
是不合理的-功能是相同的,但它是
query()
方法,这是“安卓方式”的做法。这是一个编码风格标准的问题……这个答案的第二个问题要严重得多。此语句完全错误:
不要使用finish,因为它将仅结束当前任务,并在堆栈中删除上一个屏幕而不刷新
。当上一个
活动
从堆栈中获取时,将调用
onStart()
onResume()
方法。您可以而且应该在这些方法中“刷新”活动的外观,而不是使用
标记活动\u清除\u顶部
@Vasily首先,在没有正确检查它们的情况下,不要低估任何答案。这些问题可能有100种解决方案。并非每个编码人员都知道所有的解决方案。我可以给他们一个复杂的后向导航解决方案,但他们无法正确理解。所以,如果别人不理解他们,帮助他们又有什么意义呢。所以我采取了一个简单的解决办法。@amit你能帮我删除便条的代码吗?!