Recycler View显示为空白,不显示SQLite数据

时间:2018-01-28 12:12:25

标签: android sql database sqlite android-recyclerview

我试图在SQLite数据库类“DatabaseHandler”

的回收站视图中显示数据

公共类DatabaseHandler扩展了SQLiteOpenHelper {

// 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();
}

}

 @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;
}

}

并在用户点击Add_New_Person片段

中的DONE图标时保存数据
    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();

}`

在另一个片段中,我创建了一个方法来获取一个“People”类的数组,该类具有一个带有一个String参数的constractur“userName,我从New_Person_Class获取它......

`private void preparePeopleData(){

<div ng-app="myApp">
  <div ng-controller="myCntr">

    <label>Items</label><br>
    <label>{{propertyvalue}}</label>
    <md-input-container>

      <input ng-model="property" ng-hide="ptext">
    </md-input-container>
    <i class="material-icons" ng-click="hidepr()" ng-hide="hideP">&#xE22B;</i>
    <i class="material-icons" ng-hide="hidePropertysave" ng-click="save()">&#xE161;</i>
    <br>


  </div>
</div>

在运行应用程序并插入日期后,在“添加新人”活动中单击三个编辑文本,然后单击“完成”,显示空白回收者视图。

编辑*  我发现数据确实只在再次运行应用程序后出现,因此每次我需要显示添加的项目时我需要再次运行应用程序..为什么它不会立即更新?

1 个答案:

答案 0 :(得分:0)

Override onStart方法解决了问题,并调用了为回收站视图提供数据的方法。

  @Override
public void onStart() {
    super.onStart();
    preparePeopleData();
}