并行Android中的两个页面的数据未更新

时间:2017-08-08 10:04:31

标签: android multithreading android-recyclerview realm

在我的应用程序中,我有两个页面,一个页面显示个人信息,另一个页面显示新闻信息。我正在使用Realm数据库进行本地存储。我已经使用了一些人和新闻的模拟数据,这些数据将出现在应用程序的开头。现在的问题是,当我运行我的应用程序时,首先我点击人员按钮,它显示人员信息,但如果我点击新闻按钮,它是完全空白的。窗户上没有显示任何内容。在卸载应用程序后,如果我首先单击新闻按钮,则会显示新闻信息,但在单击人员按钮后,人员页面完全空白。两个页面的问题不会同时更新。我不知道在这种情况下会出现什么问题。

我正在简短地提供我的代码

PersonPage活动

public class PersonPage extends AppCompatActivity implements PersonAdapter.PersonListListener{

private RecyclerView recyclerView;
private PersonAdapter adapter;
private Realm personRealm;
private RealmResults<PersonModel> personResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mycolleagues_layout);

    personRealm = Realm.getDefaultInstance();
    recyclerView = (RecyclerView) findViewById(R.id.person_recycler);

    setUpRecycler();

    if (!Prefs.with(this).getPreLoad()) {
        setRealmData();
    }
    showAllPersons();

}

private void showAllPersons() {
    personResult = personRealm.where(PersonModel.class).findAll();
    setAdapter(personResult);
    adapter.notifyDataSetChanged();
}

private void setAdapter(RealmResults<PersonModel> results) {

    adapter = new PersonAdapter(this, personRealm.where(PersonModel.class).findAllSortedAsync("id"),this);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
private void setUpRecycler() {

    recyclerView.setHasFixedSize(true);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
}

private void setRealmData(){

    List<PersonModel> colleague = new ArrayList<>();
    PersonModel model = new PersonModel();
    model.setId(1+System.currentTimeMillis());
    model.setName("Name1");
    model.setCompany("Comapny1");
    model.setTitle("Title1");
    person.add(model);
    model = new PersonModel();
    model.setId(2+System.currentTimeMillis());
    model.setName("Name2");
    model.setCompany("Comapny2");
    model.setTitle("Title1");
    person.add(model);

    for (PersonModel realmModel : person) {
            // Persist the colleague data
        colleagueRealm.beginTransaction();
        colleagueRealm.copyToRealm(realmModel);
        colleagueRealm.commitTransaction();
    }

    Prefs.with(this).setPreLoad(true);
}

@Override
protected void onDestroy() {
    if (personRealm!= null)
        personRealm.close();
    super.onDestroy();
   }
}

我的NewsPage活动

  public class NewsPage extends AppCompatActivity implements NewsAdapter.NewsChangeListener {

    private RecyclerView recyclerView;
    private NewsAdapter adapter;
    private Realm newsRealm;
    private RealmResults<NewsModel> newsResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_page_layout);

        newsRealm = Realm.getDefaultInstance();
        recyclerView = (RecyclerView) findViewById(R.id.news_recycler);

        setUpRecycler();

        if (!Prefs.with(this).getPreLoad()) {
            setRealmData();
        }
        showAllNews();


    }
    private void showAllNews() {
        newsResult = newsRealm.where(NewsModel.class).findAll();
        setAdapter(newsResult);
        adapter.notifyDataSetChanged();
    }

    private void setAdapter(RealmResults<NewsModel> results) {
        adapter = new NewsAdapter(this, newsRealm.where(NewsModel.class).findAllSortedAsync("id"),this);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void setUpRecycler() {

        recyclerView.setHasFixedSize(true);   
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void setRealmData() {

        List<NewsModel> newsItem = new ArrayList<>();

        NewsModel model = new NewsModel();
        model.setId(1+System.currentTimeMillis());
        model.setImage(R.drawable.image1);
        model.setTitle("Title1");
        model.setDate("01.02.2017");
        model.setDetail("Deatils1");
        newsItem.add(model);

        model = new NewsModel();
        model.setId(2+System.currentTimeMillis());
        model.setImage(R.drawable.image2);
        model.setTitle("Title2");
        model.setDate("24.05.2017");
        model.setDetail("Deatils2");
        newsItem.add(model);


        for (NewsModel realmModel : newsItem) {
            // Persist the news data
            newsRealm.beginTransaction();
            newsRealm.insertOrUpdate(realmModel);
            newsRealm.commitTransaction();
        }

        Prefs.with(this).setPreLoad(true);
    }


    @Override
    protected void onDestroy() {
        if (newsRealm!= null)
            newsRealm.close();
        super.onDestroy();
     }
   }

2 个答案:

答案 0 :(得分:0)

这里的问题似乎是这句话。

if (!Prefs.with(this).getPreLoad()) {
        setRealmData();
    }

每次加载Persons列表时,您都要将getPreLoad设置为true;

Prefs.with(this).setPreLoad(true);

当您加载News列表时,它不会从数据库加载模型,因为它总是将此语句Prefs.with(this).getPreLoad()作为false

修改

您可以为PersonNews使用不同的标识符。在Prefs类中,为两者创建一个单独的标识符,并将它们设置在各自的类中:

Prefs课程中添加以下方法:

public static void setPreLoadPerson(boolean shouldLoad) {

    sharedPreferences.edit().putBoolean("loadperson", shouldLoad);
}

public static void setPreLoadNews(boolean shouldLoad) {

    sharedPreferences.edit().putBoolean("loadNews", shouldLoad);
}

public static boolean getPreLoadPerson() {
    return sharedPreferences.getBoolean("loadperson", false);
}
public static boolean getPreLoadNews() {
    return sharedPreferences.getBoolean("loadNews", false);
}

然后根据您的需要,在您的各个班级getset

PS:我在没有任何编译器的情况下编写了这段代码,所以你可能会得到一些错字。

答案 1 :(得分:0)

if (!Prefs.with(this).getPreLoad()) {
    setRealmData();
}

应使用RealmConfiguration.Builder的{​​{1}}方法替换此代码。

initialData(Realm.Transaction)