I am working on social application and it's about to complete but I got stuck on one issue that is image flickering. When there is around 9 to 10 images on screen and if I scroll the page then the image flicker take place.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.view_explore_icon, null);
holder = new ViewHolder();
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
ImageLoader.getInstance().displayImage(
Static_Urls.explore_logo_pic + categoryList.get(position).cat_logo,
holder.img);
if (pos == position) {
holder.back.setBackgroundResource(R.drawable.explore_selected_image);
} else {
holder.back.setBackgroundResource(R.drawable.explore_blank_image);
}
holder.img.setTag(position);
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pos = (int) view.getTag();
txt_cat_name.setText(categoryList.get(position).category);
//notifyDataSetChanged();
new GetAllExplorePic().execute(categoryList.get(position).id);
}
});
return convertView;
}
displace the below lines of code inside of if(convertview == null)
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
your final code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.view_explore_icon, null);
holder = new ViewHolder();
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
// holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
// convertView.setTag(holder);
ImageLoader.getInstance().displayImage(
Static_Urls.explore_logo_pic + categoryList.get(position).cat_logo,
holder.img);
if (pos == position) {
holder.back.setBackgroundResource(R.drawable.explore_selected_image);
} else {
holder.back.setBackgroundResource(R.drawable.explore_blank_image);
}
holder.img.setTag(position);
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pos = (int) view.getTag();
txt_cat_name.setText(categoryList.get(position).category);
//notifyDataSetChanged();
new GetAllExplorePic().execute(categoryList.get(position).id);
}
});
return convertView;
}
Related
I am attempting to create a list of buttons paired with text. Whenever a button in the list is clicked, I want to increment the integer value in the associated text view. My plan was to use the following code, but I'm having an error using viewHolder within the OnClickListener because viewHolder is not final, and it can't be in order to manipulate it within the onClick. How can I modify the associated text value?
public View getView(final int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
viewHolder.button = (Button) convertView.findViewById(R.id.playerButton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
protected String scoreToSet;
#Override
public void onClick(View v) {
viewHolder.text.setText(Integer.toString(modelList.get(position).getScore() + 1)); //Error is occurring here
}
});
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.button.setText(modelList.get(position).getPlayer());
}
return convertView;
}
Thanks for the help!
Change the code like this ,
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder ;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
viewHolder.button = (Button) convertView.findViewById(R.id.playerButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.button.setText(modelList.get(position).getPlayer());
}
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO UPDATE the model here
int newScore = modelList.get(position).getScore() + 1;
modelList.get(position).setScore(newScore);
viewHolder.text.setText(Integer.toString(newScore);
}
});
return convertView;
}
You should add listeners or set values to your views outside if-else blocks -
public View getView(final int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
viewHolder.button = (Button) convertView.findViewById(R.id.playerButton);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.button.setText(modelList.get(position).getPlayer());
viewHolder.button.setOnClickListener(new View.OnClickListener() {
protected String scoreToSet;
#Override
public void onClick(View v) {
setValue(position);
}
});
return convertView;
}
Declare setValue() -
public void setValue(int position){
ViewHolder.text.setText(Integer.toString(modelList.get(position).getScore() + 1));
}
I have a ListView that is correctly populated with an array on create, but when I scroll, the data changes and is all out of order. I don't know wether the problem is in my CustomListViewAdapter or in my DetailsPage. Here's the For Loop that I am using to generate the textViews:
if (currentObject.setTime != null && currentObject.setName != null) {
String[] temporaryNames = currentObject.setName;
int totalNames = (currentObject.setTime.length / currentObject.setName.length);
for (int i = 1; i < totalNames; i++) {
currentObject.setName = ArrayUtils.addAll(currentObject.setName,temporaryNames);
}
}
listView.setAdapter(new BusRouteCustomListViewAdapter(this, currentObject.setName, currentObject.setTime));
and here is the code for my customListViewAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View v = convertView;
if (v == null)
if (locations[position] != null && times[position] != null) {
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
v = inflater.inflate(R.layout.null_row, null);
}
return v;
}
You replace the code with these lines of code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
View v = convertView;
if (v == null)
{
holder = new Holder();
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
return v;
}
Use your getView() like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
You didnt implement design holder probably. make following changes to your getView then it will work fine
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
}
else
{
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
I use an imagebutton in a listview and everything was working fine. But when i tried to change the background resource on the imagebutton on its click i got my app crashed. here is my code.
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setTag(position);
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0);
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
return convertView;
}
This following would be right flow-
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0); // if your error is layout can't be casted to image Button, then in this line may be you are getting an unexpected layout view instead of a ImageButton
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
holder.b.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
I have a custom view fcard which defined with xml.
fcards will be listed in my activity. I want to set an onClickListener to imageView which defined in fcard's.
I have already write my adapter extended from ArrayAdapter. The adapter's getView method given below. If convertView defines as a final object, it cannot be inflated. other way should be define as final for use in imageView's onClickListener and startingAnimation.
how can I accomplish this problem?
I am new on android ,thanks
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) cfcard).getLayoutInflater();
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.view_facility_card, null,
false);
holder.tvSender = (TextView) convertView
.findViewById(R.id.fcardTvSender);
holder.tvDate = (TextView) convertView
.findViewById(R.id.fcardTvDate);
holder.tvPostTitle = (TextView) convertView
.findViewById(R.id.fcardTvPostTitle);
holder.tvPostDetail = (TextView) convertView
.findViewById(R.id.fcardTvPostDetail);
holder.ivFcardBtn = (ImageView) convertView
.findViewById(R.id.fcardButton);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvSender.setText(fcards.get(position).getTvSenderStr());
holder.tvSender.setText("sender");
holder.tvDate.setText("date");
holder.tvPostTitle.setText(fcards.get(position).getTvPostTitleStr());
holder.tvPostDetail.setText("detail");
final Animation anim;
holder.ivFcardBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
anim = AnimationUtils.loadAnimation(getContext(),
android.R.anim.slide_out_right);
}
});
convertView.startAnimation(anim);
return convertView;
}
Edit
I can fix the problem with using implements View.OnClickListener on myAdapter and setOnClickListener(this) to clickable objects. I guess it will help someone. There is my new code below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) cfcard).getLayoutInflater();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.view_facility_card, null,
false);
holder.tvPostTitle = (TextView) convertView
.findViewById(R.id.fcardTvPostTitle);
holder.ivFcardBtn = (ImageView) convertView
.findViewById(R.id.fcardButton);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvPostTitle.setText(fcards.get(position).getTvPostTitleStr());
holder.ivFcardBtn.setOnClickListener(this);
holder.tvPostTitle.setOnClickListener(this);
return convertView;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
// ViewHolder holder; is defined global at myAdapter class
int viewId = v.getId();
if (viewId == holder.ivFcardBtn.getId()) {
//ivFcardBtn clicked
LinearLayout parentpnl = (LinearLayout) v.getParent().getParent();
ObjectAnimator myanimator= (ObjectAnimator) AnimatorInflater
.loadAnimator(getContext(), R.animator.fcardanimator);
myanimator.setTarget(parentpnl);
myanimator.setDuration(3000);
myanimator.start();
} else if (viewId == holder.tvPostTitle.getId()) {
//tvPostTitle clicked.
}
}
I have a listview of about 20 item. each row of adapter have a image button which do different action when i click it. when i click item of the listview, i managed to get position number correctly. but when i click the image button, the position i got is wrong. Instead of 0~19, but it showed 0 1 2 3 0 1 2 3 0 1 2 3 .....
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_video, null);
holder = new ViewHolder();
holder.rlContainer = (RelativeLayout) convertView.findViewById(R.id.rlContainer);
holder.imageView = (ImageView) convertView.findViewById(R.id.ivLogo);
holder.buttonBackground = (ImageView) convertView.findViewById(R.id.ibButtonLebih);
holder.tvtitle = (TextView) convertView.findViewById(R.id.tvTitle);
holder.buttonBackground.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
videoListClickListener.OnVideoMoreClickListener(position);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
Issue solved! by moving onclicklistener and setTag to outside of if/else.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_video, null);
holder = new ViewHolder();
holder.rlContainer = (RelativeLayout) convertView.findViewById(R.id.rlContainer);
holder.imageView = (ImageView) convertView.findViewById(R.id.ivLogo);
holder.buttonBackground = (ImageView) convertView.findViewById(R.id.ibButtonLebih);
holder.tvtitle = (TextView) convertView.findViewById(R.id.tvTitle);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.buttonBackground.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
videoListClickListener.OnVideoMoreClickListener(position);
}
});
convertView.setTag(holder);
return convertView;
}