[英]OutOfMemory Error when transferring data from text file to MySQL table in JAVA
[英]OutOfMemory Error when displaying data from SQLiteDatabase
我正在嘗試使用稱為displayData()的方法顯示存儲在SQLite數據庫中的數據。 按鈕上有一個onClickListener,只要在該onClickListener內部調用該方法,它就會完美執行。 盡管我希望每當應用啟動時就顯示數據。 當我直接在onCreate或onStart方法中調用同一方法時,我在重新啟動應用程序時遇到OutOfMemory錯誤。 每當應用啟動時,如何在沒有此錯誤的情況下如何調用該方法?
主要活動
DatabaseHelper dbHelper;
ListView listView;
EditText taskInput;
Button addButton;
ArrayList<String> taskList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskList = new ArrayList<String>();
listView = (ListView) findViewById(R.id.listView);
taskInput = (EditText) findViewById(R.id.taskInput);
addButton = (Button) findViewById(R.id.addButton);
dbHelper = new DatabaseHelper(this);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//put data to Database
String getInput = taskInput.getText().toString();
if (taskInput.length() != 0) {
putData(getInput);
taskInput.setText("");
displayData();
} else {
Toast.makeText(MainActivity.this, "You must put something!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void displayData(){
Cursor data = dbHelper.fetchData();
ToDoAdapter cursorAdapter = new ToDoAdapter(this, data);
if(data.getCount() != 0){
taskList.clear();
while(data.moveToNext()){
taskList.add(data.getString(1));
listView.setAdapter(cursorAdapter);
}
}
else{
Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
}
}
public void putData(String newEntry){
boolean insertData = dbHelper.addData(newEntry);
if(insertData == true){
Toast.makeText(this, "Successfully Added Task", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "No Data Added", Toast.LENGTH_SHORT).show();
}
}
您需要在while循環的每個迭代中設置適配器,您需要在每次迭代中將數據添加到列表中,並在完成時將其設置為while循環之外的適配器。 檢查以下代碼
while(data.moveToNext()){
taskList.add(data.getString(1));
}
listView.setAdapter(cursorAdapter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.