I have a custom adapter class which extended by ArrayAdapter. I set the adapter to a listview in my activity. In my objects list I have 8 objects. In the listview shows 5 objects when it's loading(when i'm scrolling the listview it has 3 more data to show). After I scroll the listview last 3 objects are showing same as 1st 3 objects. Here's the code what I tried.
Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
final MenuItem listItem = objects.get(position);
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + --i);
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
Activity
listViweMenu.destroyDrawingCache();
listViweMenu.setVisibility(ListView.INVISIBLE);
listViweMenu.setVisibility(ListView.VISIBLE);
menuListAdapter.notifyDataSetChanged();
List<uk.co.bapos.android.baposwaiter.data.models.menu.MenuItem> itemsList
= MenuItemController.fetchAllCategoryItems(this, String.valueOf(new ArrayList<>(menuItemData.entrySet()).get(position).getValue()));
listDataMenu.clear();
listDataMenu.addAll(itemsList);
How may I fix this?
You are calling getTag(), setTag() and setting the data in wrong place. Try this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final MenuItem listItem = objects.get(position);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + --i);
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
}
return convertView;
}
After getting all the id's of views use setTag() and if view is not null use getTag() to get all id's back. Only after that set data to the views.
In Activity :
ArrayAdapter adapter = myAdapter();//pass arraylist or data to adapter here
listview.setAdapter(adapter);
Try this.
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_list_item, parent);
//initializing your view elements
convertView.setTag(holder);
}
else{
holder = (Holder) convertView.getTag();
}
// do other stuff after if-else, setting the values to view elements
You need to understand why holder is used and set as tag.
If convertView is not null means you will be reusing the view (this improves performance and saves memory)
Now what you are doing when it is not null is
holder = (Holder) convertView.getTag();
and returning sameConvertView so you get the same view
What you need to do after getting holder tag is change the view information using holder
Eg holder.textViewItemName.setText(listItem.getItemName());//Needs to get the current list item based on position.
#Override public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
final MenuItem listItem = objects.get(position);
holder = new Holder(); LayoutInflater inflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView)convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder); }
else
{
holder = (Holder) convertView.getTag();
}
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice()))); // Check & Set if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set if (holder.buttonMinus != null)
{
holder.buttonMinus.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + --i);
}
} }); }
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
return convertView;
}
Related
I created custom adapter to bind with listview but it's showing duplicate songs in listview. please check out the code and let me know if anything is wrong
public class UnfavoriteSongAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<Media> data;
private static LayoutInflater inflater = null;
private Context context;
public UnfavoriteSongAdapter(Context context, Activity a, ArrayList<Media> d) {
activity = a;
this.context = context;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
GenreFavoriteClickListner genreFavoriteClickListner;
public interface GenreFavoriteClickListner {
public void onFavoriteClickListner(int position,Media media);
}
public void setGenreFavoriteClickListner(GenreFavoriteClickListner genreFavoriteClickListner) {
this.genreFavoriteClickListner = genreFavoriteClickListner;
}
public static class ViewHolder {
public TextView textView_title, textView_artist, textView_time;
public CircleImageView circleImageView_albumphoto, circleImageView_favorite;
public ImageView imageView_play;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder viewHolder;
try {
if (convertView == null) {
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
} else {
viewHolder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
//viewHolder.textview_albumtitle.setText("No Appointment");
} else {
final Media p = data.get(position);
viewHolder.textView_title.setText(p.getSongName());
viewHolder.textView_artist.setText(p.getArtistName());
long millis = Long.parseLong(p.getDuration());
String hms = ReusableModules.getCalculatedTime(millis);
viewHolder.textView_time.setText(hms);
String isplaying = p.getIsPlaying();
int playingid = R.mipmap.play;
if (isplaying != null) {
if(isplaying.contentEquals(Constants.ONE)){
playingid = R.mipmap.runing_music_play;
}
}
viewHolder.imageView_play.setImageResource(playingid);
ReusableModules.setFavoriteToggleButton(p.getIsFavorite(), viewHolder.circleImageView_favorite);
viewHolder.circleImageView_favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (genreFavoriteClickListner != null) {
genreFavoriteClickListner.onFavoriteClickListner(position,p);
}
}
});
final String finalUrl = p.getAlbumUrl();
viewHolder.circleImageView_albumphoto.post(new Runnable() {
#Override
public void run() {
BaseActivity.imageLoader.displayImage(finalUrl,
viewHolder.circleImageView_albumphoto, SplashScreenActivity.displayImageOptions, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(Bitmap loadedImage) {
}
});
}
});
vi.setTag(p);
}
} catch (Exception e) {
}
return vi;
}
}
Change in getView() method like this.
viewHolder = new ViewHolder();
if (convertView == null) {
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
and remove this
else {
viewHolder = (ViewHolder) vi.getTag();
}
in last of getView() method
convertView.setTag(holder);
try this and tell me it works or not?
move
vi.setTag(viewholder);
inside
if(convert==null){
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
// here
vi.setTag(viewholder);
}
and remove this:
final String finalUrl = p.getAlbumUrl();
viewHolder.circleImageView_albumphoto.post(new Runnable() {
#Override
public void run() {
BaseActivity.imageLoader.displayImage(finalUrl,
viewHolder.circleImageView_albumphoto, SplashScreenActivity.displayImageOptions, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(Bitmap loadedImage) {
}
});
}
});
remove this **vi.setTag(p);**
Add vi.setTag(p) inside :
if(convertView == null){
vi.setTag(p);
}
set tag for view holder if view holder null. Add last line of code
if (convertView == null) {
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
/************ Set holder with LayoutInflater ************/
vi.setTag( viewHolder );
}
//check you data size getCount,its a good practice. not in getview method.
#Override
public int getCount() {
if(data!=null && data.size()> 0)
return data.size();
else
return 0;
}
I suggest you to replace getview method and check you data once because your adapter code seems perfect.
I have a list,When user clicks an item in that list three buttons will be showed under that item (namely Yes,No and Maybe) so user may select any one of the buttons and that response(as 1,2,3 for yes,no,maybe) will be saved.In adapter's getView just we retrieve that response and showing imageView on all list item's row
setting image in ArrayAdapter's getView
EventUserRelationMO eventUserRelationMO1 =new EventUserRelationMO();
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
this is perfectly works but when i have more number of items in the list image is not visible for corresponding item/shuffled while scrolling
private class UserOccasions extends ArrayAdapter<EventMO> {
private ArrayList<EventMO> eventMOs;
LayoutInflater mInflater;
UserOccasions(Context context, int textViewResourceId,
ArrayList<EventMO> eventMOs) {
super(context, textViewResourceId, eventMOs);
this.eventMOs = new ArrayList<EventMO>();
this.eventMOs.addAll(eventMOs);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class ViewHolder {
TextView eventNameTxtV;
TextView eventPlaceTxtV;
TextView sendByNameTxtv;
ImageView yes;
ImageView no;
ImageView maybe;
}
#Override
public int getCount() {
return eventMOs.size();
}
/*#Override
public Object getItem(int position) {
return position;
}*/
#Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final EventMO eventMO = eventMOs.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
holder = new ViewHolder();
holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationCheckBox);
holder.sendByNameTxtv = (TextView) convertView.findViewById(R.id.sendByName);
holder.yes = (ImageView) convertView.findViewById(R.id.yes);
holder.no = (ImageView) convertView.findViewById(R.id.no);
holder.maybe = (ImageView) convertView.findViewById(R.id.maybe);
eventUserRelationMOs = eventMO.getEventUserRelationBOs();
sendUserMO = eventMO.getUserBO();
eventIdPosition = eventMO.getEventId();
for (EventUserRelationMO eventUserRelationMO1 : eventUserRelationMOs) {
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.yes.setTag(eventMOs.get(position));
holder.no.setTag(eventMOs.get(position));
holder.maybe.setTag(eventMOs.get(position));
holder.eventNameTxtV.setText(eventMOs.get(position).getText());
holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
holder.sendByNameTxtv.setText(sendUserMO.getUserName());
View v = convertView.findViewById(R.id.invitation_single);
v.getRootView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent occasionAct = new Intent(InvitationFragment.this.getActivity(), InvitationActivity.class);
occasionAct.putExtra("eventMO", eventMO);
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
});
return convertView;
}
this is my full adapter code any one help me to maintain corresponding imageView for each item in the list when scrolling
ListView works by recycling the views.To know more on how it works you can have a look at this (http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/).
The problem with the code is that the image is being set only once when convertView is null. When you scroll the list, the list view passes a recycled row into the getView function (this is when the convertView will not be null). In this case you are not setting the ImageView.
I have modified the code, this should work
// show list values name and mobile number in contact page
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final EventMO eventMO = eventMOs.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
holder = new ViewHolder();
holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationCheckBox);
holder.sendByNameTxtv = (TextView) convertView.findViewById(R.id.sendByName);
holder.yes = (ImageView) convertView.findViewById(R.id.yes);
holder.no = (ImageView) convertView.findViewById(R.id.no);
holder.maybe = (ImageView) convertView.findViewById(R.id.maybe);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
eventUserRelationMOs = eventMO.getEventUserRelationBOs();
sendUserMO = eventMO.getUserBO();
eventIdPosition = eventMO.getEventId();
for (EventUserRelationMO eventUserRelationMO1 : eventUserRelationMOs) {
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
}
holder.yes.setTag(eventMOs.get(position));
holder.no.setTag(eventMOs.get(position));
holder.maybe.setTag(eventMOs.get(position));
holder.eventNameTxtV.setText(eventMOs.get(position).getText());
holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
holder.sendByNameTxtv.setText(sendUserMO.getUserName());
View v = convertView.findViewById(R.id.invitation_single);
v.getRootView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent occasionAct = new Intent(InvitationFragment.this.getActivity(), InvitationActivity.class);
occasionAct.putExtra("eventMO", eventMO);
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
});
return convertView;
}
My ListView displays in first Item something else as the rest of the ListView
The Problem is, that it replaces the first item instead of taking a own position.
Example how it has to be:
A (diferent to the others)
B
B
B
How it is:
A (B Overlaid by A)
B
B
As you can see, one B is missing because it is overlaid by A.
Here is the Code. I wrote my own CustomAdapter:
#Override
public int getCount() {
if (taskItems.size() + 1 >= 0)
return taskItems.size();
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView taskTitleTextView;
public TextView taskObjectTextView;
public TextView taskLocationTextView;
public ImageView taskImageImageView;
}
public static class ViewHolderProjects {
public TextView projectTitelTextView;
public TextView projectInfoTextView;
public TextView projectDeadline;
public ImageView projectImageImageView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolderProjects holderProjects;
if (position == 0) {
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_projects, null);
holderProjects = new ViewHolderProjects();
holderProjects.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
holderProjects.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
holderProjects.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
holderProjects.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);
vi.setTag(holderProjects);
} else {
holderProjects = (ViewHolderProjects) vi.getTag();
}
if (projectMap.size() <= 0) {
holderProjects.projectInfoTextView.setText("Keine Infos");
holderProjects.projectTitelTextView.setText("Kein Titel");
} else {
if (projectMap.get(position).containsKey("project_company_image")) {
if (projectMap.get(position).get("project_company_image").getBytes().length == 0) {
holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
return vi;
}
Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(projectMap.get(position).get("project_company_image")));
holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
holderProjects.projectImageImageView.setImageBitmap(bm);
}
}
} else {
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_tasks, null);
holder = new ViewHolder();
holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (taskItems.size() <= 0) {
holder.taskTitleTextView.setText("Kein Titel");
holder.taskObjectTextView.setText("Kein Objekt");
holder.taskLocationTextView.setText("Kein Standort");
} else {
if (taskItems.get(position).containsKey("object_image")) {
if (taskItems.get(position).get("object_image").getBytes().length == 0) {
holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
return vi;
}
}
Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("object_image")));
holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
holder.taskImageImageView.setImageBitmap(bm);
}
}
return vi;
}
First inflate your row layout into view:
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(<R.layout.header>, myListView, false);
Now you can take each component of view and can assign the values to them.eg:
TextView tv = header.findViewById(<R.id.text>);
tv.setText("Something");
Now just pass that header to the list view.
myListView.addHeaderView(header, null, false);
To add listener to that header. Just pass a listnener to that view group:
header.setOnClickListener(<pass a new OnClickListener Instance>);
After that you put the code to add adapter into list view.
myListView.setAdapter(<your adapter instance>);
If there is condition in inflating view in getView of CustomAdapter then you must
override extra two methods..
declare a integer named "type" globally in adapter class
int type;
Override these two functions
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if(position == 0){
return 0;
}else{
return 1;
}
return super.getItemViewType(position);
}
Make a slight change to your getView function as given in the following code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolderProjects holderProjects;
type = getItemViewType(position);
if (type == 0) {
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_projects, null);
holderProjects = new ViewHolderProjects();
holderProjects.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
holderProjects.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
holderProjects.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
holderProjects.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);
vi.setTag(holderProjects);
} else {
holderProjects = (ViewHolderProjects) vi.getTag();
}
if (projectMap.size() <= 0) {
holderProjects.projectInfoTextView.setText("Keine Infos");
holderProjects.projectTitelTextView.setText("Kein Titel");
} else {
if (projectMap.get(position).containsKey("project_company_image")) {
if (projectMap.get(position).get("project_company_image").getBytes().length == 0) {
holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
return vi;
}
Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(projectMap.get(position).get("project_company_image")));
holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
holderProjects.projectImageImageView.setImageBitmap(bm);
}
}
} else {
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_tasks, null);
holder = new ViewHolder();
holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (taskItems.size() <= 0) {
holder.taskTitleTextView.setText("Kein Titel");
holder.taskObjectTextView.setText("Kein Objekt");
holder.taskLocationTextView.setText("Kein Standort");
} else {
if (taskItems.get(position).containsKey("object_image")) {
if (taskItems.get(position).get("object_image").getBytes().length == 0) {
holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
return vi;
}
}
Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("object_image")));
holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
holder.taskImageImageView.setImageBitmap(bm);
}
}
return vi;
}
You can set header to ListView like this
View headerView = getLayoutInflater().inflate(R.layout.header_view, null, false);
listview.addHeaderView(headerView);
Note: you have to set header view before setting the adapter
First time 16 item load in the listview.
I need only 2 item selected (red color button).
But when I scroll the listview all item after i showing wrong selected 11 item.
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
#SuppressLint("ResourceAsColor")
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
v.setTag(holder);
}
else
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
return v;
}
Please write else part also.
if (selectedarray.get(position).equals("1")) {
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
} else {
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
First
if (selectedarray.get(position).equals("1")) {
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
} else {
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
Second dont keep it under if (convertView == null) or in else part
keep it after if and else block
see here
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
v.setTag(holder);
}
else
{
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
}
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
#SuppressLint("ResourceAsColor")
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
return v;
}
i solved my problem.
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
#SuppressLint("ResourceAsColor")
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
v.setTag(holder);
}
else
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
else
{
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
return v;
}
I am creating an app where there are images in a custom Listview. The database put a picture or another. Clicking the image has to change. But when I click the image is not changed at the correct ImageView. It changes other ImageView.
I need any help. thanks
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final MonumentosHolder holder = new MonumentosHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
holder.position = position;
final MonumentosObj p = monumentosLista.get(position);
holder.texto.setText(p.getNombre());
if (p.getVisto().equals("n")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
}
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(holder.position);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(holder.position);
}
});
imageLoader.displayImage(imageUrls[position], holder.imagen, options, animateFirstListener);
return v;
}
EDIT
Onclick not change the correct image. But when I scroll the listview to below and and return to top, the correct image has changed...
I think that bit of code maybe wrong :
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
private int pos = position;
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(pos);
Try to store position inside holder object and when assigning it in onClick Listener instead
private int pos = position;
do
private int pos = holder.getPosition()
Remember that android ListViews are not loading all items at one time it just loads thats what is visible and few items beyond that,
Also remember that positions of your items in your Arrays doesn't change you may use it somehow :)
try to change
holder.imagenVisto.setOnClickListener(new View.OnClickListener()
by
holder.imagenVisto.setOnClickListener(new OnClickListener()
I think your click listener affect early items in your list!
do this:
put a long item in your viewHolder:
class MonumentosHolder {
.
.
.
long position;
}
and then in your getView, set the holder positon:
View v = convertView;
--EDITED--
final MonumentosHolder holder = new MonumentosHolder();
--END EDITED--
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
**holder.postion = position;**
and then click listener would be like this:
EDITED
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(**holder.position**);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(holder.position);
}
});
EDIT 4
remove the final modifier from holder, define another MonumentosHolder named finalHolder like this:
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
MonumentosHolder holder = new MonumentosHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
holder.position = position;
final MonumentosObj p = monumentosLista.get(position);
holder.texto.setText(p.getNombre());
if (p.getVisto().equals("n")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
}
final MonumentosHolder finalHolder = holder;
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(finalHolder.position);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(finalHolder.position);
}
});
imageLoader.displayImage(imageUrls[position], holder.imagen, options, animateFirstListener);
return v;
}