RecyclerView不显示项目,未报告任何错误

时间:2016-02-24 18:52:15

标签: java android android-fragments android-recyclerview

所以这就是问题的要点,我已经成功地在几个活动中实施了RecyclerViews,但是这个特定的RecyclerView并没有显示任何东西!

我在tablayout中创建了一个片段来显示,并且还重复了三次代码。以下是整个片段的代码(View正在充气,以及回收器顶部的搜索栏)适配器位于底部。我故意将OnBindViewHolder留空,因为我在aldready之前写了两次没有任何结果。任何帮助将不胜感激。

片段+适配器代码:

public static class EditJob extends Fragment {

    ServerRequests serverRequests;
    UserLocalStore userLocalStore;
    User receivedUser;
    EditText searchJob;
    ImageView searchBtn;
    RecyclerView editJobRecycler;
    MyRecyclerAdapter editJobAdapter;
    ArrayList<Job> arrayJob;
    TextView noJobSearch;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        serverRequests = new ServerRequests(context);
        userLocalStore = new UserLocalStore(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_job, container, false);
        receivedUser = userLocalStore.getAllDetails();
        arrayJob = new ArrayList<>();
        noJobSearch = (TextView) view.findViewById(R.id.noJobEdit);
        searchJob = (EditText) view.findViewById(R.id.etEditJob);
        searchBtn = (ImageView) view.findViewById(R.id.editJobImage);
        editJobRecycler = (RecyclerView)           view.findViewById(R.id.jobEditRecycler);
        editJobAdapter = new MyRecyclerAdapter(arrayJob);
        editJobRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        editJobRecycler.setAdapter(editJobAdapter);
        searchBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                searchJob();
           }
        });
        return view;
    }

    public void searchJob() {
        arrayJob.clear();
        serverRequests.searchJobListings(searchJob.getText().toString(), new     GetJobObjectsCallBack() {
            @Override
            public void done(Job[] returnedJobs) {
                if (returnedJobs == null || returnedJobs.length == 0) {
                    noJobSearch.setVisibility(View.VISIBLE);
                } else {
                    noJobSearch.setVisibility(View.GONE);
                    for (int i = 0; i < returnedJobs.length; i++) {
                        arrayJob.add(i, returnedJobs[i]);
                    }
                    editJobAdapter = new MyRecyclerAdapter(arrayJob);
                    editJobRecycler.setAdapter(editJobAdapter);
                    editJobAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    private class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.VH> {

       ArrayList<Job> jobArray;

        public MyRecyclerAdapter(ArrayList<Job> jl) {
            jobArray = jl;
        }

        public class VH extends RecyclerView.ViewHolder {
            TextView jobID, jobPosition, jobExperience, jobRemarks, jobLocation, jobDescription, jobDomain;
            EditText etjobID, etjobPosition, etjobExperience, etjobRemarks, etjobLocation, etjobDescription, etjobDomain;
            Button bEditJob;

            public VH(View v) {
                super(v);
                jobID = (TextView) v.findViewById(R.id.returnedJobID);
                jobExperience = (TextView) v.findViewById(R.id.returnedJobExperience);
                jobRemarks = (TextView) v.findViewById(R.id.returnedJobRemarks);
                jobLocation = (TextView) v.findViewById(R.id.returnedJobLocation);
                jobDescription = (TextView) v.findViewById(R.id.returnedJobDescription);
                jobDomain = (TextView) v.findViewById(R.id.returnedJobDomain);
                jobPosition = (TextView) v.findViewById(R.id.returnedJobPosition);
                etjobID = (EditText) v.findViewById(R.id.etreturnedJobID);
                etjobPosition = (EditText) v.findViewById(R.id.etreturnedJobPosition);
                etjobExperience = (EditText) v.findViewById(R.id.etreturnedJobExperience);
                etjobRemarks = (EditText) v.findViewById(R.id.etreturnedJobRemarks);
                etjobLocation = (EditText) v.findViewById(R.id.etreturnedJobLocation);
                etjobDescription = (EditText) v.findViewById(R.id.etreturnedJobDescription);
                etjobDomain = (EditText) v.findViewById(R.id.etreturnedJobDomain);
                bEditJob = (Button) v.findViewById(R.id.bEditJob);
            }
        }

        @Override
        public VH onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater li = LayoutInflater.from(parent.getContext());
            View v = li.inflate(R.layout.job_edit_listview, parent);
            return new VH(v);
        }

        @Override
        public void onBindViewHolder(VH holder, int position) {
            Job current = jobArray.get(position);
        }

        @Override
        public int getItemCount() {
            System.out.println("jobarray size = " + jobArray.size());
            return jobArray.size();
        }
    }
}

片段的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">
    <RelativeLayout
        style="@style/FloatingTextView"
        android:layout_marginTop="10dp"
        android:elevation="6dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:animateLayoutChanges="true">

        <EditText
            android:id="@+id/etEditJob"
            style="@style/FloatingTextView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="0.5dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/editJobDivider"
            android:elevation="0dp"
            android:hint="Enter a search term"
            android:translationZ="0dp" />

        <View
            android:id="@+id/editJobDivider"
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/editJobImage"
            android:alpha="0.2"
            android:background="@color/Black" />

        <ImageView
            android:id="@+id/editJobImage"
            style="@style/FloatingTextView"
            android:layout_width="47dp"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="1dp"
            android:alpha="0.5"
            android:clickable="true"
            android:elevation="0dp"
            android:padding="10dp"
            android:src="@drawable/ic_search_black_48dp"
            android:translationZ="0dp" />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No Jobs match your query"
        android:id="@+id/noJobEdit"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:id="@+id/jobEditRecycler"/>
</LinearLayout>

PS:我检查了在查询数据库时返回大小的数据集,也已通知适配器并且getItemCount返回非零大小。老实说我对这个问题一无所知!

编辑:这是我用于另一个静态内部类片段的(几乎)相同的代码。这个实现工作正常。

public static class JobSearcher extends Fragment {

        EditText searchJob;
        ImageView searchImage;
        RecyclerView searchJobListView;
        ArrayList<Job> arrayJob;
        ServerRequests serverRequests;
        Context parentActivity;
        UserLocalStore userLocalStore;
        TextView noJobSearch;
        MyJobRecyclerAdapter jobArrayAdapter;
        User receivedUser;

        @Override
        public void onAttach(Context activity) {
            super.onAttach(activity);
            parentActivity = activity;
            serverRequests = new ServerRequests(activity);
            userLocalStore = new UserLocalStore(activity);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_search_job, container, false);
            searchJob = (EditText) v.findViewById(R.id.searchJobEditText);
            searchImage = (ImageView) v.findViewById(R.id.searchJobImage);
            receivedUser = userLocalStore.getAllDetails();
            noJobSearch = (TextView) v.findViewById(R.id.noJobSearch);
            searchImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    searchJob();
                }
            });
            searchJobListView = (RecyclerView) v.findViewById(R.id.jobSearchRecycler);
            arrayJob = new ArrayList<>();
            jobArrayAdapter = new MyJobRecyclerAdapter(arrayJob);
            searchJobListView.setLayoutManager(new LinearLayoutManager(getContext()));
            searchJobListView.setAdapter(jobArrayAdapter);
            return v;
        }

        public void searchJob() {
            arrayJob.clear();
            serverRequests.searchJobListings(searchJob.getText().toString(), new GetJobObjectsCallBack() {
                @Override
                public void done(Job[] returnedJobs) {
                    if (returnedJobs == null || returnedJobs.length == 0) {
                        noJobSearch.setVisibility(View.VISIBLE);
                    } else {
                        noJobSearch.setVisibility(View.GONE);
                        for (int i = 0; i < returnedJobs.length; i++) {
                            arrayJob.add(i, returnedJobs[i]);
                        }
                        jobArrayAdapter = new MyJobRecyclerAdapter(arrayJob);
                        searchJobListView.setAdapter(jobArrayAdapter);
                        jobArrayAdapter.notifyDataSetChanged();
                    }
                }
            });
        }

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

            ArrayList<Job> arrayJob = new ArrayList<>();

            public MyJobRecyclerAdapter(ArrayList<Job> jl) {
                arrayJob = jl;
            }

            @Override
            public MyJobRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater li = LayoutInflater.from(parent.getContext());
                View inflatedView = li.inflate(R.layout.job_item_list, parent, false);
                ViewHolder VH = new ViewHolder(inflatedView);
                return VH;
            }

            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                Job currentJob = arrayJob.get(position);
                holder.jobDesc.setText("Job Description: " + currentJob.getDescription());
                holder.jobID.setText("ID: " + currentJob.getID());
                holder.jobRemarks.setText("Remarks: " + currentJob.getRemarks());
                holder.jobExperience.setText("Experience Required: " + currentJob.getExperience() + " years");
                holder.jobDomain.setText("Domain: " + currentJob.getDomain());
                holder.jobLoc.setText("Location: " + currentJob.getLocation());
                holder.jobType.setText("Type: " + currentJob.getType());
                holder.jobPos.setText("Position: " + currentJob.getPosition());
            }

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

            public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
                TextView jobID, jobDesc, jobType, jobLoc, jobPos, jobRemarks, jobExperience, jobDomain;
                Button applyForJob;

                public ViewHolder(View itemView) {
                    super(itemView);
                    jobID = (TextView) itemView.findViewById(R.id.returnedJobID);
                    jobDesc = (TextView) itemView.findViewById(R.id.returnedJobDescription);
                    jobLoc = (TextView) itemView.findViewById(R.id.returnedJobLocation);
                    jobType = (TextView) itemView.findViewById(R.id.returnedJobType);
                    jobPos = (TextView) itemView.findViewById(R.id.returnedJobPosition);
                    jobRemarks = (TextView) itemView.findViewById(R.id.returnedJobRemarks);
                    jobExperience = (TextView) itemView.findViewById(R.id.returnedJobExperience);
                    jobDomain = (TextView) itemView.findViewById(R.id.returnedJobDomain);
                    applyForJob = (Button) itemView.findViewById(R.id.bApplyJob);
                    applyForJob.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.bApplyJob:
                            serverRequests.applyForJob(receivedUser.getEmail(), receivedUser.getName(), jobID.getText().toString(), jobPos.getText().toString());
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您的ViewHolder必须使用视图才能显示。例如:

atan2()