我有一个listview,它从SQLite数据库中获取信息并显示它。有一个EditText和一个addButton。当按下addButton时,EditText中的文本将被放入数据库,之后应该更新listview。添加到数据库的工作原理是,在暂停活动并再次恢复活动之前,列表视图不会更新。
在此布局中还有一个eraseButton,所有带有选中复选框的文本视图都应从数据库中删除并从列表视图中删除。在这个问题中,该按钮不会删除所选的文本视图(可能是由于数据库中的一些rowid问题。我相信它不会在删除后更新列表视图,因为addButton的方法相同。
感谢任何帮助。
这是MainActivity
package net.ddns.introzen.jagharaldrig;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Main3Activity extends AppCompatActivity {
//ArrayList<String> questions = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//SQLite Database
final DatabaseHandler db = new DatabaseHandler(this);
//listview
//add items
/*questions.add("Jag har aldrig");
questions.add("Jag har aldrig varit");
questions.add("Jag har aldrig2");
//Checkboxadapters
final ListView questionList = (ListView) findViewById(R.id.questionList);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, questions);
if(questionList != null) {
questionList.setAdapter(adapter);
questionList.setItemsCanFocus(false);
questionList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}*/
final ListView questionList = (ListView) findViewById(R.id.questionList);
final Cursor cursor = db.getData();
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, new String[] { DatabaseHandler.COL_QUESTION }, new int[] { R.id.checkedTextView1 },0);
assert questionList != null;
questionList.setAdapter(adapter);
questionList.setItemsCanFocus(false);
questionList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//Erasebutton listener
final Button eraseButton = (Button) findViewById(R.id.eraseButton);
assert eraseButton != null;
eraseButton.setOnClickListener(new View.OnClickListener() { //erasebutton onclick
public void onClick(View eraseButton) {
SparseBooleanArray checked = questionList.getCheckedItemPositions();
for(int i = questionList.getCount() - 1; i >= 0; i--)
{
if(checked.get(i)) {
//What to do with selected listitems
db.removeData(i);
}
}
checked.clear();
Cursor newCursor = db.getData();
adapter.swapCursor(newCursor);
}
});
//addbutton listener
final Button addButton = (Button) findViewById(R.id.addButton);
assert addButton != null;
addButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
public void onClick(View addButton) {
EditText addText = (EditText) findViewById(R.id.addText);
assert addText != null;
String text = addText.getText().toString();
db.insertData(text);
Cursor newCursor = db.getData();
adapter.swapCursor(newCursor);
}
});
//Nextbutton listener
final Button returnButton = (Button) findViewById(R.id.returnButton);
assert returnButton != null;
returnButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
public void onClick(View returnButton) {
Intent mainMenu = new Intent(Main3Activity.this, Main2Activity.class);
Main3Activity.this.startActivity(mainMenu);
}
});
}
}
这是DatabaseHandler
package net.ddns.introzen.jagharaldrig;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "questions.db";
public static final String TABLE_NAME = "questions";
public static final String COL_ID = "ID";
public static final String COL_QUESTION = "question";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_QUESTION + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String question) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_QUESTION, question);
db.insert(TABLE_NAME, null, cv);
return true;
}
public boolean removeData(int position) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COL_ID+"="+position, null);
return true;
}
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT "+COL_ID+" AS _id, "+COL_QUESTION+" FROM " + TABLE_NAME, null);
return res;
}
}
答案 0 :(得分:0)
您可以尝试将项目添加到适配器,然后调用适配器。在它之后的notifyDataSetChanged。
它看起来像这样
db.insertData(text)
adapter.add()//the item;
adapter.notifyDataSetChanged();
或在swapCursor
之后放置notify方法答案 1 :(得分:0)
更新数据库后添加新游标解决了问题。感谢user3431672的回答。
adapter.swapCursor(newCursor);