listview of image and text in Android
I have a button and when I click on it, I want it to show a listview with image & text (static text). How can I do this?
My code..
public class category extends ListActivity {
Button category;
TextView selection;
private static final String[] country = { "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
"Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
"Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD",
"EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD",
"LTL ", "EUR"
};
/** Called when the activity is first created. */
@Override
开发者_开发百科public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
category=(Button)findViewById(R.id.category);
class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(OnClickListener onClickListener) {
mInflater = LayoutInflater.from((Context) onClickListener);
}
public int getCount() {
return country.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(curr[position]);
holder.text2.setText(country[position]);
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
}
}
category.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListView l1 = (ListView) findViewById(R.id.ListView);
l1.setAdapter(new EfficientAdapter(this));
}
});
}
}
"R.layout.listview" in this layout file ,which u r inflating in getView method, add one imageview.
Now add imageview in view holder in place of second textview text2.
class ViewHolder { TextView text; ImageView IM2; }
Do this in getView:
holder.IM2 = (ImageView) convertView .findViewById(R.id.ImageView02);
In place of
holder.text2.setText(country[position]);
, set a background image on your imageview.
ex. holder.IM2.setBackgroundDrawable(getResources().getDrawable(R.drawable.vimage));
Thanks.
精彩评论