解析在RecyclerView中不显示的对象

时间:2016-01-26 13:40:41

标签: android parse-platform android-recyclerview

我正在尝试从Parse数据库中获取数据并将其显示到RecyclerView中。根据我的知识,我无法看到我做错了什么,当我运行应用程序时不会发生错误,并且当打开这个片段时,应用程序不会崩溃只显示一个空白屏幕。

知道这是什么问题吗?

企业

package zafir.com.app;

import com.parse.ParseClassName;
import com.parse.ParseObject;


@ParseClassName("Businesses")
public class Businesses extends ParseObject
{

    private String Name;

    public String getName()
    {
        return getString("Name");
    }

    public void setName(String name)
    {
        put("Name", name);
    }

    public String getCategory()
    {
        return getString("Category");
    }

    public void setCategory(String category)
    {
        put("Category", category);
    }

    public String getEmail()
    {
        return getString("Email");
    }

    public void setEmail(String email)
    {
        put("Email", email);
    }

    public String getLocation()
    {
        return getString("Location");
    }

    public void setLocation(String location)
    {
        put("Location", location);
    }

    public String getPhone()
    {
        return getString("Phone");
    }

    public void setPhone(String phone)
    {
        put("Phone", phone);
    }

    public String getWebsite()
    {
        return getString("Website");
    }

    public void setWebsite(String website)
    {
        put("Website", website);
    }
}

RecyclerAdapter

public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView zName;
        public TextView zPhone;
        public TextView zEmail;
        public TextView zWebsite;
        public TextView zLocation;
        public TextView zCategory;

        public ViewHolder(View itemView)
        {
            super(itemView);

            zName = (TextView) itemView.findViewById(R.id.name);
            zPhone = (TextView) itemView.findViewById(R.id.phone);
            zEmail = (TextView) itemView.findViewById(R.id.email);
            zWebsite = (TextView) itemView.findViewById(R.id.website);
            zLocation = (TextView) itemView.findViewById(R.id.location);
            zCategory = (TextView) itemView.findViewById(R.id.category);
        }
    }

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

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View recView = inflater.inflate(R.layout.recycler_layout, parent, false);

        ViewHolder ViewHolder = new ViewHolder(recView);

        return ViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int position)
    {
        Businesses businesses = data.get(position);

        TextView name = viewHolder.zName;
        name.setText(businesses.getName());

        TextView phone = viewHolder.zPhone;
        phone.setText(businesses.getPhone());

        TextView email = viewHolder.zEmail;
        email.setText(businesses.getEmail());

        TextView website = viewHolder.zWebsite;
        website.setText(businesses.getWebsite());

        TextView location = viewHolder.zLocation;
        location.setText(businesses.getLocation());

        TextView category = viewHolder.zCategory;
        category.setText(businesses.getCategory());
    }

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

分类(片段)

    public class Categories extends Fragment
{

    List<Businesses> data = new ArrayList<>();
    private RecyclerView zRecyclerView;
    private RecyclerAdapter zAdapter;
    private RecyclerView.LayoutManager zLayoutManager;


    public Categories()
    {
        // Required empty public constructor
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_categories,container, false);
        zRecyclerView = (RecyclerView) rootView.findViewById(R.id.recview_categories);
        zRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        getData();

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void getData()
    {
        final List<Businesses> data = new ArrayList<>();

        ParseQuery<Businesses> query = new ParseQuery<>("Businesses");
        query.findInBackground(new FindCallback<Businesses>()
        {
            @Override
            public void done(List<Businesses> list, ParseException e)
            {
                if(e == null)
                {
                    for(Businesses businesses : list)
                    {
                        Businesses bizList = new Businesses();
                        bizList.setNames(businesses.getNames());
                        bizList.setPhone(businesses.getPhone());
                        bizList.setEmail(businesses.getEmail());
                        bizList.setWebsite(businesses.getWebsite());
                        bizList.setLocation(businesses.getLocation());
                        bizList.setCategory(businesses.getCategory());
                        data.add(bizList);
                    }

                    zAdapter = new RecyclerAdapter(getActivity(), data);
                    zRecyclerView.setAdapter(zAdapter);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

一切看起来都不错。只要您更改数据,就必须在notifyDataSetChanged()上致电zAdapter。所以改变你的代码:

zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
zAdapter.notifyDataSetChanged();