gpt4 book ai didi

android - 将两个适配器设置到同一个列表

转载 作者:行者123 更新时间:2023-11-30 00:41:25 25 4
gpt4 key购买 nike

我的应用程序从服务器下载文本和图像并将它们显示在列表中我使用两个单独的适配器,一个用于文本,另一个用于图像。我不能将它们放在同一个适配器中,也不能将两个适配器放在同一个列表中。那么如何连接两个具有相同列表的适配器这是我的第一个适配器的一些代码

private class LoadService extends AsyncTask<String, Void, Void> {

private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private final String TAG = null;
private ProgressDialog Dialog = new ProgressDialog(mainpage.this);

ImageView imagee = (ImageView) findViewById(R.id.image);
String url = "images url";
String[] img = new String[1000];
// lv.setAdapter(new ImageLoader(, img));


protected void onPreExecute() {

Dialog.setMessage("Loading service..");
Dialog.show();
Dialog.dismiss();

}

protected Void doInBackground(String... urls) {
try {


HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);


} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}

return null;
}

protected void onPostExecute(Void unused) {
// Close progress dialog
Dialog.dismiss();
Log.e(TAG, "Raw output "
+ Content);


try {

// Load json data and display
JSONObject json = new JSONObject(Content);
JSONArray jre = json.getJSONArray("updates");
for (int j = 0; j < jre.length(); j++) {

JSONObject jobject = jre.getJSONObject(j);

String name11 = jobject.getString("title");
String description = jobject.getString("description");

String image = jobject.getString("image");
String total = url + image;
img[j] = total;



HashMap<String, String> contact = new HashMap<>();

// adding each child node to HashMap key => value
contact.put("title", name11);
contact.put("description", description);
contactList.add(contact);
}




} catch (JSONException e) {

e.printStackTrace();
}


ListAdapter adapter = new SimpleAdapter(mainpage.this, ContactList, R.layout.item, new String[]{"title", "description"}, new int[]{R.id.title, R.id.description});

lv.setAdapter(adapter);}

我的第二个适配器

 public class loader extends ArrayAdapter {

public Context context;
private LayoutInflater inflater;

private String[] img;

public loader(Context context, String[] img) {
super(context, R.layout.item, img);

this.context = context;
this.img = img;

inflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


if (null == convertView) {
convertView = inflater.inflate(R.layout.item, parent, false);

}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);

Picasso.with(context).load(img[position]).into(imageView);

return convertView;

}

我使用

在列表中设置它
        loader im=new loader(mainpage.this,img);

lv.setAdapter(im);

对不起我的长代码

最佳答案

您可以将 Arraylist 与模型(包含您的文本和图像 url 作为字符串)类一起使用,并将该 arraylist 传递给您的适配器。查看以下代码:

新建Product.java

public class Product{  

private String yourText = "";
private String imgUrl= "";


public String getyourText () {
return yourText ;
}

public void setyourText(String yourText) {
this.yourText = yourText
}

public String getimgUrl() {
return imgUrl;
}

public void setimgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}

}

现在你的 AsyncTask:

private LoaderAdapter mLoaderAdapter;
private ArrayList<Product> mArrayList = null;

Product mProduct;

private class LoadService extends AsyncTask<String, Void, Void> {

private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private final String TAG = null;
private ProgressDialog Dialog = new ProgressDialog(mainpage.this);

ImageView imagee = (ImageView) findViewById(R.id.image);
String url = "http://phone.tmsline.com/images/uploads/";
String[] img = new String[1000];
// lv.setAdapter(new ImageLoader(, img));


protected void onPreExecute() {

Dialog.setMessage("Loading service..");
Dialog.show();
Dialog.dismiss();

}

protected Void doInBackground(String... urls) {
try {


HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);


} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}

return null;
}

protected void onPostExecute(Void unused) {
// Close progress dialog
Dialog.dismiss();
Log.e(TAG, "Raw output "
+ Content);


try {

// Load json data and display
JSONObject json = new JSONObject(Content);
JSONArray jre = json.getJSONArray("updates");

mArrayList = new ArrayList<Product>();

for (int j = 0; j < jre.length(); j++) {

mProduct = new Product();

JSONObject jobject = jre.getJSONObject(j);

String name11 = jobject.getString("title");
String description = jobject.getString("description");

String image = jobject.getString("image");
String fullUrl = url + image;

mProduct.setyourText(name11);
mProduct.setimgUrl(fullUrl);

mArrayList.add(mProduct);

mProduct = null;
}




} catch (JSONException e) {

e.printStackTrace();
}


mLoaderAdapter = new LoaderAdapter (getApplicationContext(),mArrayList);

lv.setAdapter(mLoaderAdapter);}

最后是你的适配器类:

public class LoaderAdapter extends ArrayAdapter<Product> {

public Context context;
private ArrayList<Product> values;

private LayoutInflater inflater;

private String[] img;

public loader(Context context, ArrayList<Product> values) {
super(context, R.layout.item, img);

this.context = context;
this.values = values;

inflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


if (null == convertView) {
convertView = inflater.inflate(R.layout.item, parent, false);

}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
TextView textView = (TextView) convertView.findViewById(R.id.yourtextview);

Product mProduct = values.get(position);

textView.setText(mProduct.getyourText());
Picasso.with(context).load(mProduct.getimgUrl()).into(imageView);

return convertView;

}

现在您可以将所有文本和图像绑定(bind)到同一个列表上,而无需为同一个列表使用 1 个适配器类。

干杯!

关于android - 将两个适配器设置到同一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506983/

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