Items not showing up in RecycleView from Room database

…衆ロ難τιáo~ 提交于 2019-12-12 12:28:07

问题


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.

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?

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

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

}

回答1:


There are two mistake in your adapter class correct

1). You should return the size of your data results ArrayList in getItemCount()

Use this

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

Instead of this

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

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

Use this

<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">

Instead of this

<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">


来源:https://stackoverflow.com/questions/49109641/items-not-showing-up-in-recycleview-from-room-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!