房间数据库中没有显示在RecycleView中的项目 [英] Items not showing up in RecycleView from Room database

查看:65
本文介绍了房间数据库中没有显示在RecycleView中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在先前的调试之后,我断言我的数据库确实可以正常工作,尽管即使正确设置了ReycleViewAdapter,但它仍然出现,但数据仍未显示.

After previous debugging I've asserted that my database is indeed working correctly however even though it appears although I've set up the ReycleViewAdapter correctly the data is not displaying.

我在主要活动中连接到数据库,然后使用接口将信息传递给片段.然后将此List<GlobalLists>数据作为参数传递给我的适配器,以供onBind方法使用;以前,我是在列表视图中显示此数据的,但是当我想要实时更新时,我不得不切换到回收视图.有人可以指出这里发生了什么吗?

I connect to my database in my main activity and pass the information to my fragment using an interface. This List<GlobalLists> data is then passed as a parameter to my Adapter for use in the onBind method; previously I was presenting this data in a list view but when I wanted live updates I had to switch to a recycle view. Can someone point out what's happening here?

DataCommunication.java对象是我已省略的接口,因为它仅包含用于数据检索的方法,

The DataCommunication.java object is the interface which I've omitted because it just contains the methods for data retrieval,

我的XML:

show_all_lists.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/allItems"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customRow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/rowImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_info_outline"/>

    <TextView
        android:id="@+id/rowTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

RecycleViewAdapter.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;    
import java.util.List;

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {

    private LayoutInflater inflater = null;
    private List<GlobalLists> results = null;

    public RecycleViewAdapter(Context context, List<GlobalLists> results){
        this.inflater = LayoutInflater.from(context);
        this.results = results;
    }    
    @NonNull
    @Override
    public RecycleViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_row, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleViewAdapter.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        GlobalLists item = this.results.get(position);
        holder.rowTitle.setText(item.getTitle());


    }    
    @Override
    public int getItemCount() {
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleViewAdapter.ViewHolder holder, int position) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView rowTitle = null;
        private ImageView rowImage = null;

        public ViewHolder(View itemView) {
            super(itemView);
            this.rowTitle = itemView.findViewById(R.id.rowTitle);
            this.rowImage = itemView.findViewById(R.id.rowImage);

        }
    }
}

ShowAllListsFragment.java

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


public class showAllLists_fragment extends android.support.v4.app.Fragment implements Observer{

    private static final String TAG = "showAllLists";           
    private RecyclerView mAllItemsView = null;
    private RecycleViewAdapter mRecycleAdapter = null;
    private DataCommunication mData = null;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        View view = inflater.inflate(R.layout.show_all_lists_frag, container, false);


        this.mAllItemsView = (RecyclerView) view.findViewById(R.id.allItems);
        this.mRecycleAdapter = new RecycleViewAdapter(getActivity(), this.mData.getResults());
        this.mAllItemsView.setAdapter(this.mRecycleAdapter);
        this.mAllItemsView.setLayoutManager(new LinearLayoutManager(getActivity()));




        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            this.mData = (DataCommunication) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement DataCommunication");
        }
    }    
    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "Paused!");
    }   

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void update(Observable o, Object arg) {
        View root = getView();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements DataCommunication{

    private static final String TAG = "MainActivity";
    private SectionsPageAdapter mSectionsPageAdapter = null;
    private ViewPager pager = null;
    private  BloomDatabase userData = null;    
    private String selectedTitle = null;
    private String selectedContent = null;
    private List<GlobalLists> searchResults = null;    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Starting...");
        this.userData = BloomDatabase.getFileDatabase(this);
        this.searchResults = this.userData.listModel().loadAllLists();


        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        pager = (ViewPager) findViewById(R.id.container);
        setupViewPager(pager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(pager);


    }


    private void setupViewPager(ViewPager pager){

        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new itemView_fragment(), "Detail View");
        adapter.addFragment(new showAllLists_fragment(), "All Lists");
        adapter.addFragment(new addList_fragment(), "Add List");
        pager.setAdapter(adapter);
    }


    @Override
    public String getItemTitle(){
        return this.selectedTitle;
    }

    @Override
    public String getItemContents() {
        return this.selectedContent;
    }

    @Override
    public void setItemTitle(String title) {
        this.selectedTitle = title;
    }

    @Override
    public void setItemContent(String content) {
        this.selectedContent = content;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);

        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchDatabase = (SearchView) searchItem.getActionView();

        searchDatabase.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return searchTerm(query);
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean searchTerm(String title) {
        try {
            this.searchResults = this.userData.listModel().searchByTitle(title);
            Log.d(TAG, "Search Successful!");
            return true;
        } catch (Exception e){
            Log.d(TAG, "Search Failed \n -See Console");
            return false;
        }
    }

    @Override
    public List<GlobalLists> getResults() {
        return this.searchResults;
    }

    @Override
    public void refresh(){
        this.mSectionsPageAdapter.updateFragments();
    }

}

推荐答案

适配器类正确有两个错误

There are two mistake in your adapter class correct

1).您应该在getItemCount()

使用此

@Override
public int getItemCount() {
      return results.size();
}

代替这个

@Override
public int getItemCount() {
    return 0;
}

2)像下面的代码一样,将custom_row.xml的高度更改为wrap_content

2) Change your custom_row.xml hight to wrap_content like below code

使用此

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

代替这个

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customRow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

这篇关于房间数据库中没有显示在RecycleView中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆