我正在用Json中的项目填充一个懒惰加载的listview,它只显示第一个项目,没有图像,不显示其余的项目。我使用AsyncTask加载我的项目到我的列表视图。有人能帮我解决这个问题吗?
这是我的LatesAlbums类:public class LatestAlbums extends FragmentActivity {
// widgets
private ListView list;
private ImageView back, clear;
private EditText find;
private static String url = "jsonURL";
// JSON Node names
private static final String TAG_ALBUMS = "albums";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_SINGER = "singer";
private static final String TAG_GENRE = "genre";
private static final String TAG_MIX = "mix";
private static final String TAG_THUMB = "thumb";
private static final String TAG_SONGS = "songs";
private static final String TAG_SONG_TITLE = "song";
private static final String TAG_SONG_ARTIST = "artist";
private static final String TAG_SONG_MP3 = "mp3";
private static final String TAG_SONG_MP4 = "mp4";
private static final String TAG_SONG_THUMB = "thumb";
private ProgressDialog pdialog;
// albums JSONArray
private JSONArray albums;
private JSONArray sngs;
private ArrayList<Albums> albumsArray;
private ArrayList<Songs> songsArray;
private AlbumLazyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.latest_albums);
albumsArray = new ArrayList<Albums>();
songsArray = new ArrayList<Songs>();
try {
loadJSON();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// initWidgets();
}
private void initWidgets() {
list = (ListView) findViewById(R.id.listView1);
back = (ImageView) findViewById(R.id.latest_back_img);
clear = (ImageView) findViewById(R.id.latest_search_clear);
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
find.setText("");
find.clearFocus();
find.clearComposingText();
clear.setVisibility(View.INVISIBLE);
}
});
find = (EditText) findViewById(R.id.latest_search_et);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
adapter = new AlbumLazyAdapter(this, albumsArray, this);
// set list view adapter
list.setAdapter(adapter);
// setting find edit text searching method
find.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
clear.setVisibility(View.VISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < albumsArray.size(); i++) {
if (albumsArray.get(i).getTAG_NAME().toLowerCase()
.indexOf(find.getText().toString().toLowerCase()) != -1) {
Log.v("--", albumsArray.get(i).getTAG_NAME());
} else {
Log.v("--", "no results..");
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
initWidgets();
}
// load json from server via asyncTask
public void loadJSON() throws JSONException {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
initWidgets();
pdialog = new ProgressDialog(LatestAlbums.this);
pdialog.setMessage("Loading ....");
pdialog.show();
}
@Override
protected Void doInBackground(Void... params) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject theJSON;
try {
// Getting Array of albums
theJSON = json.getJSONObject("json");
albums = theJSON.getJSONArray(TAG_ALBUMS);
// looping through All albums
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
String album_id = c.getString(TAG_ID);
String album_name = c.getString(TAG_NAME);
String album_singer = c.getString(TAG_SINGER);
String album_mix = c.getString(TAG_MIX);
String album_genre = c.getString(TAG_GENRE);
String album_thumb = c.getString(TAG_THUMB);
albumsArray.add(new Albums(album_id, album_name,
album_singer, album_genre, album_mix,
album_thumb, songsArray));
sngs = c.getJSONArray(TAG_SONGS);
// getting songs for the album
if (!sngs.isNull(0)) {
for (int j = 0; j < sngs.length(); j++) {
JSONObject songs = sngs.getJSONObject(j);
String song_artist = songs
.getString("artist");
String song_mp3 = songs.getString(TAG_SONG_MP3);
String song_mp4 = songs.getString(TAG_SONG_MP4);
String song_thumb = songs
.getString(TAG_SONG_THUMB);
String song_title = songs
.getString(TAG_SONG_TITLE);
songsArray.add(new Songs(song_title,
song_artist, song_mp3, song_mp4,
song_thumb));
}
}
albumsArray.get(i).setSongs(songsArray);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long arg3) {
Intent i = new Intent(getBaseContext(),
AlbumDetails.class);
i.putExtra("songs", albumsArray.get(position)
.getSongs());
startActivity(i);
}
});
// setting the slider
ViewPager pager = (ViewPager) findViewById(R.id.my_slider);
FragmentManager fm = getSupportFragmentManager();
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(
fm, albumsArray);
pager.setAdapter(pagerAdapter);
pdialog.dismiss();
adapter.notifyDataSetChanged();
}
};
task.execute();
}
}
和我的Json输出:
{
"json":{
"albums":[{"id":"1","name":"Best Sumer colections","singer":"Dj Tiresto","genre":"Trance","mix":"yes","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
},
{"id":"2","name":"Best Sumer colections","singer":"Dj Tiresto","genre":"Trance","mix":"yes","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
},
{"id":"3","name":"O viatza","singer":"Maria Bieshu","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
},
{"id":"4","name":"De petrecere","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
},
{"id":"5","name":"De petrecere","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
},
{"id":"6","name":"De petrecere 2013","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
[
{"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
{"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
]
}
]
}
}
LogCat输出:
04-02 13:35:12.109: W/System.err(12798): org.json.JSONException: Value at 6 is null.
04-02 13:35:12.119: W/System.err(12798): at org.json.JSONArray.get(JSONArray.java:259)
04-02 13:35:12.119: W/System.err(12798): at org.json.JSONArray.getJSONObject(JSONArray.java:480)
04-02 13:35:12.119: W/System.err(12798): at com.outsourcefarm.musicagratis.activities.LatestAlbums$4.doInBackground(LatestAlbums.java:188)
04-02 13:35:12.119: W/System.err(12798): at com.outsourcefarm.musicagratis.activities.LatestAlbums$4.doInBackground(LatestAlbums.java:1)
04-02 13:35:12.119: W/System.err(12798): at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-02 13:35:12.119: W/System.err(12798): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-02 13:35:12.119: W/System.err(12798): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-02 13:35:12.129: W/System.err(12798): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-02 13:35:12.129: W/System.err(12798): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-02 13:35:12.129: W/System.err(12798): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-02 13:35:12.129: W/System.err(12798): at java.lang.Thread.run(Thread.java:856)
第188行是JSONObject songs = sngs.getJSONObject(j);
似乎一切都很好,对于那些将有类似的问题,但要确保在json类,或更多的解释看看这个问题
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
的设置如下:
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);