【问题标题】:How to parse an xml to java class with recycler view or in adapter of recyclerview如何使用回收器视图或在回收器视图的适配器中将 xml 解析为 java 类
【发布时间】:2019-05-03 08:28:42
【问题描述】:

我有一个名为 Bookmark 的 xml 文件,它位于 xml 文件夹中。 我想将 xml 解析为回收器视图,我可以在回收器视图中显示书签列表。 书签 xml 它不在 assets 中,它在 res 和 xml 文件夹中。 这是我的代码。

xml文件夹中的Bookmark.xml

<Bookmarks>
    <Bookmark id="1" icon="google.png" name="Google" searchUrl="https://www.google.com" hidden="true" />
    <Bookmark id="2" icon="youtube_new.png" name="Youtube" searchUrl="http://m.youtube.com" />
    <Bookmark id="3" icon="facebook.png" name="Facebook" nativeUrl="facebook://" searchUrl="https://m.facebook.com" />
    <Bookmark id="4" icon="twitter.png" name="Twitter" searchUrl="https://mobile.twitte.com" />
    <Bookmark id="5" icon="instagram.png" name="Instagram" nativeUrl="instagram://" searchUrl="https:instagram.com" />
    <Bookmark id="6" icon="gmail.png" name="Gmail" nativeUrl="googlemail://" searchUrl="https://gmail.com" />
    <Bookmark id="7" icon="google_translate.png" name="Translate" searchUrl="https://" />

</Bookmarks>

这是回收器视图的java类

 public class FragmentBookmark extends Fragment {
    ArrayList<Bookmark> arrayList = new ArrayList<>();
    XmlPullParserFactory pullParserFactory;
    RecyclerView myRecyclerView;
    MyAdapter myAdapter;

    public void onCreateView(@Nullable Bundle savedInstanceState) {
        myRecyclerView = getActivity().findViewById(R.id.myRecyclerView);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
        myRecyclerView.setHasFixedSize(true);
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream in_s = getActivity().getApplicationContext().getAssets().open("bookmarks.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_s, null);

            parseXML(parser);

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < arrayList.size(); i++) {
            Log.e("OUTPUT", arrayList.get(i).toString());
        }

        myAdapter = new MyAdapter(getActivity(), arrayList);
        myRecyclerView.setAdapter(myAdapter);
    }

    private void parseXML(XmlPullParser parser) throws XmlPullParserException, IOException {
        ArrayList<Bookmark> countries = null;
        int eventType = parser.getEventType();
        Bookmark country = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name;
            name = parser.getName();
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    countries = new ArrayList();
                    break;
                case XmlPullParser.START_TAG:
                    break;

                case XmlPullParser.END_TAG:
                    if (name.equals("Bookmark")) {

                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(parser.getAttributeValue(null, "name"));
                        bookmark.setIcon(parser.getAttributeValue(null, "icon"));
                        bookmark.setId(parser.getAttributeValue(null, "id"));
                        bookmark.setSearchUrl(parser.getAttributeValue(null, "searchUrl"));
                        bookmark.setNativeUrl(parser.getAttributeValue(null, "nativeUrl"));
                        arrayList.add(bookmark);
                    }
                    break;
            }
            eventType = parser.next();
        }


    }

    private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException {
        int eventType = parser.getEventType();
        Bookmark bookmark = null;


    }
    }

这是回收站视图 xml

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/listRecyclerView"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:fillViewport="false">

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

这是recyclerview的适配器

public class MyAdapter extends RecyclerView.Adapter {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_item, viewGroup, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((ListViewHolder) viewHolder).bindView(i);
    }

    @Override
    public int getItemCount() {
        return OurData.title.length;
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mItemText;
        private ImageView mItemImage;

        public ListViewHolder(View itemView) {
            super(itemView);
            mItemText = itemView.findViewById(R.id.textView);
            mItemImage =  itemView.findViewById(R.id.image_view);
            itemView.setOnClickListener(this);
        }

        public void bindView(int position) {
            mItemText.setText(OurData.title[position]);
            mItemImage.setImageResource(OurData.picture[position]);

        }

        @Override
        public void onClick(View v) {

        }
    }
}

这是我手动添加并在回收站视图中工作的 java 类,但我需要书签 xml 才能在回收站视图中显示

public class OurData {
    public static String[] title = new String[] {
            "Bing",
            "Facebook",
            "Gmail",
            "Translate",
            "Bing",
            "Facebook",
            "Gmail",
            "Translate"
    };

    public static int[] picture = new int[] {
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram,
            R.drawable.instagram

    };
}

这是显示图像和用于回收站视图的 TextView 的 xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:id="@+id/recyclerView">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="28dp"
        android:layout_height="27dp"
        android:layout_alignParentTop="true"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.129"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="16dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="80dp"
        android:layout_height="23dp"
        android:layout_below="@+id/image_view"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintHorizontal_bias="0.069"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image_view" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: java android xml android-recyclerview xml-parsing


    【解决方案1】:

    更新 2

    片段书签

    public class FragmentBookmark extends Fragment {
    
    
        public FragmentBookmark() {
            // Required empty public constructor
        }
    
        private Context mContext;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
        XmlPullParserFactory pullParserFactory;
    
        RecyclerView myRecyclerView;
        DataAdapter dataAdapter;
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mContext = context;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
    
            View rootView = inflater.inflate(R.layout.fragment_bookmark, container, false);
    
            myRecyclerView = rootView.findViewById(R.id.myRecyclerView);
            myRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            myRecyclerView.setHasFixedSize(true);
    
            dataAdapter = new DataAdapter(mContext, arrayList);
            myRecyclerView.setAdapter(dataAdapter);
    
            try {
    
                XmlPullParser xpp = getResources().getXml(R.xml.bookmarks);
    
                while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                    if (xpp.getEventType() == XmlPullParser.START_TAG) {
                        if (xpp.getName().equals("Bookmark")) {
    
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(0) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(1) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(5) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(2) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(3) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(4) + " * ");
    
    
                            Bookmark bookmark = new Bookmark();
                            bookmark.setName(xpp.getAttributeValue(0));
    
                            int drawableResourceId = this.getResources().getIdentifier(xpp.getAttributeValue(1), "drawable", mContext.getPackageName());
                            bookmark.setIcon(drawableResourceId);
    
                            bookmark.setId(xpp.getAttributeValue(2));
    
                            bookmark.setSearchUrl(xpp.getAttributeValue(3));
                            bookmark.setNativeUrl(xpp.getAttributeValue(4));
                            arrayList.add(bookmark);
    
                        }
                    }
    
                    xpp.next();
                }
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dataAdapter.notifyDataSetChanged();
            return rootView;
        }
    
    }
    

    layout.fragment_bookmark

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    数据适配器

    public class DataAdapter  extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    
        private Context context;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
    
        public DataAdapter(Context context, ArrayList<Bookmark> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    
            View view=LayoutInflater.from(context).inflate(R.layout.custom_layout,parent,false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.tvName.setText(arrayList.get(position).getName());
    
            holder.tvIcon.setImageResource(arrayList.get(position).getIcon());
    
            holder.tvId.setText(arrayList.get(position).getId());
            holder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            holder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView tvName,tvId,tvSearchUrl,tvNativeUrl;
    
            ImageView tvIcon;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
    
                tvName=itemView.findViewById(R.id.tvName);
                tvIcon=itemView.findViewById(R.id.tvIcon);
                tvId=itemView.findViewById(R.id.tvId);
                tvSearchUrl=itemView.findViewById(R.id.tvSearchUrl);
                tvNativeUrl=itemView.findViewById(R.id.tvNativeUrl);
            }
        }
    }
    

    layout.custom_layout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Name  : " />
    
                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Icon  : " />
    
                <ImageView
                    android:id="@+id/tvIcon"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Id  : " />
    
                <TextView
                    android:id="@+id/tvId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="SearchUrl  : " />
    
                <TextView
                    android:id="@+id/tvSearchUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="NativeUrl  : " />
    
                <TextView
                    android:id="@+id/tvNativeUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    书签模型类

    public class Bookmark
    {
        String name,id,nativeUrl,searchUrl;
        int icon;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public int getIcon() {
            return icon;
        }
    
        public void setIcon(int icon) {
            this.icon = icon;
        }
    
        public String getNativeUrl() {
            return nativeUrl;
        }
    
        public void setNativeUrl(String nativeUrl) {
            this.nativeUrl = nativeUrl;
        }
    
        public String getSearchUrl() {
            return searchUrl;
        }
    
        public void setSearchUrl(String searchUrl) {
            this.searchUrl = searchUrl;
        }
    
        @Override
        public String toString() {
            return "Bookmark{" +
                    "name='" + name + '\'' +
                    ", icon='" + icon + '\'' +
                    ", id='" + id + '\'' +
                    ", nativeUrl='" + nativeUrl + '\'' +
                    ", searchUrl='" + searchUrl + '\'' +
                    '}';
        }
    }
    

    更新

    XML 文件

    <Bookmarks>
        <Bookmark
            name="Google"
            hidden="true"
            icon="google.png"
            id="1"
            nativeUrl=""
            searchUrl="https://www.google.com" />
        <Bookmark
            name="Youtube"
            hidden=""
            icon="youtube_new.png"
            id="2"
            nativeUrl=""
            searchUrl="http://m.youtube.com" />
        <Bookmark
            name="Facebook"
            hidden=""
            icon="facebook.png"
            id="3"
            nativeUrl="facebook://"
            searchUrl="https://m.facebook.com" />
        <Bookmark
            name="Twitter"
            hidden=""
            icon="twitter.png"
            id="4"
            nativeUrl=""
            searchUrl="https://mobile.twitte.com" />
        <Bookmark
            name="Instagram"
            hidden=""
            icon="instagram.png"
            id="5"
            nativeUrl="instagram://"
            searchUrl="https:instagram.com" />
        <Bookmark
            name="Gmail"
            hidden=""
            icon="gmail.png"
            id="6"
            nativeUrl="googlemail://"
            searchUrl="https://gmail.com" />
        <Bookmark
            name="Translate"
            hidden=""
            icon="google_translate.png"
            id="7"
            nativeUrl=""
            searchUrl="https://" />
    
    </Bookmarks>
    

    这是从res/XML文件夹解析XML的代码

        try {
    
            XmlPullParser xpp = getResources().getXml(R.xml.bookmarks);
    
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("Bookmark")) {
    
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(0) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(1) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(5) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(2) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(3) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(4) + " * ");
    
                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(xpp.getAttributeValue(0));
                        bookmark.setIcon(xpp.getAttributeValue(1));
                        bookmark.setId(xpp.getAttributeValue(2));
                        bookmark.setSearchUrl(xpp.getAttributeValue(3));
                        bookmark.setNativeUrl(xpp.getAttributeValue(4));
                        arrayList.add(bookmark);
    
                    }
                }
    
                xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    我们可以使用三种类型的 android XML 解析器。

    • SAX Parsers

    • DOM Parsers

    • XMLPullParser

    您可以使用XmlPullParserFactory 来解析您的xml

    试试这个是工作代码

    主活动

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlPullParserFactory;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        ArrayList<Bookmark> arrayList = new ArrayList<>();
        XmlPullParserFactory pullParserFactory;
    
        RecyclerView myRecyclerView;
        DataAdapter dataAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myRecyclerView = findViewById(R.id.myRecyclerView);
            myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            myRecyclerView.setHasFixedSize(true);
            try {
                pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();
    
                InputStream in_s = getApplicationContext().getAssets().open("bookmark.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);
    
                parseXML(parser);
    
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < arrayList.size(); i++) {
                Log.e("OUTPUT", arrayList.get(i).toString());
            }
    
            dataAdapter = new DataAdapter(this, arrayList);
            myRecyclerView.setAdapter(dataAdapter);
        }
    
        private void parseXML(XmlPullParser parser) throws XmlPullParserException, IOException {
            ArrayList<Bookmark> countries = null;
            int eventType = parser.getEventType();
            Bookmark country = null;
    
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name;
                name = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        countries = new ArrayList();
                        break;
                    case XmlPullParser.START_TAG:
                        break;
    
                    case XmlPullParser.END_TAG:
                        if (name.equals("Bookmark")) {
                            Log.e("VALUE", parser.getAttributeValue(null, "name") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "icon") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "id") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "searchUrl") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "nativeUrl") + "");
    
                            Bookmark bookmark = new Bookmark();
                            bookmark.setName(parser.getAttributeValue(null, "name"));
                            bookmark.setIcon(parser.getAttributeValue(null, "icon"));
                            bookmark.setId(parser.getAttributeValue(null, "id"));
                            bookmark.setSearchUrl(parser.getAttributeValue(null, "searchUrl"));
                            bookmark.setNativeUrl(parser.getAttributeValue(null, "nativeUrl"));
                            arrayList.add(bookmark);
                        }
                        break;
                }
                eventType = parser.next();
            }
    
    
        }
    
        private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException {
            int eventType = parser.getEventType();
            Bookmark bookmark = null;
    
    
        }
    }
    

    layout.activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    数据适配器

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class DataAdapter  extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    
        private Context context;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
    
        public DataAdapter(Context context, ArrayList<Bookmark> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    
            View view=LayoutInflater.from(context).inflate(R.layout.custom_layout,parent,false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.tvName.setText(arrayList.get(position).getName());
            holder.tvIcon.setText(arrayList.get(position).getIcon());
            holder.tvId.setText(arrayList.get(position).getId());
            holder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            holder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView tvName,tvIcon,tvId,tvSearchUrl,tvNativeUrl;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
    
                tvName=itemView.findViewById(R.id.tvName);
                tvIcon=itemView.findViewById(R.id.tvIcon);
                tvId=itemView.findViewById(R.id.tvId);
                tvSearchUrl=itemView.findViewById(R.id.tvSearchUrl);
                tvNativeUrl=itemView.findViewById(R.id.tvNativeUrl);
            }
        }
    }
    

    layout.custom_layout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Name  : " />
    
                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Icon  : " />
    
                <TextView
                    android:id="@+id/tvIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Id  : " />
    
                <TextView
                    android:id="@+id/tvId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="SearchUrl  : " />
    
                <TextView
                    android:id="@+id/tvSearchUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="NativeUrl  : " />
    
                <TextView
                    android:id="@+id/tvNativeUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    书签模型类

    public class Bookmark
    {
        String name,icon,id,nativeUrl,searchUrl;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getIcon() {
            return icon;
        }
    
        public void setIcon(String icon) {
            this.icon = icon;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getNativeUrl() {
            return nativeUrl;
        }
    
        public void setNativeUrl(String nativeUrl) {
            this.nativeUrl = nativeUrl;
        }
    
        public String getSearchUrl() {
            return searchUrl;
        }
    
        public void setSearchUrl(String searchUrl) {
            this.searchUrl = searchUrl;
        }
    
        @Override
        public String toString() {
            return "Bookmark{" +
                    "name='" + name + '\'' +
                    ", icon='" + icon + '\'' +
                    ", id='" + id + '\'' +
                    ", nativeUrl='" + nativeUrl + '\'' +
                    ", searchUrl='" + searchUrl + '\'' +
                    '}';
        }
    }
    

    更多信息请查看下面的帖子

    输出

    【讨论】:

    • 你知道图标是来自 drawable-hdpi 的图像并且显示图标而不是字符串。
    • @Spritzig 您的图标存储在哪里意味着在drawable 文件夹中
    • 是的,它们已经存储在那里
    • @Spritzig 但在哪里
    • 在文件夹中~drawable hdpi~
    【解决方案2】:

    您可以使用this 库将XML 解析为您的POJO 文件。解析可以在您的Fragment中完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      相关资源
      最近更新 更多