mardi 1 octobre 2019

i have tired with volly in android studio

i just tried with the volly by android studio... i have make a json file, and then i'm implement everything, but it's not working.. i'm not understanding what is the problem. below i have shared the codes.

here is the app screenshot see the screenshot

Database.java

package net.gurujibd.ajkerkhobor;

public class Database {

    private String name;
    private String webview;
    private String id;
    private String image;

    public Database() {
    }

    public Database(String name, String webview, String id, String image) {
        this.name = name;
        webview = webview;
        this.id = id;
        this.image = image;
    }


    public String getName() {
        return name;
    }

    public String getWebview() {
        return webview;
    }

    public String getId() {
        return id;
    }


    public String getImage() {
        return image;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setWebview(String webview) {
        webview = webview;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

RecyclerViewAdapter.java

package net.gurujibd.ajkerkhobor;

import android.content.Context;
import android.content.Intent;
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.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext ;
    private List<Database> mData ;
    RequestOptions option;


    public RecyclerViewAdapter(Context mContext, List<Database> mData) {
        this.mContext = mContext;
        this.mData = mData;

        // Request option for Glide
        option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view ;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view = inflater.inflate(R.layout.anime_row_item,parent,false) ;
        final MyViewHolder viewHolder = new MyViewHolder(view) ;
        viewHolder.view_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(mContext, MainActivity.class);
                i.putExtra("name",mData.get(viewHolder.getAdapterPosition()).getName());
                i.putExtra("webview",mData.get(viewHolder.getAdapterPosition()).getWebview());
                i.putExtra("id",mData.get(viewHolder.getAdapterPosition()).getId());
                i.putExtra("image",mData.get(viewHolder.getAdapterPosition()).getImage());

                mContext.startActivity(i);

            }
        });




        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_rating.setText(mData.get(position).getId());

        // Load Image from the internet and set it into Imageview using Glide

        Glide.with(mContext).load(mData.get(position).getImage()).apply(option).into(holder.img_thumbnail);



    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name ;
        TextView tv_rating ;
        TextView tv_studio ;
        TextView tv_category;
        ImageView img_thumbnail;
        LinearLayout view_container;





        public MyViewHolder(View itemView) {
            super(itemView);

            view_container = itemView.findViewById(R.id.container);
            tv_name = itemView.findViewById(R.id.anime_name);
            tv_category = itemView.findViewById(R.id.categorie);
            tv_rating = itemView.findViewById(R.id.rating);
            tv_studio = itemView.findViewById(R.id.studio);
            img_thumbnail = itemView.findViewById(R.id.thumbnail);

        }
    }

}

Home.java (it's a fragment)

package net.gurujibd.ajkerkhobor.fragments;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import net.gurujibd.ajkerkhobor.Database;
import net.gurujibd.ajkerkhobor.MainActivity;
import net.gurujibd.ajkerkhobor.R;
import net.gurujibd.ajkerkhobor.RecyclerViewAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Home extends Fragment implements View.OnClickListener {

    private final String JSON_URL = "http://dev.gurujibd.net/final/json.php?id=0" ;
    private JsonArrayRequest request ;
    private RequestQueue requestQueue ;
    private List<Database> lstAnime ;
    private RecyclerView recyclerView ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        lstAnime = new ArrayList<>() ;
        recyclerView = root.findViewById(R.id.recyclerviewid);
        jsonrequest();

        return root;
    }

    private void jsonrequest() {

        request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject  = null ;

                for (int i = 0 ; i < response.length(); i++ ) {


                    try {
                        jsonObject = response.getJSONObject(i) ;
                        Database anime = new Database() ;
                        anime.setName(jsonObject.getString("name"));
                        anime.setWebview(jsonObject.getString("webview"));
                        anime.setId(jsonObject.getString("id"));
                        anime.setImage(jsonObject.getString("image"));
                        lstAnime.add(anime);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

                setuprecyclerview(lstAnime);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(request) ;


    }

    private void setuprecyclerview(List<Database> lstAnime) {


        RecyclerViewAdapter myadapter = new RecyclerViewAdapter(getContext(),lstAnime) ;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(myadapter);

    }

    @Override
    public void onClick(View view) {

    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);

    }

}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBG"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".fragments.Home"
        android:padding="10dp"
        android:orientation="vertical">

        <!-- TODO: Update blank fragment layout -->

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:id="@+id/recyclerviewid">

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

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

anime_row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginTop="5dp"
    android:padding="8dp"
    android:background="@drawable/bgg"
    android:id="@+id/container">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:background="@drawable/loading_shape"
        android:id="@+id/thumbnail"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="vertical"
        android:layout_margin="8dp">


        <TextView
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/anime_name"
            android:text="Anime Title"
            android:textStyle="bold"
            android:textSize="18sp"/>

        <TextView
            android:textColor="#fff"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Category"
            android:id="@+id/categorie"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rating"
            android:text="0.0"
            android:layout_marginTop="10dp"
            android:background="@drawable/rating_background"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold"
            android:drawableLeft="@drawable/ic_star_black_24dp"
            android:paddingRight="5dp"/>

        <TextView
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Studio"
            android:layout_marginTop="5dp"
            android:id="@+id/studio"/>



    </LinearLayout>


</LinearLayout>

can you please explain me what's the problem and how can solve this issue?




Aucun commentaire:

Enregistrer un commentaire