我有本地JSON和Web服务器。当我从Web服务器获取数据时,它可以在ListView中设置项目。但是当我从本地JSON获取数据时,它不起作用而不在ListView中设置项目。
这是从Web服务器获取的代码,它起作用:
public void work2(String url) {
pDialog = new ProgressDialog(this);
String dowspee = getResources().getString(R.string.dowspee);
String load = getResources().getString(R.string.Loadingd);
pDialog.setTitle(load);
pDialog.setMessage(dowspee);
pDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.seturl(obj.getString("url"));
movie.setRating(((Number) obj.get("rating")).doubleValue());
movie.setYear(obj.getInt("releaseYear"));
movie.setStat(obj.getString("State"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
JSONArray epArry = obj.getJSONArray("eps");
ArrayList<String> ep = new ArrayList<String>();
for (int j = 0; j < epArry.length(); j++) {
ep.add((String) epArry.get(j));
}
movie.setep(ep);
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
movieList.clear();
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
}
但是当我尝试从txt文件获取并放入ListView时,它不会在ListView中显示任何内容:
public void workhistory() {
Movie movie = new Movie();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.seturl(obj.getString("url"));
movie.setRating(((Number) obj.get("rating")).doubleValue());
movie.setYear(obj.getInt("releaseYear"));
movie.setStat(obj.getString("State"));
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
JSONArray epArry = obj.getJSONArray("eps");
ArrayList<String> ep = new ArrayList<String>();
for (int j = 0; j < epArry.length(); j++) {
ep.add((String) epArry.get(j));
}
movie.setep(ep);
movieList.add(movie);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
movieList.clear();
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = new FileInputStream("/storage/emulated/0/data/1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
System.out.println(json);
return json;
}
这是JSON:
{
"genre": [
"Music",
"Sci-Fi"
],
"eps": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13"
],
"title": "AKB0048 First Stage",
"State": "Completed",
"image": ".jpg",
"rating": 7.68,
"url": "http:\/\/",
"releaseYear": 2012
}
答案 0 :(得分:0)
我找到了解决方案:
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
我应该把它们放在:
之后 adapter.notifyDataSetChanged();
感谢。