Search code examples
androidandroid-fragmentspojo

Value not inserting in pojo


i am working on music application. i am getting song data from url and i am using Json parsing. and set and display my data.

so i have pojo class i am getting data from url but that data not set in pojo i don't know what i am missing.

Thanks!! This is my pojo class

    public class OnlinePojo
       {
    String Id,Album,Songname,Artist;
    String SongPath;

    public String getSongPath() {
        return SongPath;
    }

    public void setSongPath(String songPath) {
        SongPath = songPath;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getAlbum() {
        return Album;
    }

    public void setAlbum(String album) {
        Album = album;
    }

    public String getSongname() {
        return Songname;
    }

    public void setSongname(String songname) {
        Songname = songname;
    }

    public String getArtist() {
        return Artist;
    }

    public void setArtist(String artist) {
        Artist = artist;
    }
}

And this is my java class

@SuppressLint("ValidFragment")
    public Onlinesong_Home(Context c) {
        context = c;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.activity_onlinesong_home, container, false);
        song = v.findViewById(R.id.online_song);
        weeklytop = v.findViewById(R.id.online_trainding);
        week = v.findViewById(R.id.online_txtview);


        url = "https://burdened-committee.000webhostapp.com/musicbox/display.php";

        //onlinePojos = new ArrayList<OnlinePojo>();


        onlinehome_adpater = new Onlinehome_Adpater(context, onlinePojos);
        weeklytop.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        new onlinesongdisplay().execute();


        return v;
    }


    class onlinesongdisplay extends AsyncTask<Void, Void, String> {

        json js = new json();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(Void... urls) {


            js = new json();
            String result = js.processdata(url);
            onlinePojos = new ArrayList<>();

            try {
                //geting json object
                JSONObject jo = new JSONObject(result);
                JSONArray ja = jo.getJSONArray("res");

                onlinePojos = new ArrayList<>();
                for (int i = 0; i < ja.length(); i++) {

                    JSONObject j = ja.getJSONObject(i);

                    onlinePojo = new OnlinePojo();

                    onlinePojo.setSongname(j.getString("songname"));
                    onlinePojo.setAlbum(j.getString("albumname"));
                    onlinePojos.add(onlinePojo);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            weeklytop.setAdapter(onlinehome_adpater);

        }
    }


}

And finally that my adapter to show data(getting null).

 public Onlinehome_Adpater(Context c,ArrayList<OnlinePojo> onlinePojos)
{
    context=c;
    OnlinePojos=onlinePojos;
}
@Override
public holder onCreateViewHolder(ViewGroup parent, int viewType) {

    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.activity_onlinehome_adpater, null);

    return new Onlinehome_Adpater.holder(v);
}
@Override
public void onBindViewHolder(holder holder, int position) {

    OnlinePojo onlinePojo=OnlinePojos.get(position);

    holder.songname.setText(onlinePojo.getSongname());
    holder.songimage.setImageResource(R.drawable.splash);
    holder.albumname.setText(onlinePojo.getAlbum());

    Log.wtf("online",""+onlinePojo.getSongname());
}

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

class holder extends RecyclerView.ViewHolder {

    ImageView songimage, dot;
    TextView songname, albumname;

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

        songimage = itemView.findViewById(R.id.online_songimage);
        dot = itemView.findViewById(R.id.overflow);
        songname = itemView.findViewById(R.id.onlinesong);
        albumname = itemView.findViewById(R.id.onlinealbum);
    }
}



}

Solution

  • I send pojo empty to another class that not getting result now i am setting pojo on postExcute after getting result.

     @SuppressLint("ValidFragment")
            public Onlinesong_Home(Context c) {
                context = c;
            }
    
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
                v = inflater.inflate(R.layout.activity_onlinesong_home, container, false);
                song = v.findViewById(R.id.online_song);
                weeklytop = v.findViewById(R.id.online_trainding);
                week = v.findViewById(R.id.online_txtview);
    
    
                url = "https://burdened-committee.000webhostapp.com/musicbox/display.php";
    
                //onlinePojos = new ArrayList<OnlinePojo>();
    
    
                weeklytop.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                new onlinesongdisplay().execute();
    
    
                return v;
            }
    
    
            class onlinesongdisplay extends AsyncTask<Void, Void, String> {
    
                json js = new json();
    
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
    
                }
    
                @Override
                protected String doInBackground(Void... urls) {
    
    
                    js = new json();
                    String result = js.processdata(url);
                    onlinePojos = new ArrayList<>();
    
                    try {
                        //geting json object
                        JSONObject jo = new JSONObject(result);
                        JSONArray ja = jo.getJSONArray("res");
    
                        onlinePojos = new ArrayList<>();
                        for (int i = 0; i < ja.length(); i++) {
    
                            JSONObject j = ja.getJSONObject(i);
    
                            onlinePojo = new OnlinePojo();
    
                            onlinePojo.setSongname(j.getString("songname"));
                            onlinePojo.setAlbum(j.getString("albumname"));
                            onlinePojos.add(onlinePojo);
    
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
    
              onlinehome_adpater = new Onlinehome_Adpater(context, onlinePojos);
    
             weeklytop.setAdapter(onlinehome_adpater);
    
                }
            }
    
    
        }