gpt4 book ai didi

java - 如何在android中的自定义列表适配器中显示来自服务器的带有标题和描述的图像

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:56 26 4
gpt4 key购买 nike

我正在使用 PHP 和 Mysql 创建一个应用程序,该应用程序在选项卡 fragment 中显示带有图像、标题、描述和价格的自定义 ListView 。

为了实现这一目标,我创建了一个带有 ItemList AsyncTask 内部类的 ProductTabs 类,该类用于获取除图像下载之外的项目详细信息。

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 个下载的图像而不是整个图像,当我滚动 ListView 时,上面下载的图像也消失了,并且在 logcat GetImage AsyncTask 中像无限进程一样继续运行。

因此,如果有人可以建议我更好的解决方案,或者可以告诉我我在这里做错了什么,那将对我非常有帮助。

提前致谢。

最佳答案

请使用 Android-Universal-Image-Loader 而不是 asynctasks 从 url 加载图像。

显示以下链接: https://github.com/nostra13/Android-Universal-Image-Loader

关于java - 如何在android中的自定义列表适配器中显示来自服务器的带有标题和描述的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com