不插入pojo的值

时间:2018-01-30 15:56:03

标签: android android-fragments pojo

我正在从事音乐应用。我从网址获取歌曲数据,我正在使用 Json解析。并设置和显示我的数据。

所以我有pojo课我从网址获取数据,但数据没有在pojo中设置我不知道我错过了什么。

谢谢! 这是我的pojo课程

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

这是我的java类

@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);

        }
    }


}

最后我的适配器显示数据(变为空)。

 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);
    }
}



}

1 个答案:

答案 0 :(得分:1)

我发送pojo为另一个没有得到结果的类我在获得结果后在postExcute上设置pojo。

 @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);

            }
        }


    }