2016-02-06 91 views
-1

刚接触android时,我的代码显示recyclerview,直到我对我的item.xml布局文件进行了一些更改(后来我还原了它)。但是现在它不显示布局,只是敬酒味精。不知道出了什么问题..不显示回收视图的片段

这里基本上我解析XML文件显示在我的recyclerview。我的敬酒信息显示可用书籍的数量是一个,并显示其标题。

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

问题不在recycleView 问题是“for”循环在“RecyclerViewFrag”类的函数“initializeData”上。 for循环从来没有开始,因为我>总始终是假的,从 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)); }

+1

添加也为土司可以显示books.size不仅仅是总更多的检查 – Sally

+1

非常感谢莎莉!我错过了这样一件小事。 – Dep