片段不显示recycleview

时间:2016-02-06 12:03:03

标签: android xml android-fragments android-recyclerview

Android的新手,我的代码显示了recyclerview,直到我对我的item.xml布局文件进行了一些更改,我后来还原了。但现在它没有显示布局,只是toast msg。不知道出了什么问题..

这里基本上我正在解析xml文件以在我的recyclerview中显示。我的toast msg显示可用的书籍数量是一本并且也显示了它的标题。

Recycleviewfrag.java

package com.androidatc.customviewindrawer;

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
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.TextView;
import android.widget.Toast;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class RecyclerViewFrag extends Fragment {

    public List<Book> books;
    public RecyclerView rv;
    public TextView formatTxt, contentTxt, TitleTxt, PublisherTxt, CreatorTxt, AvailabiltyTxt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.recyclerview, container, false);

        String response =getArguments().getString("book_xml");

        rv=(RecyclerView)rootView.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        readDetails(response);
        initializeAdapter();

        return rootView;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }


    public void initializeData(String[] titles, String [] creators, int total){
        books = new ArrayList<>();

        for(int i = 0;i>total;i++)
        {
            books.add(new Book(titles[i], creators[i], R.drawable.barscan));
        }
        Toast.makeText(getActivity().getApplicationContext(), "Total Number of Books Found:"
                + total + " " + titles[0], Toast.LENGTH_LONG).show();
    }

    public void initializeAdapter(){
        BookListAdapter adapter = new BookListAdapter(books);
        rv.setAdapter(adapter);
    }


    public void readDetails(String response) {
        DocumentBuilder builder = null;

        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource src = new InputSource();

            src.setCharacterStream(new StringReader(response));
            Document doc = builder.parse(src);

            NodeList nodes1 = doc.getElementsByTagName("dc:title");
            src.setCharacterStream(new StringReader(response));

           NodeList nodes = doc.getElementsByTagName("dc:duedate");

            int cnt = 0;
            String[] titles1 = new String[10000];
            String[] titles = new String[10000];
            String[] creators = new String[10000];

            for (int i = 0; i < nodes.getLength(); i++) {

                if (nodes.item(i).getTextContent() == "dc:title") {
                    titles1[i] = doc.getElementsByTagName("dc:title").item(0).getTextContent();

                }

                if (nodes.item(i).getTextContent().trim().isEmpty()) {
                    cnt++;
                }

            }
            Log.e("TAGLOG", "" + cnt);
            for (int i = 0; i < nodes1.getLength(); i++) {
                    titles[i] = doc.getElementsByTagName("dc:title").item(0).getTextContent();
                    creators[i] = doc.getElementsByTagName("dc:creator").item(0).getTextContent();
            }

              initializeData(titles, creators, nodes1.getLength());

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getActivity().getApplicationContext(), "No Book Found", Toast.LENGTH_LONG).show();
        } finally {

        }
    }}

Booklistadapter.java

package com.androidatc.customviewindrawer;

import android.support.v7.widget.CardView;
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 java.util.List;

public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView title;
        TextView creator;
        ImageView personPhoto;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            title = (TextView)itemView.findViewById(R.id.title);
            creator = (TextView)itemView.findViewById(R.id.creator);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
        }
    }

    List<Book> books;

    BookListAdapter(List<Book> books){
        this.books = books;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.title.setText(books.get(i).title);
        personViewHolder.creator.setText(books.get(i).creator);
        personViewHolder.personPhoto.setImageResource(books.get(i).photoId);
    }

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

recycleview.xml

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



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

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


</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_photo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_alignParentTop="true"
            android:textSize="30sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/creator"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_below="@+id/title"
            />

    </RelativeLayout>

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

cardview.xml

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/title"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_alignParentTop="true"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/creator"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_below="@+id/title"
                />





        </RelativeLayout>

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

</LinearLayout>

我在下面的代码中调用了searchbooklistfrag中的片段:

public void replaceFragment(String response) {

        try {

            Fragment fr=new RecyclerViewFrag();
            final FragmentTransaction ft = getFragmentManager().beginTransaction();
            Bundle args = new Bundle();
            args.putString("book_xml", response);
            fr.setArguments(args);
            ft.replace(R.id.search_view, fr);
            ft.addToBackStack(null);
            ft.commit();

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getActivity().getApplicationContext(), "Unable to change fragment", Toast.LENGTH_LONG).show();
        }

    }

我仍然看到上面(上一个)片段的布局。

花了几个小时但却无法弄清楚是什么错。欢迎任何建议。

1 个答案:

答案 0 :(得分:1)

问题不在recycleView上 问题是&#34;对于&#34;函数循环&#34; initializeData&#34; on&#34; RecyclerViewFrag&#34;类。 for循环永远不会启动,因为i&gt; total总是为false 改变它 for(int i = 0;i>total;i++) { books.add(new Book(titles[i], creators[i], R.drawable.barscan)); } for(int i = 0;i<total;i++) { books.add(new Book(titles[i], creators[i], R.drawable.barscan)); }