我从我的php页面获取图像URL并使用JSON解析,我需要将图像插入列表视图,但我不知道我该怎么做。任何人都可以帮助我吗?我的代码在这里:
// Getting JSON from URL
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json1 = jParser.makeHttpRequest(url_all_products,"GET",params);
Log.d("All Products: ", json1.toString());
try {
products = json1.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String name = c.getString(TAG_NAME);
String url = c.getString(TAG_URL);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_URL, url);
map.put(TAG_NAME, name);
productsList.add(map);
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.items, new String[] {
TAG_NAME},
new int[] { R.id.name });
ListView list = (ListView) getView().findViewById(R.id.list);
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
最好使用Lazyloading
您可以使用thest1/LazyList
或高级图像缓存Android-Universal-Image-Loader From here
Android-Universal-Image-Loader包含不同的示例,用于列出从url到列表视图的图像。
编辑后
您设置适配器的主要功能
在您的代码中设置并初始化 for循环中的列表视图,这是错误的,您需要使用自定义适配器使用延迟加载列出图像。下面的代码是您的代码后我编辑了部分内容。
// Getting JSON from URL
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json1 = jParser.makeHttpRequest(url_all_products,"GET",params);
Log.d("All Products: ", json1.toString());
try {
products = json1.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String name = c.getString(TAG_NAME);
String url = c.getString(TAG_URL);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_URL, url);
map.put(TAG_NAME, name);
productsList.add(map);
}
//DON'T SET ADAPTER IN A LOOP
MyListViewAdaptor adapter = new MyListViewAdaptor(
getActivity(), productsList,
);
ListView list = (ListView) getView().findViewById(R.id.list);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
编写一个custum适配器,将您的图像显示到SimpleAdapter以外的列表视图,并使用adpator中延迟加载中使用的函数并显示图像。让我们创建一个适配器MyListViewAdaptor。示例如下所示
<强> MyListViewAdaptor.java 强>
public class MyListViewAdaptor extends BaseAdapter {
// Declaring Variables
private Activity activity;
private ArrayList<HashMap<String, String>> myarray;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
// PLAESE READ ImageLoader IS A CLASS FROM LIB thest1/LazyList LAZYLOADING
public MyListViewAdaptor(Activity a, ArrayList<HashMap<String, String>> myarray) {
activity = a;
this.myarray = myarray;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity);
}
public int getCount() {
return myarray.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
// Get the view from mainpage_row.xml
vi = inflater.inflate(R.layout.yourlistviewrowname, null);
ImageView userImage = (ImageView) vi
.findViewById(R.id.youtimageviewname);
TextView textName = (TextView) vi.findViewById(R.id.yourtextviewname);
HashMap<String, String> datamap = new HashMap<String, String>();
datamap = myarray.get(position);
textName.setText(datamap.get("yourTextKeyHere"));
imageLoader.DisplayImage(datamap.get("yourImageKeyHere"), userImage);
return vi;
}
}
不要忘记添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
希望这会对你有所帮助
答案 1 :(得分:1)
这是适配器类
class ItemAdapter extends BaseAdapter {
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private class ViewHolder {
public TextView text;
public ImageView image;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText("Item " + (position + 1));
imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);
return view;
}
}
这是主要的活动
public class ImageListActivity extends AbsListViewBaseActivity {
DisplayImageOptions options;
String[] imageUrls;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_list);
Bundle bundle = getIntent().getExtras();
imageUrls = bundle.getStringArray(Extra.IMAGES);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
listView = (ListView) findViewById(android.R.id.list);
((ListView) listView).setAdapter(new ItemAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
}
}
**它是来自通用图像加载器的官方网站的示例