How do you inflate a view and add it to a the list of child views of a Linear开发者_JAVA百科Layout
?
When you call the inflater constructor, set attachToRoot
to false; then manually add the view after you've initialized it. Otherwise, you'll lose your initialization for all but the first child added.
Example:
View view = inflater.inflate(R.layout.some_view, parent, false);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
parent.addView(view);
An example of what not to do:
View view = inflater.inflate(R.layout.some_view, parent);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
public class SearchResultAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<SearchResultInfo> ChoseInfo;
private static LayoutInflater inflater=null;
public SearchResultImageLoader imageLoader;
public SearchResultAdapter(Activity a, ArrayList<SearchResultInfo> ChoseInfo) {
activity = a;
this.ChoseInfo=ChoseInfo;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new SearchResultImageLoader(activity.getApplicationContext());
}
public int getCount() {
return ChoseInfo.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView PriceValue;
public ImageView image;
public TextView LikeValue;
public TextView LikeName;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.searchresult_showlayout_item, null);
holder=new ViewHolder();
holder.PriceValue=(TextView)vi.findViewById(R.id.priceValue);
holder.image=(ImageView)vi.findViewById(R.id.clothimage);
holder.LikeName=(TextView)vi.findViewById(R.id.LikeName);
holder.LikeValue=(TextView)vi.findViewById(R.id.LikeValue);
vi.setTag(holder);
}
else
{
holder=(ViewHolder)vi.getTag();
}
holder.PriceValue.setText(ChoseInfo.get(position).Price);
holder.LikeValue.setText(ChoseInfo.get(position).LikeNum);
holder.image.setTag(ChoseInfo.get(position).BitmapPath);
imageLoader.DisplayImage(ChoseInfo.get(position).BitmapPath, activity, holder.image);
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里以后要进行传值
Intent intent =new Intent();
intent.setClass(getDialogContext(activity), SearchDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
getDialogContext(activity).startActivity(intent);
}
});
return vi;
}
精彩评论