我正在尝试使用 ArrayAdapter 获取 JSON 数据并将其显示在我的 ListView 中。无论如何,在我的适配器中,我正在使用来自https://github.com/nostra13/Android-Universal-Image-Loader的通用图像加载器,并且除了图像之外的其他数据都已正确加载。一些图像显示正确,一些不应该在特定项目上,还有一些没有完全加载。
这是我的调用片段:
public class SampleFragment extends Fragment implements
OnItemClickListener {
private ListView songsListView;
private ArrayList<String> list;
private ArrayAdapter<String> dataAdapter;
private View rootView;
private Button selectReferenceSong;
private Fragment memberFragment;
private String[] songsArray;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<ReferenceTracks> arrayOfList;
private String bufferString;
private static final String rssFeed = "https://dl.dropboxusercontent.com/u/32769459/reference_tracks.json";
private static final String TAG_MUSIC = "music";
private static final String TAG_SONG = "song";
private static final String TAG_TITLE = "title";
private static final String TAG_ARTIST = "artist";
private static final String TAG_DURATION = "duration";
private static final String TAG_THUMBURL = "thumb_url";
public SampleFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.sample_layout, container, false);
songsListView = (ListView) rootView.findViewById(R.id.songsListView);
songsListView.setOnItemClickListener(this);
arrayOfList = new ArrayList<ReferenceTracks>();
if (Utility.isNetworkAvailable(getActivity())) {
new AsynchronousTask().execute(rssFeed);
} else {
Toast.makeText(getActivity().getApplicationContext(),
"No Network Connection!", Toast.LENGTH_SHORT).show();
}
return rootView;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
/**
*
* @Asynchronous class
*/
public class AsynchronousTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
private RowAdapter objRowAdapter;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
return Utility.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (null == result || result.length() == 0) {
Toast.makeText(getActivity().getApplicationContext(),
"No data found from web.", Toast.LENGTH_SHORT).show();
} else {
try {
JSONObject topObject = new JSONObject(result);
JSONObject musicObject = topObject.getJSONObject(TAG_MUSIC);
JSONArray jsonArray = musicObject.getJSONArray(TAG_SONG);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objJson = jsonArray.getJSONObject(i);
ReferenceTracks objItem = new ReferenceTracks();
objItem.setTitle(objJson.getString(TAG_TITLE));
objItem.setArtist(objJson.getString(TAG_ARTIST));
objItem.setDuration(objJson.getString(TAG_DURATION));
objItem.setThumbUrl(objJson.getString(TAG_THUMBURL));
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
setAdapterToListview();
}
}
public void setAdapterToListview() {
objRowAdapter = new RowAdapter(getActivity(), R.layout.reference_song_list_item, arrayOfList);
songsListView.setAdapter(objRowAdapter);
}
}}
这是我的自定义适配器:
public class RowAdapter extends ArrayAdapter<ReferenceTracks> {
private Context activity;
private ArrayList<ReferenceTracks> items;
private ReferenceTracks objBean;
private int row;
private ImageLoader imageLoader;
private DisplayImageOptions options;
public RowAdapter(Context act, int resource, ArrayList<ReferenceTracks> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
imageLoader = ImageLoader.getInstance();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tvImage = (ImageView) view.findViewById(R.id.list_image);
holder.tvTitle = (TextView) view.findViewById(R.id.title);
holder.tvArtist = (TextView) view.findViewById(R.id.artist);
holder.tvDuration = (TextView) view.findViewById(R.id.duration);
holder.pbar = (ProgressBar) view.findViewById(R.id.progressbar);
if (holder.tvImage != null) {
if (null != objBean.getThumbUrl()
&& objBean.getThumbUrl().trim().length() > 0) {
final ProgressBar pbar = holder.pbar;
imageLoader.destroy();
imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
imageLoader.displayImage(objBean.getThumbUrl(), holder.tvImage,
options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri,
View view) {
// TODO Auto-generated method stub
pbar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri,
View view, FailReason failReason) {
// TODO Auto-generated method stub
pbar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
// TODO Auto-generated method stub
pbar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingCancelled(String imageUri,
View view) {
// TODO Auto-generated method stub
}
});
} else {
holder.tvImage.setImageResource(R.drawable.ic_launcher);
}
}
if (holder.tvTitle != null && null != objBean.getTitle()
&& objBean.getTitle().trim().length() > 0) {
holder.tvTitle.setText(Html.fromHtml(objBean.getTitle()));
}
if (holder.tvArtist != null && null != objBean.getArtist()
&& objBean.getArtist().trim().length() > 0) {
holder.tvArtist.setText(Html.fromHtml(objBean.getArtist()));
}
if (holder.tvDuration != null && null != objBean.getDuration()
&& objBean.getDuration().trim().length() > 0) {
holder.tvDuration.setText(Html.fromHtml(objBean.getDuration()));
}
return view;
}
public class ViewHolder {
public ImageView tvImage;
public ProgressBar pbar;
public TextView tvTitle, tvArtist, tvDuration;
}}
虽然这是我的应用程序类:
public class SampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
DisplayImageOptions displayimageOptions = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageOnFail(R.drawable.ic_error)
.showImageForEmptyUri(R.drawable.ic_empty)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.build();
File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPoolSize(4)
.threadPriority(Thread.NORM_PRIORITY)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.defaultDisplayImageOptions(displayimageOptions)
.discCacheSize(50 * 1024 * 1024)
.discCacheFileCount(1000000)
.memoryCache(new WeakMemoryCache())
.build();
ImageLoader.getInstance().init(config);
}}
这也是日志:
06-24 11:18:12.935: E/ImageLoader(6428): null
06-24 11:18:12.935: E/ImageLoader(6428): java.io.EOFException
06-24 11:18:12.935: E/ImageLoader(6428): at libcore.io.Streams.readAsciiLine(Streams.java:203)
06-24 11:18:12.935: E/ImageLoader(6428): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:561)
06-24 11:18:12.935: E/ImageLoader(6428): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:801)
06-24 11:18:12.935: E/ImageLoader(6428): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
06-24 11:18:12.935: E/ImageLoader(6428): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:113)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:84)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:84)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:290)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:250)
06-24 11:18:12.935: E/ImageLoader(6428): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:131)
06-24 11:18:12.935: E/ImageLoader(6428): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-24 11:18:12.935: E/ImageLoader(6428): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-24 11:18:12.935: E/ImageLoader(6428): at java.lang.Thread.run(Thread.java:856)
我在图像加载器的应用程序类上是否有任何设置错误或遗漏?我一直在寻找同样的例外,但似乎没有什么能接近修复。任何帮助都可以。非常感谢!