public class RecipeListApapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
List<RecipeBean> recipes;
OnRecipeClickListener onRecipeClickListener;
ImageLoaderListner imageLoaderListner;
public RecipeListApapter(Context context, List<RecipeBean> recipes, OnRecipeClickListener onRecipeClickListener) {
this.context = context;
this.recipes = recipes;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.onRecipeClickListener=onRecipeClickListener;
imageLoaderListner = (ImageLoaderListner)context;
}
@Override
public int getCount() {
return recipes.size();
}
@Override
public RecipeBean getItem(int position) {
return recipes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//int idofimage = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.recipe_item, null);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
initView(position, holder, convertView);
RecipeBean recipe = recipes.get(position);
setView(holder, recipe);
return convertView;
}
private void setView(ViewHolder holder, RecipeBean recipe) {
//idofimage = Comman.getImageResourceId(context, recipe.getRecipeImage());
holder.txt_recipetitle.setText(recipe.getRecipeName());
holder.txt_recipetime.setText(recipe.getRecipetime());
//holder.img_recipe.setImageResource(idofimage);
/* Picasso.with(context).load(recipe.getRecipeUrl())
.into(holder.img_recipe);*/
imageLoaderListner.onImageLoad(recipe.getRecipeUrl(), holder.img_recipe, R.drawable.ic_launcher);
if(recipe.getIsFavourites()==1){
holder.likeTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_selected, 0, 0, 0);
}else
{
holder.likeTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like, 0, 0, 0);
}
}
private void initView(final int position, ViewHolder holder, View convertView) {
holder.layoutRateLike = (LinearLayout) convertView
.findViewById(R.id.layoutRateLike);
holder.layouRate = (LinearLayout)convertView.findViewById(R.id.layouRate);
holder.layoutLike = (LinearLayout)convertView.findViewById(R.id.layoutLike);
holder.lineView = (View)convertView.findViewById(R.id.lineView);
holder.layoutRateLike.setVisibility(View.VISIBLE);
holder.layouRate.setVisibility(View.GONE);
holder.lineView.setVisibility(View.GONE);
holder.img_recipe = (ImageView) convertView
.findViewById(R.id.img_recipe);
holder.txt_recipetitle = (TextView) convertView
.findViewById(R.id.txt_recipetitle);
holder.txt_recipetime = (TextView) convertView
.findViewById(R.id.txt_recipetime);
holder.likeTxt = (TextView) convertView.findViewById(R.id.likeTxt);
holder.layoutLike.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onRecipeClickListener != null) {
onRecipeClickListener.onLikeClick(v, position);
}
}
});
}
static class ViewHolder {
ImageView img_recipe;
TextView txt_recipetitle, txt_recipetime, likeTxt;
LinearLayout layoutRateLike, layoutLike, layouRate;
View lineView;
}
public interface OnRecipeClickListener {
void onLikeClick(View view, int position);
}
/*private class ImageLoadTask extends AsyncTask<Void, Void, Integer> {
String imgName;
ImageView imageView;
public ImageLoadTask(String imgName, ImageView imageView) {
this.imgName = imgName;
this.imageView = imageView;
}
@Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
int imgId = context.getResources().getIdentifier(imgName,
"drawable", context.getPackageName());
return imgId;
}
@Override
protected void onPostExecute(Integer imgResId) {
// TODO Auto-generated method stub
super.onPostExecute(imgResId);
imageView.setImageResource(imgResId);
}
}*/
}
Context context;
LayoutInflater inflater;
List<RecipeBean> recipes;
OnRecipeClickListener onRecipeClickListener;
ImageLoaderListner imageLoaderListner;
public RecipeListApapter(Context context, List<RecipeBean> recipes, OnRecipeClickListener onRecipeClickListener) {
this.context = context;
this.recipes = recipes;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.onRecipeClickListener=onRecipeClickListener;
imageLoaderListner = (ImageLoaderListner)context;
}
@Override
public int getCount() {
return recipes.size();
}
@Override
public RecipeBean getItem(int position) {
return recipes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//int idofimage = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.recipe_item, null);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
initView(position, holder, convertView);
RecipeBean recipe = recipes.get(position);
setView(holder, recipe);
return convertView;
}
private void setView(ViewHolder holder, RecipeBean recipe) {
//idofimage = Comman.getImageResourceId(context, recipe.getRecipeImage());
holder.txt_recipetitle.setText(recipe.getRecipeName());
holder.txt_recipetime.setText(recipe.getRecipetime());
//holder.img_recipe.setImageResource(idofimage);
/* Picasso.with(context).load(recipe.getRecipeUrl())
.into(holder.img_recipe);*/
imageLoaderListner.onImageLoad(recipe.getRecipeUrl(), holder.img_recipe, R.drawable.ic_launcher);
if(recipe.getIsFavourites()==1){
holder.likeTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_selected, 0, 0, 0);
}else
{
holder.likeTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like, 0, 0, 0);
}
}
private void initView(final int position, ViewHolder holder, View convertView) {
holder.layoutRateLike = (LinearLayout) convertView
.findViewById(R.id.layoutRateLike);
holder.layouRate = (LinearLayout)convertView.findViewById(R.id.layouRate);
holder.layoutLike = (LinearLayout)convertView.findViewById(R.id.layoutLike);
holder.lineView = (View)convertView.findViewById(R.id.lineView);
holder.layoutRateLike.setVisibility(View.VISIBLE);
holder.layouRate.setVisibility(View.GONE);
holder.lineView.setVisibility(View.GONE);
holder.img_recipe = (ImageView) convertView
.findViewById(R.id.img_recipe);
holder.txt_recipetitle = (TextView) convertView
.findViewById(R.id.txt_recipetitle);
holder.txt_recipetime = (TextView) convertView
.findViewById(R.id.txt_recipetime);
holder.likeTxt = (TextView) convertView.findViewById(R.id.likeTxt);
holder.layoutLike.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onRecipeClickListener != null) {
onRecipeClickListener.onLikeClick(v, position);
}
}
});
}
static class ViewHolder {
ImageView img_recipe;
TextView txt_recipetitle, txt_recipetime, likeTxt;
LinearLayout layoutRateLike, layoutLike, layouRate;
View lineView;
}
public interface OnRecipeClickListener {
void onLikeClick(View view, int position);
}
/*private class ImageLoadTask extends AsyncTask<Void, Void, Integer> {
String imgName;
ImageView imageView;
public ImageLoadTask(String imgName, ImageView imageView) {
this.imgName = imgName;
this.imageView = imageView;
}
@Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
int imgId = context.getResources().getIdentifier(imgName,
"drawable", context.getPackageName());
return imgId;
}
@Override
protected void onPostExecute(Integer imgResId) {
// TODO Auto-generated method stub
super.onPostExecute(imgResId);
imageView.setImageResource(imgResId);
}
}*/
}
No comments:
Post a Comment