gpt4 book ai didi

android - 不在 pojo 中插入值

转载 作者:行者123 更新时间:2023-11-30 00:08:42 25 4
gpt4 key购买 nike

我正在开发音乐应用程序。我正在从 url 获取歌曲数据,并且正在使用 Json 解析。并设置和显示我的数据。

所以我有 pojo 类,我从 url 获取数据,但该数据未在 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);
}
}



}

最佳答案

我将空的 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);

}
}


}

关于android - 不在 pojo 中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48525578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com