我正在使用PHP和Mysql创建一个应用程序,它在选项卡片段中显示带有图像、标题、描述和价格的自定义列表视图。
为此,我创建了一个ProductTabs类,其中包含ItemList AsyncTask内部类,除图像下载外,该类用于获取项目详细信息。
ProductTabs类:
public class ProductTabs extends Fragment {
HttpHandler httpHandler = new HttpHandler();
JSONArray items = null;
ArrayList<HashMap<String,String>> list;
ProgressDialog progressDialog = null;
private static String url_products = "";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static String JSON_PRODUCT_ID = "product_id";
private static String JSON_PRODUCT_NAME = "product_name";
private static String JSON_PRODUCT_PRICE = "product_price";
private static String JSON_PRODUCT_DESC = "product_desc";
private static String JSON_PRODUCT_URL = "url";
private static String JSON_PRODUCT_IMAGE = "product_image";
ListView productList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.product_tabs, container, false);
productList = (ListView) v.findViewById(R.id.productList);
if(this.getTag() == "Popular") {
url_products = "http://kamalevent.com/cakeshop/products.php";
} else {
url_products = "http://kamalevent.com/cakeshop/products.php?sort_by=product_price";
}
list = new ArrayList<HashMap<String, String>>();
new ItemList().execute();
return v;
}
class ItemList extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Fetching Cakelist...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... args) {
String json = httpHandler.makeHttpRequest(url_products, "GET");
Log.d("Response :", json);
if(json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
items = jsonObject.getJSONArray(TAG_PRODUCTS);
for(int i=0; i < items.length(); i++) {
JSONObject product = items.getJSONObject(i);
String id = product.getString(JSON_PRODUCT_ID);
String name = product.getString(JSON_PRODUCT_NAME);
String price = product.getString(JSON_PRODUCT_PRICE);
String desc = product.getString(JSON_PRODUCT_DESC);
String url = product.getString(JSON_PRODUCT_URL);
HashMap<String,String> products = new HashMap<String,String>();
products.put(JSON_PRODUCT_ID, id);
products.put(JSON_PRODUCT_NAME, name);
products.put(JSON_PRODUCT_DESC, desc);
products.put(JSON_PRODUCT_PRICE, price);
products.put(JSON_PRODUCT_URL, url);
list.add(products);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
ProductAdapter productAdapter = new ProductAdapter(getActivity(), R.layout.item_list, list);
productList.setAdapter(productAdapter);
productAdapter.notifyDataSetChanged();
}
}
}下面是用于创建自定义列表的自定义ProductAdapter代码,在这个适配器中,我创建了一个新的内部GetImage AsyncTask类,用于从url下载图像。
ProductAdapter类:
public class ProductAdapter extends ArrayAdapter<HashMap<String, String>> {
private ArrayList<HashMap<String,String>> products;
private Context context;
private View view;
private int resource;
private HashMap<String,String> hashMap;
public ProductAdapter(Context context, int resource, ArrayList<HashMap<String, String>> products) {
super(context, resource, products);
this.context = context;
this.resource = resource;
this.products = products;
}
public class ValueHolder {
TextView name, desc, price;
ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(resource, parent, false);
ValueHolder valueHolder = new ValueHolder();
valueHolder.name = (TextView) view.findViewById(R.id.item_title);
valueHolder.desc = (TextView) view.findViewById(R.id.item_desc);
valueHolder.price = (TextView) view.findViewById(R.id.item_price);
valueHolder.image = (ImageView) view.findViewById(R.id.itemImage);
hashMap = products.get(position);
valueHolder.name.setText(hashMap.get("product_name"));
valueHolder.desc.setText(hashMap.get("product_desc"));
valueHolder.price.setText("Rs. " + hashMap.get("product_price"));
if(valueHolder.image != null) {
new GetImage(valueHolder.image).execute(hashMap.get("url"));
}
return view;
}
class GetImage extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewWeakReference;
public GetImage(ImageView imageView) {
imageViewWeakReference = new WeakReference<ImageView>(imageView);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... args) {
URL url = null;
Bitmap image = null;
String urlToImage = args[0];
try {
Log.d("Image :", urlToImage);
url = new URL(urlToImage);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(imageViewWeakReference != null) {
ImageView imageView = imageViewWeakReference.get();
if(imageView != null) {
if(bitmap != null) {
Log.d("Image :", "Image found");
imageView.setImageBitmap(bitmap);
}
}
}
}
}
}现在,当我运行这个应用程序时,自定义适配器成功地显示了项目列表,过了一段时间,它显示了3到4个下载的图像,而当我滚动列表视图时,上面下载的图像也消失了,在logcat GetImage AsyncTask中,它像无限进程一样继续运行。
因此,如果有人能给我提出更好的解决方案,或者告诉我我做错了什么,这将对我很有帮助。
提前谢谢。
发布于 2016-07-09 08:50:43
请使用images而不是异步任务从url加载图像。
显示下面的链接:https://github.com/nostra13/Android-Universal-Image-Loader
https://stackoverflow.com/questions/38279963
复制相似问题