gpt4 book ai didi

java - ListView 在自定义适配器中重复项目

转载 作者:行者123 更新时间:2023-12-01 11:13:14 25 4
gpt4 key购买 nike

我正在学习 android,我的代码运行良好。它从网站获取数据并将其显示在列表中。但是当 subcat==1 时,它有 9 个项目需要获取。它显示 7 个不同的项目并重复两个项目。首先它只显示 4 个,即 NullPointerException 第 5 个图像没有名称,因此它重复 4 个图像直到 9 次,但现在一切都很好,它仍然只显示 7 个图像,然后重复 2 个图像。这是我的代码:

public class JSONAsyncTask extends AsyncTask<String, Void, Void> {
ProgressDialog pd;
Context context;


JSONAsyncTask(Context context) {
this.context = context;
}

@Override
protected void onPreExecute() {

super.onPreExecute();
pd = new ProgressDialog(context);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
pd.setMessage("Please wait..\nLoading data");
pd.show();
}

@Override
protected Void doInBackground(String... strings) {
ArrayList<item> jsonArrayList = new ArrayList<item>();
try {
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://avdeal.in/get_all_products.php");
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
if (jsonObject != null) {
String posts = jsonObject.getString("products");
JSONArray postsArray = new JSONArray(posts);
if (postsArray != null) {
if (postsArray.length() > 0) {
for (int i = 0; i < postsArray.length(); i++) {
JSONObject postsObject = postsArray
.getJSONObject(i);
int subcat = postsObject.getInt("subcat_id");

if (subcats == subcat) {
int id=postsObject.getInt("id");
String title = postsObject.getString("product_title");
String price = postsObject.getString("product_price");
String url = "http://avdeal.in/seller/upload_images/" + postsObject.getString("product_url");


list1.add(new item(id, title, price, url));
}

}
}


}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}

@Override
protected void onPostExecute(Void result) {

super.onPostExecute(result);
if (list1 != null) {
pd.dismiss();
adp = new actorAdapter(context, R.layout.listview, list1);
lv.setAdapter(adp);
adp.notifyDataSetChanged();
}
else {
pd.setMessage("No internet access");
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(itemView.this, "Please Wait " + i + 1, Toast.LENGTH_SHORT).show();
int id1 = list1.get(i).getId();
String name = list1.get(i).getName();
String price = list1.get(i).getPrice();
Bitmap bitmap = list1.get(i).getImage();

if (bitmap == null) {
Intent intent = new Intent(context, productDetail.class);
intent.putExtra("name", name);
intent.putExtra("price", price);
intent.putExtra("id", id1);
intent.putExtra("imagena", 1);
startActivity(intent);
} else {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(context, productDetail.class);
intent.putExtra("name", name);
intent.putExtra("price", price);
intent.putExtra("image", byteArray);
intent.putExtra("id", id1);
startActivity(intent);
}
}
});
}
}

private class ImageLoadTask extends AsyncTask<String, String, Bitmap>{
Context context;
ImageLoadTask(Context context) {
this.context = context;
}
@Override
protected Bitmap doInBackground(String... strings) {
for(int i = 0 ; i < list1.size() ; i++)
{
if (list1.get(i).getImage()==null);
{
String url=list1.get(i).getUrl();
InputStream is = null;
try {
is = (InputStream) new URL(url).getContent();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap b = BitmapFactory.decodeStream(is);
list1.set(i, new item(list1.get(i).getId(), list1.get(i).getName(), list1.get(i).getPrice(), list1.get(i).getUrl() ,b));
}
}
return null;

}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);

}
protected void onPostExecute(Bitmap result){
super.onPostExecute(result);
adp = new actorAdapter(context, R.layout.listview, list1);
lv.setAdapter(adp);
adp.notifyDataSetChanged();
}
}

我可以做些什么来延迟加载图像吗?我尝试创建另一个 AsyncTask,但它在加载所有图像时显示图像。

我可以在下载一张图像后立即更新我的 ListView 吗?

请帮忙。我自己无法弄清楚。这两个问题阻碍了我了解更多。

public View getView(int position, View convertView, ViewGroup parent){
inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v =convertView;
if (v == null) {

holder=new ViewHolder();
v=inflater.inflate(Resource,null);
holder.name= (TextView) v.findViewById(R.id.titles);
holder.price= (TextView) v.findViewById(R.id.price);
holder.img= (ImageView) v.findViewById(R.id.itemimage);
v.setTag(holder);
holder.name.setText(list1.get(position).getName());
holder.price.setText((CharSequence) list1.get(position).getPrice());
holder.img.setImageBitmap(list1.get(position).getImage());



}
else {
holder = (ViewHolder) v.getTag();
}


return v;
}

最佳答案

对于延迟图像加载,您需要为每个图像创建一个AsyncTask,而不是为所有图像创建一个AsyncTask(dwonload它们在单独的异步任务)。

我建议您使用这些库之一:

这是 4 个库之间基于意见的比较表(来自 here ),它可能会帮助您选择:

编辑:

对于重复的项目,请更改 getView 方法,如下所示:

public View getView(int position, View convertView, ViewGroup parent){
inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v =convertView;
if (v == null) {

holder=new ViewHolder();
v=inflater.inflate(Resource,null);
holder.name= (TextView) v.findViewById(R.id.titles);
holder.price= (TextView) v.findViewById(R.id.price);
holder.img= (ImageView) v.findViewById(R.id.itemimage);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}

holder.name.setText(list1.get(position).getName());
holder.price.setText((CharSequence) list1.get(position).getPrice());
holder.img.setImageBitmap(list1.get(position).getImage());

return v;
}

您在使用回收 View 时忘记更新项目数据 (v != null)。

关于java - ListView 在自定义适配器中重复项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32129632/

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