【发布时间】:2018-07-07 06:15:04
【问题描述】:
我正在尝试在 SQLite 数据库类“DatabaseHandler”的回收站视图中显示数据
公共类 DatabaseHandler 扩展 SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "NewPersonManager";
// New Person table name
private static final String TABLE_NEW_PERSON = "newPerson";
// New Person Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_ID = "id";
private static final String KEY_FACEBOOK_ID = "facebook_id";
private static final String KEY_TWITTER_ID = "twitter_id";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NEW_PERSON_TABLE = "CREATE TABLE " + TABLE_NEW_PERSON +
"("+KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_NAME + " TEXT, " + KEY_FACEBOOK_ID + " TEXT," + KEY_TWITTER_ID + " TEXT " + ")";
db.execSQL(CREATE_NEW_PERSON_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEW_PERSON);
// Create tables again
onCreate(db);
}
// Adding new contact
public void addNewPerson(New_Person_data newPerson) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, newPerson.getmUserName()); // Person Name
values.put(KEY_FACEBOOK_ID, newPerson.getmFacebookId()); // Person Facebook ID
values.put(KEY_TWITTER_ID, newPerson.getmTwitterId()); //Person Twitter ID
// Inserting Row
db.insert(TABLE_NEW_PERSON, null, values);
db.close(); // Closing database connection
}
// Getting single Person
public New_Person_data getPerson(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NEW_PERSON, new String[] {KEY_ID,KEY_NAME, KEY_FACEBOOK_ID,
KEY_TWITTER_ID }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
New_Person_data newPerson = new New_Person_data
( Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
// return New Person
return newPerson;
}
// Getting All Persons
public List<New_Person_data> getAllPeople() {
List<New_Person_data> PeopleList = new ArrayList<New_Person_data>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NEW_PERSON;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
New_Person_data newPerson = new New_Person_data();
newPerson.setID(Integer.parseInt(cursor.getString(0)));
newPerson.setmUserName(cursor.getString(1));
newPerson.setmFacebookId(cursor.getString(2));
newPerson.setmTwitterId(cursor.getString(2));
// Adding person to list
PeopleList.add(newPerson);
} while (cursor.moveToNext());
}
// return people list
return PeopleList;
}
// Getting PEOPLE Count
public int getPeopleCount() {
String countQuery = "SELECT * FROM " + TABLE_NEW_PERSON;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating single person
public int updatePerson(New_Person_data new_person_data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, new_person_data.getmUserName());
values.put(KEY_FACEBOOK_ID, new_person_data.getmFacebookId());
values.put(KEY_FACEBOOK_ID, new_person_data.getmTwitterId());
// updating row
return db.update(TABLE_NEW_PERSON, values, KEY_ID + " = ?",
new String[] { String.valueOf(new_person_data.getID()) });
}
// Deleting single person
public void deletePerson(New_Person_data newPerson) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NEW_PERSON, KEY_ID + " = ?",
new String[] { String.valueOf(newPerson.getID()) });
db.close();
}
}
并在用户单击 Add_New_Person 片段中的 DONE 图标时保存数据
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId =item.getItemId();
New_Person_data new_person_data;
if(itemId == R.id.new_person_done)
userName = mUserName.getText().toString();
facebookId = mFacebookId.getText().toString();
twitterId = mTwitterId.getText().toString();
// after user click done image , add new person row
new_person_data= new New_Person_data(userName,facebookId,twitterId);
databaseHandler.addNewPerson(new_person_data);
getActivity().finish();
return true;
}
在另一个片段中,我创建了一个方法来获取“People”类的数组,该数组有一个带有一个字符串参数“userName”的构造器,我从 New_Person_Class 获得它...
`private void preparePeopleData() {
DatabaseHandler databaseHandler = new DatabaseHandler(this.getContext());
// List to get all people from database
List<New_Person_data> peopleArrayList = databaseHandler.getAllPeople();
//get people's user name and add to people class to show in recycler view
for (New_Person_data newPerson:peopleArrayList) {
People People = new People(newPerson.getmUserName());
PeopleList.add(People);
}
Log.d("Peple Array size ", String.valueOf(peopleArrayList.size()));
// sort user name from A-Z
Collections.sort(PeopleList, new Comparator<People>() {
@Override
public int compare(People lhs, People rhs) {
return lhs.getPerson().compareTo(rhs.getPerson());
}
});
// set the new changes of data
mAdapter.notifyDataSetChanged();
}`
在运行应用程序的最后,在 Add New Person 活动中插入三个编辑文本并单击完成后,它会显示空白回收站视图。
编辑* 我发现数据只有在再次运行应用程序后才会出现,所以每次我需要显示添加的项目时我都需要再次运行应用程序..为什么它没有立即更新?
【问题讨论】:
-
你确定数据正在被插入到 DB 中。使用 stetho 检查是否插入了数据。检查此以了解如何使用 Stetho github.com/facebook/stetho
-
@suja 我发现数据只有在再次运行应用程序后才会出现,所以每次我需要显示添加的项目时我都需要再次运行应用程序..为什么它没有立即更新?
-
尝试在您的片段/活动中添加 loadercallback。这样您的回收器就会在数据成功插入数据库时进行更新。
标签: android sql database sqlite android-recyclerview