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;
}
Related
I'm a beginner in Android development. This problem happens when I am vertically scrolling in this listview. Switch button changes the state automatically. How do I solve this issue? The code is shown below:
public class TrainerSessionAdapterView extends BaseAdapter {
public TrainerSessionAdapterView(Context context) {
this.context = context;
inflater = LayoutInflater.from(this.context);
workoutList = new ArrayList<>();
newSessionList = new ArrayList<>();
type = context.getResources().getString(R.string.type);
other = context.getResources().getString(R.string.other);
objectID = context.getResources().getString(R.string.object_id);
}
#Override
public int getCount() {
if (workoutList.isEmpty()) {
return 0;
} else {
return workoutList.size();
}
}
#Override
public ParseObject getItem(int position) {
return workoutList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
removeSession = new ArrayList<String>();
trainerWorkoutList = (ArrayList<String>)user.get("trainer_workout_type");
if (convertView == null) {
convertView = inflater.inflate(R.layout.session_listview, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (!workoutList.isEmpty() && position < workoutList.size() && change) {
ParseObject workoutObject = workoutList.get(position);
if (!TextUtils.isEmpty(workoutObject.get(type).toString()));
holder.sessionTypeView.setText(workoutObject.get(type).toString());
if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
if (trainerWorkoutList.contains(workoutObject.getObjectId())){
holder.sessionSwitch.setChecked(true);
}
}
else{
holder.sessionSwitch.setChecked(false);
}
}
return convertView;
}
}
add this line in your getView method.
holder.switch.setChecked(ArrayList.get(position).isStudentPresent());
You could try this,
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
removeSession = new ArrayList<String>();
trainerWorkoutList = (ArrayList<String>)user.get("trainer_workout_type");
if (convertView == null) {
convertView = inflater.inflate(R.layout.session_listview, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.sessionSwitch.setChecked(false);
if (!workoutList.isEmpty() && position < workoutList.size() && change) {
ParseObject workoutObject = workoutList.get(position);
if (!TextUtils.isEmpty(workoutObject.get(type).toString()));
holder.sessionTypeView.setText(workoutObject.get(type).toString());
if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
if (trainerWorkoutList.contains(workoutObject.getObjectId())){
holder.sessionSwitch.setChecked(true);
}
}
}
return convertView;
}
}
I need your help. My problem is that I need to highlight the selected listview item. Note that it only needs to set single item, not multiple.
Here's what I've tried so far:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup && position == 0) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup) { // first time highlight first row
firstTimeStartup = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}
highlightCurrentRow(viewHolder.background);
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup = true;
But the problem is I couldn't unhighlight the item when I clicked the convertView. Also the default (first) item is not unhighlighting when I tried to click other items. Would appreciate any help. Thanks!
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
for(int i=0;i<data.size();i++){
if(i == position){
highlightCurrentRow(viewHolder.background);
}
else{
unhighlightCurrentRow(viewHolder.background);
}
}
});
create a method like this :
CustomAdapter extends BaseAdapter {
int selectedPosition=0;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(position==selectedPosition){
highlightCurrentRow(viewHolder.background);
}else{
unhighlightCurrentRow(viewHolder.background);
}
}
void highlightUnhighlight(int position){
seslectedPostion= postion;
notifyDatasetChanged
}
}
Call highlightUnhighlight(postion) in onItemClickListener.
I edited your code ,please check this code may be your problem will solve. I used this code in my project.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup[position]) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup[position]) {
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}else{
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = true;
highlightCurrentRow(viewHolder.background);
}
notifyDataSetChanged();
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup[] = new Boolean[data.size()];
firstTimeStartup[0]=true;
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;
}
I have a listview, which shows the picture, type/description of car and a checkbox. When the user now selected a car/s, I want to show them in another listview. When he unselected him from the CarListAdapter, it should also disappear from the new listview. Can I realize this with using the same Viewholder?
class CarListAdapter extends BaseAdapter
{
ArrayList<ItemsHolder> holderList;
BitmapHelper bitmapHelper = BitmapHelper.getInstance();
public CarListAdapter(ArrayList<ItemsHolder> holderList)
{
this.holderList = holderList;
}
#Override
public int getCount()
{
return holderList.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(RentingDataActivity.this);
convertView = inflater.inflate(R.layout.listview_renting_entry, null, true);
holder = new ViewHolder();
holder.rowCarId = (TextView) convertView.findViewById(R.id.rowCarId);
holder.rowImageView = (ImageView) convertView
.findViewById(R.id.rowImageView);
holder.rowCarType = (TextView) convertView.findViewById(R.id.rowCarType);
holder.rowCarDescription = (TextView) convertView
.findViewById(R.id.rowCarDescription);
holder.rowCheckbox = (CheckBox) convertView.findViewById(R.id.rowCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
// holder.rowCheckbox.setOnCheckedChangeListener(null);
// holder.rowCheckbox.setTag(R.id.rowCheckBox, position);
}
// auto = holderList.get(position);
final ItemsHolder ih = holderList.get(position);
holder.rowImageView.setImageBitmap(bitmapHelper.getBigImage(ih.getImagePath(), 248));
holder.rowCarType.setText(ih.getCarType());
holder.rowCarDescription.setText(ih.getCarDescription());
// holder.rowCheckbox.setTag(ih);
holder.rowCarId.setText(ih.getCarId());
holder.rowCheckbox.setChecked(ih.isSelected());
holder.rowCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.rowCheckBox);
// Toast.makeText(getApplicationContext(), holder.rowCarType.getText().toString(),
// Toast.LENGTH_LONG).show();
ih.setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
I would add this as a comment but alas I haven't got enough points:
add to your adapter;
notifyDataSetChanged( );
This refreshes the list asynchronously.
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