Monday, 3 December 2018

How to parse an xml to java class with recycler view or in adapter of recyclerview

I have an xml file which name it is Bookmark and it is into the xml folder. I want to parse xml to recycler view which I can show the list of the Bookmark in recycler view. This is my code.

Bookmark.xml in the xml folder

<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>

This is the java class for recycler view

public class FragmentBookmark extends Fragment {
    View paramView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.bookmark, container, false);
        RecyclerView recyclerView =  paramView.findViewById(R.id.listRecyclerView);

        MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 4);
        recyclerView.setLayoutManager(layoutManager);
        return paramView;
    }
}

This is the recycler view 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>

This is the adapter for 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) {

        }
    }
}

This is a java class which I have added manually and works in the recycler view but I need the Bookmark xml to show in recycler view

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

    };
}

This is the xml to show an Image and a TextView for recycler view

<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>



from How to parse an xml to java class with recycler view or in adapter of recyclerview

No comments:

Post a Comment