android - 按下后退时,Sqlite Listview 会清除

标签 android sqlite listview

这是 logcat。这里似乎有什么错误?

03-07 03:11:55.841: D/dalvikvm(292): GC_EXTERNAL_ALLOC freed 4085 objects / 243720 bytes in 190ms
03-07 03:11:56.221: D/dalvikvm(292): GC_EXTERNAL_ALLOC freed 303 objects / 16040 bytes in 85ms
03-07 03:12:06.990: W/KeyCharacterMap(292): No keyboard for id 0
03-07 03:12:06.990: W/KeyCharacterMap(292): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-07 03:12:07.190: E/Cursor(292): Invalid statement in fillWindow()

从我的 sqliteadapter 获取数据的第一个 Activity

  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.search);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


  mySQLiteAdapter = new SQLiteAdapter(this);
  mySQLiteAdapter.openToRead();

  Cursor cursor = mySQLiteAdapter.queueAll();
  startManagingCursor(cursor);

  String[] from = new String[]{SQLiteAdapter.KEY_FOODNAME,SQLiteAdapter.KEY_CALORIES};
  int[] to = new int[]{R.id.tv1, R.id.tv2};

  SimpleCursorAdapter cursorAdapter =
   new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

  listContent1.setAdapter(cursorAdapter);
  listContent1.setOnItemClickListener(listContentOnItemClickListener); 
  mySQLiteAdapter.close();
   private ListView.OnItemClickListener listContentOnItemClickListener = new ListView.OnItemClickListener(){
 public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

  Cursor cursor = (Cursor) parent.getItemAtPosition(position);

  String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME));
  String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CALORIES));
  arlene = arlene + Integer.parseInt(item_content2);
  String item = String.valueOf(" Food Name: " + item_content1 ) + "\n" +
          " Calories: " +  item_content2;

   Toast.makeText(Search.this, item, Toast.LENGTH_LONG).show();      

   food.setText(item_content1);
   calories.setText(String.valueOf(arlene));


   Intent myIntent = new Intent(Search.this, Foodlog.class);
   Bundle bundle = new Bundle();
   bundle.putString("SQLITEDATA1", food.getText().toString()); 
   bundle.putString("SQLITEDATA2", calories.getText().toString()); 
   myIntent.putExtras(bundle);
   startActivity(myIntent); 



   SavePreferences("calories", calories.getText().toString());
   LoadPreferences();

获取数据的第二个 Activity

   Bundle bundle = getIntent().getExtras();  
   if( bundle != null){
   String food = bundle.getString("SQLITEDATA1");
   String calories = bundle.getString("SQLITEDATA2");

   String[] values = new String[] { food, calories }; //That you got from your intent bundle
   LVAdapter adapter = new LVAdapter(this, R.layout.foodlist, values);
   setListAdapter(adapter);

当我按回去时,它会清除我第一个 Activity 的所有 ListView 数据

最佳答案

在onCreate中移除

mySQLiteAdapter.close();  

将其移至onDestroy

@override
protected void onDestroy()
{
    mySQLiteAdapter.close();  
}

关于android - 按下后退时,Sqlite Listview 会清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256152/

相关文章:

.net - SQLite 等效于 ISNULL()、NVL()、IFNULL() 或 COALESCE()

android - 将固定的、透明的标题附加到 ListView?

listview - flutter 。在非常大的 ListView 上设置 ListView 初始位置

java - 当我返回到上一个 Activity 时,内容没有出现

android - JetpackCompose Modifier 属性的 aspectRatio(1f) 和 fillMaxSize() 有什么区别

android - Android中有什么简单、通用的方法来获取布局的 Root View ?

android - TextInputLayout 和 TextInputEditText 的区别

c# - connection.Close() 后对 SQLite 的更改丢失

android - 在 ListView 中保留一个 EditText 值

c# - SQLite 的 DLL hell