Reputation: 157
My app download text and image from sever and display them in list i use two separate adapters one for text and another for image . i cannot put them in same adapter and also i cannot put both of adapters in same list . so how to connect two adapters with same list that's some of my code for first adapter
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);}
my second 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;
}
i set it in list using
loader im=new loader(mainpage.this,img);
lv.setAdapter(im);
sorry for my long code
Upvotes: 1
Views: 211
Reputation: 856
You can use Arraylist with model(contains your text and image url as string) class and pass that arraylist to your adapter. Check out below code:
Create new 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;
}
}
Now your 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);}
Finally your adapter class:
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;
}
Now you can bind all your text and image on same without with 1 adapter class for same list.
Cheers!!
Upvotes: 1