简体   繁体   English

RecyclerView未被填充(getItemCount()返回零)

[英]RecyclerView not being populated (getItemCount() is returning zero)

I looked at the other questions on SO about this, and matched by code word for word but I must have missed something because nothing is working. 我查看了有关此问题的其他问题,并逐字逐句地匹配,但我一定错过了一些东西,因为没有任何工作。 getItemCount() is returning zero, but I debugged my data list and it seemed to add all my data properly, so something is going wrong with the communication to the adapter. getItemCount()返回零,但我调试了我的数据列表,它似乎正确地添加了我的所有数据,因此与适配器的通信出了问题。

Please tell me what's going wrong here! 请告诉我这里出了什么问题!

Below is my code for MainActivity.java: 下面是我的MainActivity.java代码:

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;


public class Profile_Page extends ActionBarActivity {
    UserLocalStore userLocalStore;
    private RecyclerView recyclerView;
    private DataAdapter adapter;
    private Context context;
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    public List<Information> data = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile__page);
        ParseUser currentUser = ParseUser.getCurrentUser();
        String struser = currentUser.getUsername();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 

        Button logoutBtn = (Button) findViewById(R.id.logOutBtn);



        adapter = new DataAdapter(getApplicationContext(), getData()); 
        recyclerView.setAdapter(adapter); 
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

   public List<Information> getData()
{
    ParseQuery <ParseObject> query = ParseQuery.getQuery("ParseClassName");
        query.whereEqualTo("author", ParseUser.getCurrentUser());
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) { 
                if (e == null) {
                    for (ParseObject getData : list) { 
                        Information current = new Information();
                        current.thing= getData.getString("Thing");                                           
                        data.add(current); 
                        adapter.notifyDataSetChanged();
                        //for some reason, data isn't getting transferred to adapter


                    }

                } else {
                    //something went wrong
                }

            }


        });
return data;
    }


    public void logOut(View view)
    {
        // Logout current user
        ParseUser.logOut();
        finish();
    }
}

Code for DataAdapter: DataAdapter代码:

import android.content.Context;
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.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>
{
    private LayoutInflater inflater;
    List<Information> data = Collections.emptyList();



    public DataAdapter(Context context, List<Information> data) 
    {
        inflater = LayoutInflater.from(context);
        this.data= data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = inflater.inflate(R.layout.recycler_view_layout, parent, false); 
        MyViewHolder holder = new MyViewHolder(view); 
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        Information current = data.get(position);
        holder.tv.setText(current.thing);

    }

    @Override
    public int getItemCount()
    {   Log.d("datasize", String.valueOf(data.size()));
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder
    {    TextView tv;




        public MyViewHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.thingID);


        }
    }
}

Information.java Information.java

public class Information
{
    public String thing;


}

Recycler_view_layout.xml Recycler_view_layout.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dummy Text"
        android:id="@+id/thingID"/>


</LinearLayout>

Activity in which the RecyclerView is added: 添加RecyclerView的活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.blah.Profile_Page"
    >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button3"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/recyclerView"
        android:layout_alignEnd="@+id/recyclerView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log out"
        android:id="@+id/logOutBtn"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="logOut"
        android:layout_marginTop="85dp" />


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height = "wrap_content"
        android:id="@+id/recyclerView"
        android:layout_alignTop="@id/logOutBtn" />

</RelativeLayout>

Maybe if u set adapter inside done method it will work. 也许如果你在适当的方法中设置适配器它将工作。 Insert bottom for loop. 插入底部循环。

adapter = new DataAdapter(getApplicationContext(), data); 
recyclerView.setAdapter(adapter); 
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

每当相关数据集发生变化时,您都需要调用RecyclerView.Adapter.notifyDataSetChanged()。

I think you should do like this: 我想你应该这样做:

mXxxData.clear();  
mXxxData.addAll(reqForInternetData);  
mRecyclerViewAdapter.notifyItemInserted(0);

That is worked for me. 这对我有用。

Try setting the adapter only after setting the layout manager 尝试仅在设置布局管理器后设置适配器

recyclerView.setLayoutManager(new  
LinearLayoutManager(getApplicationContext()));
adapter = new DataAdapter(getApplicationContext(), data); 
recyclerView.setAdapter(adapter); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView 不调用 getItemCount - RecyclerView not calling getItemCount 为什么RecyclerView行使用相同的数据填充? - Why are RecyclerView rows being populated with same data? RecyclerView 里面的 RecyclerView 弄错了 getItemCount() - RecyclerView inside RecyclerView made wrong getItemCount() RecyclerView getChildCount() 和 getItemCount() 返回相同的值 - RecyclerView getChildCount() and getItemCount() returns same value RecyclerView 适配器的 getItemCount 抛出 NullPointerException - RecyclerView adapter's getItemCount throws NullPointerException 未调用RecyclerView重写方法,并且getItemCount始终返回0 - RecyclerView Overriden methods not called and getItemCount always return 0 如何搜索从SQLite数据库填充的recyclerView? - How to search through a recyclerView which is being populated from SQLite database? 我们可以从RecyclerView的getItemCount()中获得两种返回类型吗? - Can we get two return types from getItemCount() in RecyclerView? 如何将 ArrayList 大小从 onBindViewHolder 传递给 RecyclerView 中的 getItemCount() 方法? - How to pass ArrayList size from onBindViewHolder to getItemCount() method in RecyclerView? 我的 RecyclerView 适配器的 getItemCount() 返回 0,即使里面有项目 - getItemCount() of my RecyclerView adapter returns 0 even though I have items in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM