2016-02-06 131 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