如何正确解析此html并将其显示在可扩展的RecyclerView中

时间:2016-07-20 03:10:00

标签: android android-recyclerview jsoup recycler-adapter

我正在尝试使用Expandable Recyclerview和Jsoup来解析以下内容:

<div class="book-column">
   <div class="group">
      <h3 class="group-title">Authors</h3>
      <ul class="book-specs-list">
         <li>
            <strong class="book-term">Written with</strong>
            <div class="book-attr-value">
               <span class="book-1co">Tablet</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Model</strong>
            <div class="book-attr-value">
               <span class="book-1co">Android</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Published</strong>
            <div class="book-attr-value">
               <span class="book-1co">December 1945</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Released</strong>
            <div class="book-attr-value">
               <span class="book-1co">December 1945</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Status</strong>
            <div class="book-attr-value">
               <span class="book-1co">Available</span>
            </div>
         </li>
      </ul>
   </div>
   <div class="group">
      <h3 class="group-title">Design</h3>
      <ul class="book-specs-list">
         <li>
            <strong class="book-term book-tooltip">Type</strong>
            <div class="book-attr-value">
               <span class="book-1co">Crime fiction</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Dimensions</strong>
            <div class="book-attr-value">
               <span class="book-1co">211*123*8.3mm</span>
            </div>
         </li>
         <li>
            <strong class="book-term">Colors</strong>
            <div class="book-attr-value">
               <span class="book-1co">Green</span>
            </div>
         </li>
      </ul>
   </div>
</div>

我想要的是这样的

作者

Tablet      Tablet

Model       Droidpad 8D

Published   December 1945

Released    April2016

Status      Available

设计

Type        Crime fiction

Dimension   211*123*8.3mm

Colors      Green

但数据没有显示,但我可以在logcat中看到上述值。

我的代码和布局:

fragment_gadget.xml

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ozuf-popularbooks.Fragment.GadgetFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:padding="5dp">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gad_features"
            android:layout_below="@+id/gad_gallery"
            android:layout_marginTop="10dp"/>

    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

gad_parent_feat.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="1dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="@string/accept"
        android:layout_margin="10dp"
        android:id="@+id/gad_cat_title"/>

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

gad_child_feat.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:id="@+id/gad_feat_term"
        android:text="@string/about"/>

    <View
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="#000"
        android:id="@+id/gad_child_line"
        android:layout_marginRight="10dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/gad_feat_term"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/gad_feat_term"
        android:layout_marginLeft="10dp"
        android:text="@string/sth_wrongme"
        android:id="@+id/gad_feat_value"/>

</RelativeLayout>

GadgetFragment

public class GadgetFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");

        mRecyclerView = (RecyclerView) view.findViewById(R.id.gad_features);
        mParentAdapter = new GadParentAdapter(getActivity(), mGadParentItems);
        mRecyclerView.setAdapter(mParentAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return view;
    }

    private void loadGad() {
        Log.d(TAG, "loadGad called");
        progressCrime fiction.setVisibility(View.VISIBLE);


        stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Log.d("Debug", response.toString());

                        progressCrime fiction.setVisibility(View.GONE);

                        parseGad(response);
                        gadData = response;


                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Log.d(TAG,  "onErrorResponse for loadad called");

                        progressCrime fiction.setVisibility(View.GONE);

                        if (sthWrongAlert != null) {
                            sthWrongAlert.show();
                        }


                    }
                });

        //Creating requestqueue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //Adding request queue
        requestQueue.add(stringRequest);


    }

    private void parseGad(String response) {
        Log.d(TAG, "parsingGad");
        Document document = Jsoup.parse(response);

        Elements column = document.select("div.book-column");
        int index = 0;

        for (Element uli : column.select("div.group")) {
            Log.d(TAG, "Category " + (++index));

            Log.d(TAG, "Head is " + uli.select("h3.group-title").text());

            for (Element columli : uli.select("ul.book-specs-list > li")) {
                Log.d(TAG, "Category\t " + (++index));
                Elements columli_li = columli.select("li");
                Log.d(TAG, "Cat\t" + columli_li.select("strong.book-term").text() + ":" + columli_li.select("div.book-attr-value").text());
                GadChildItem gadChildItem = new GadChildItem();
                gadChildItem.setChildTerm(columli_li.select("strong.book-term").html());
                gadChildItem.setChildValue(columli_li.select("div.book-attr-value").html());

                new GadParentItem(uli.select("h3.group-title").html(), mGadChildItems);
                mParentAdapter.notifyItemRangeChanged(0, mGadParentItems.size());
            }
        }

        Log.d("jsoup", "size: " + column.size());

    }

GadParentItem

public class GadParentItem implements ParentListItem {
    private String parentTitle;
    private List<GadChildItem> mGadChildItems;

    public GadParentItem(String title, List<GadChildItem> gadChildItemList) {
        parentTitle = title;
        mGadChildItems = gadChildItemList;
    }

    public String getParentTitle() {
        return parentTitle;
    }

    @Override
    public List<?> getChildItemList() {
        return mGadChildItems;
    }

    @Override
    public boolean isInitiallyExpanded() {
        return true;
    }
}

**GadChildItem**

public class GadChildItem {
    private String childTerm;
    private String childValue;

    public String getChildTerm() {
        return childTerm;
    }

    public void setChildTerm(String childTerm) {
        this.childTerm = childTerm;
    }

    public String getChildValue() {
        return childValue;
    }

    public void setChildValue(String childValue) {
        this.childValue = childValue;
    }
}

GadParentViewHolder

public class GadParentViewHolder extends ParentViewHolder {

    private TextView parentTitle;

    public GadParentViewHolder(View itemView) {
        super(itemView);
        parentTitle = (TextView) itemView.findViewById(R.id.gad_cat_title);
    }

    public void bind(GadParentItem gadParentItem) {
        parentTitle.setText(gadParentItem.getParentTitle());
    }
}

GadChildViewHolder

public class GadChildViewHolder extends ChildViewHolder {

      private TextView term, value;

      public GadChildViewHolder(View itemView) {
          super(itemView);
          term = (TextView) itemView.findViewById(R.id.gad_feat_term);
          value = (TextView) itemView.findViewById(R.id.gad_feat_value);
      }

      public void bind (GadChildItem gadChildItem) {
          term.setText(gadChildItem.getChildTerm());
          value.setText(gadChildItem.getChildValue());
      }

GadParentAdapter

public class GadParentAdapter extends ExpandableRecyclerAdapter<GadParentViewHolder, GadChildViewHolder> {

    private LayoutInflater mInflater;

    public GadParentAdapter(Context context, @NonNull List<? extends ParentListItem> parentItemList) {
        super(parentItemList);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public GadParentViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
        View parentView = mInflater.inflate(R.layout.gad_parent_feat, parentViewGroup, false);
        return new GadParentViewHolder(parentView);
    }

    @Override
    public GadChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
        View childView = mInflater.inflate(R.layout.gad_child_feat, childViewGroup, false);
        return new GadChildViewHolder(childView);
    }

    @Override
    public void onBindParentViewHolder(GadParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
        GadParentItem gadParentItem = (GadParentItem) parentListItem;
        parentViewHolder.bind(gadParentItem);
    }

    @Override
    public void onBindChildViewHolder(GadChildViewHolder childViewHolder, int position, Object childListItem) {
        GadChildItem gadChildItem = (GadChildItem) childListItem;
        childViewHolder.bind(gadChildItem);
    }
}

值得注意的是,我正在解析的数据在数量和内容方面是100%动态的,我无法编辑html。

拜托,您知道问题可能是什么吗?

0 个答案:

没有答案