Android: how to dynamically load resource images in a ListView?
I have a bunch of resource images, and I want to display a certain one for each element of a ListView. Currently I have a row.xml file that has each element displaying the "icon" image. I want to use the same format that I specified in row.xml, but change the image to whatever I want each time.
I have done some research on "lazy loading" but it seems like开发者_JAVA技巧 it is overly complicated and does not apply to this.
You should override the getView method of you listview's adapter so that it will return a component that will inflate your row.xml. This component can then be customized before being returned.
Here is an example :
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//where myview is your custom class loading your row.xml
if( !(convertView instanceof MyView) )
convertView = new MyView();
MyView v = (MyView) convertView;
v.setImage( <your image depending on position> );
return convertView;
}//met
Regards, Stéphane
Ok, to expand on the answer above, you make an "adapter" like the one shown in the code below:
package com.example.consumer.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.consumer.R;
import com.example.consumer.model.AppModel;
import com.example.consumer.model.Deal;
public class DealListAdapter extends BaseAdapter {
private static ArrayList<Deal> dealArrayList = new ArrayList<Deal>();
private LayoutInflater inflator;
private AppModel appModel;
public DealListAdapter(Context context, ArrayList<Deal> results) {
dealArrayList = results;
inflator = LayoutInflater.from(context);
appModel = (AppModel) context.getApplicationContext();
}
public int getCount() {
if (dealArrayList == null)
return 0;
else
return dealArrayList.size();
}
public Object getItem(int position) {
try {
return dealArrayList.get(position);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.list_item_deal, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.deal_listview_item_one);
holder.distance = (TextView) convertView.findViewById(R.id.deal_listview_item_two);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(dealArrayList.get(position).getMerchantName() + ": "+ dealArrayList.get(position).getTitle());
holder.distance.setText(dealArrayList.get(position).getDistanceFormatted(appModel.getLocation()));
return convertView;
}
static class ViewHolder {
TextView name;
TextView distance;
}
}
Note the use of a ViewHolder - this will save a lot of "findViewById" Calls.
To use this adapter in your list, you use this code:
dealList = (ListView) findViewById(R.id.browse_list_view);
dealListAdapter = new DealListAdapter(this, dealList);
dealList.setAdapter(dealListAdapter);
Where in this instance, the dealList
is an arrayList of Deals (you would use some other custom object)
Hopefully this helps with your OO programming... :)
I found one useful example to display image in listview, gridview. Hope it will be very useful to you.
https://github.com/nostra13/Android-Universal-Image-Loader
精彩评论