gpt4 book ai didi

android - JsonAdapter 无法打印数据

转载 作者:太空狗 更新时间:2023-10-29 14:01:11 25 4
gpt4 key购买 nike

我关注这个video尝试从 mongodb 获取数据 从 mongodb 获取 JsonArray,并使用 Debug模式检查 JsonTest.java 类:ArrayList<JsonSongs> songlist;里面有 21 条数据,而且都是正确的,listview 显示了 21 行,但不知何故数据没有显示在 textview 上,

我认为 JsonTest.java 类: ˋArrayList 歌曲列表; did pass the data to class JsonSongAdapter.java ˋArrayList<JsonSongs> songlist;正确的?请帮忙,卡在这里好几个小时了

JsonTest.java

public class JsonTest extends Activity {

ListView list;
ArrayList<JsonSongs> songlist;
private TextView test;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jsonmain);
list = (ListView)findViewById(R.id.JsonlistView);
songlist = new ArrayList<JsonSongs>();
new JsonTalk().execute("http://192.168.1.102:3000/songs");

}





class JsonTalk extends AsyncTask< String , String ,String>{

@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
// JSONObject parentJsonObject = new JSONObject(finalJson);
JSONArray jsonArray = new JSONArray(finalJson);
JsonSongs jsonSongs = new JsonSongs();
for (int i = 0 ; i < jsonArray.length();i++){
JSONObject finaObject = jsonArray.getJSONObject(i);
jsonSongs.set_id(finaObject.getString("_id"));
jsonSongs.setDecade(finaObject.getString("decade"));
jsonSongs.setArtist(finaObject.getString("artist"));
jsonSongs.setSong(finaObject.getString("song"));
jsonSongs.setWeeksAtOne(finaObject.getInt("weeksAtOne"));
songlist.add(jsonSongs);
}

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

finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result == null){
JsonSonAdapter adapter = new JsonSonAdapter(getApplicationContext(),R.layout.jsonlayout,songlist);
list.setAdapter(adapter);
}else {
Toast.makeText(JsonTest.this,"NOP",Toast.LENGTH_LONG).show();
}
}
}
}

JsonSonAdapter

public class JsonSonAdapter extends ArrayAdapter<JsonSongs> {
ArrayList<JsonSongs> songList;
int Resource;
Context context;
LayoutInflater vi;

public JsonSonAdapter(Context context, int resource, ArrayList<JsonSongs> objects) {
super(context, resource, objects);
songList = objects;
Resource = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
if(convertView == null){
convertView = vi.inflate(Resource,null);
holder = new ViewHolder();
holder.textJson_ID=(TextView)convertView.findViewById(R.id.textJson_ID);
holder.textJSONname=(TextView)convertView.findViewById(R.id.textJSONname);
holder.textJsonSong=(TextView)convertView.findViewById(R.id.textJsonSong);
holder.textJsonYear=(TextView)convertView.findViewById(R.id.textJsonYear);
holder.textJsonWeeks=(TextView)convertView.findViewById(R.id.textJsonWeeks);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}

return convertView;
}

static class ViewHolder{

public TextView textJsonYear;
public TextView textJSONname;
public TextView textJsonSong;
public TextView textJsonWeeks;
public TextView textJson_ID;
}

}

JsonSongs

public class JsonSongs {

private String _id;
private String decade;
private String artist;
private String song;
private int weeksAtOne;

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

public String getArtist() {
return artist;
}

public void setArtist(String artist) {
this.artist = artist;
}

public String getDecade() {
return decade;
}

public void setDecade(String decade) {
this.decade = decade;
}

public String getSong() {
return song;
}

public void setSong(String song) {
this.song = song;
}

public int getWeeksAtOne() {
return weeksAtOne;
}

public void setWeeksAtOne(int weeksAtOne) {
this.weeksAtOne = weeksAtOne;
}




}

最佳答案

but somehow the data did't show on the textview ,

您没有设置它,所以不足为奇。完成 convertView 后,您需要在 EditText 上调用 setText()(在 getView() 中)以用数据填充它。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
if(convertView == null){
convertView = vi.inflate(Resource,null);
holder = new ViewHolder();
holder.textJson_ID=(TextView)convertView.findViewById(R.id.textJson_ID);
...
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}


// populate it with data
JsonSongs song = songs.get(position);
holder.textJson_ID.setText(song.getSomething());
...


return convertView;
}

PS:JsonSongs 应该命名为JsonSong。你的命名模式一团糟。

关于android - JsonAdapter 无法打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35480145/

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