如何修复Android中的ListView鬼项?

时间:2019-02-01 09:24:39

标签: android firebase android-listview

我正在从firebase中获取数据,并且一切正常,但是在列表视图中,我得到了幽灵条目(项),如下所示。

https://i.imgur.com/9sVk20x.jpg “这是设备的屏幕截图”

这是我获取和显示列表视图项的代码

    public void displayNearTopics(GeoQuery locGeoQuery) {

    //Log.d(TAG, "SharedPref, new topics: "+prefs.getInt(NO_NEW_TOPICS,0));

    locTopics.clear();
    loctopicId.clear();
    locTopicsListAdapter = new TopicsListAdapter(getActivity(), locTopics, loctopicId);
    mTopicsRecyclerView.setAdapter(locTopicsListAdapter);
    //mTopicsRecyclerView.setHasFixedSize(true);
    mTopicsRecyclerView.showShimmerAdapter();
    mTopicsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    locGeoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(final String key, GeoLocation location) {
            //key is the topic key
            //Toast.makeText(getActivity(), "The retrieved keys are:" + key, Toast.LENGTH_SHORT).show();
            mTopicsRecyclerView.hideShimmerAdapter();
            Log.d(TAG, "Key entered:"+ key);
            if(bottomNavigation.getCurrentItem()!=2){
                int currentCount = prefs.getInt(NO_NEW_TOPICS, 0)+1;

                Log.d(TAG, "ONKEYENTERED, SharedPref, new topics, current count: "+currentCount);

                SharedPreferences.Editor editor = prefs.edit();
                editor.putInt(NO_NEW_TOPICS, currentCount);
                editor.apply();
                Log.d(TAG, "ONKEYENTERED, SharedPref, new topics, shared ref: "+prefs.getInt(NO_NEW_TOPICS, 0));
                //bottomNavigation.setNotification(String.valueOf(currentCount),2);
                //bottomNavigation.refresh();

            }
            else{
                SharedPreferences.Editor editor = prefs.edit();
                editor.putInt(NO_NEW_TOPICS, 0);
                editor.apply();
            }
            DatabaseReference topicRef = FirebaseDatabase.getInstance().getReference()
                    .child(FirebaseValues.TOPICS_TABLE).child(key);
            topicRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Topics tempTopic;
                    tempTopic = dataSnapshot.getValue(Topics.class);


                    locTopics.add(0, tempTopic);
                    loctopicId.add(0, key);
                    locTopicsListAdapter.notifyItemInserted(locTopics.size() - 1);
                    swipeRefTopic.setRefreshing(false);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

        @Override
        public void onKeyExited(String key) {

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {

        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });


}

我正在使用Firebase geofire来获取具有给定半径的密钥。一切都只是我在我的列表视图得到一些未知的物品可以正常使用。同样,一旦我向下滚动然后向上滚动,它们就会消失。

1 个答案:

答案 0 :(得分:0)

您似乎在通知错误的位置。

def is_alcoholic
  if abv == 0.0
    self.alcoholic = false
  else
    self.alcoholic = true
  end
end

您应将// Inserting item at index 0. locTopics.add(0, tempTopic); // Updating position at the end of the list. locTopicsListAdapter.notifyItemInserted(locTopics.size() - 1); 更改为通过notifyItemInserted

0