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;
}
}
Related
i have done custom spinner with custom adapter in it but getting some default text everytime how to avoid this issue everytime it should only display 0th position text on spinner:
CourseAdapter courseAdapter = new CourseAdapter(mContext);
customViewHolder.spinner.setAdapter(courseAdapter);
while CourseAdapter.java
public class CourseAdapter extends BaseAdapter {
public static final String TAG=CourseAdapter.class.getName();
List<String> courses =new ArrayList<>(); // code to get the courses ArrayList
Context context;
private void fillCourses()
{
for(int i =0;i<10;i++)
{
if(i == 0)
{
courses.add("Choose");
}
else
{
courses.add("courses:"+i);
}
}
}
public CourseAdapter(Context context)
{
this.context = context;
fillCourses();
}
#Override
public int getCount() {
return courses.size();
}
#Override
public Object getItem(int position) {
return courses.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderFilterItem viewHolder;
if (convertView == null) {
Log.d(TAG, "fillCourses size:" + courses.size());
convertView = LayoutInflater.from(context)
.inflate(R.layout.layout_customspinner, parent, false);
viewHolder = new ViewHolderFilterItem();
viewHolder.textView = (TextView) convertView.findViewById(R.id.txtViewFilterItem);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkboxFilterItem);
//store the holder with the view.
convertView.setTag(viewHolder);
return convertView;
}
else
{
Log.d(TAG, "convertview != null:" + courses.size());
viewHolder = (ViewHolderFilterItem) convertView.getTag();
}
if(courses != null && courses.size() > 0) {
String itemText = courses.get(position);
if (itemText != null) {
viewHolder.textView.setText(itemText);
viewHolder.checkBox.setChecked(true);
}
if(position == 0)
{
viewHolder.checkBox.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolderFilterItem{
TextView textView;
CheckBox checkBox;}
}[![[why Filter item text with checkbox appearing ?][1]][1]
you have to use courseAdapter.notifyDataSetChanged() after setting customViewHolder.spinner.setAdapter(courseAdapter);
Hope this helps.
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;
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
public class ImageAdapter extends BaseAdapter {
LayoutInflater inflater;
ImageView photo;
CheckBox CHECK;
BitmapFactory.Options options;
public ImageAdapter(Context c) {
inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return receivedphoto.size();
}
public Object getItem(int position) {
return receivedphoto.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.phototab_list, parent, false);
photo = (ImageView)convertView.findViewById(R.id.PHOTOS);
CHECK = (CheckBox)convertView.findViewById(R.id.PHOTOSCHECK);
options = new BitmapFactory.Options();
options.inSampleSize = 16;
}
Log.i("LOAD", position + "");
if(ReceivePhotos.get(position) == null) {
ReceivePhotos.set(position, BitmapFactory.decodeFile((String)(receivedphoto.get(position)), options));
}
photo.setImageBitmap(ReceivePhotos.get(position));
int check = Photoischeck.get(position);
if(check == 0) {
CHECK.setChecked(false);
} else {
CHECK.setChecked(true);
}
return convertView;
}
}
I use ArrayList as GridView data.
but when I scroll down and scroll up, my photos is mixed.
mixed means that photos' position is changed.
Why is that?
Please, help me ㅠㅜ
Try this... Use View holder for holding details...
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if(convertView == null) {
view = inflater.inflate(R.layout.phototab_list, parent, false);
holder = new ViewHolder();
holder.photo = (ImageView)convertView.findViewById(R.id.PHOTOS);
holder.CHECK = (CheckBox)convertView.findViewById(R.id.PHOTOSCHECK);
holder.options = new BitmapFactory.Options();
holder.options.inSampleSize = 16;
}else {
holder = (ViewHolder) view.getTag();
}
Log.i("LOAD", position + "");
if(ReceivePhotos.get(position) == null) {
ReceivePhotos.set(position, BitmapFactory.decodeFile((String)(receivedphoto.get(position)), options));
}
holder.photo.setImageBitmap(ReceivePhotos.get(position));
int check = Photoischeck.get(position);
if(check == 0) {
holder.CHECK.setChecked(false);
} else {
holder.CHECK.setChecked(true);
}
return view;
}
I have a listView (vertical) and every list item has a horizontal list view (horizontal).
But the problem is when i scroll the horizontal scrollview in the row the vertical list is also calling getView()...
So, there is a huge performance hit..
So , can any one tell me a better solution to it ..
public class GridViewAdapter extends BaseAdapter {
List<List<Hotel>> gridDatasource;
Context mContext;
public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}
public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}
#Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}
#Override
public Object getItem(int position) {
return gridDatasource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);
holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}
}
static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}
public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;
public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}
#Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}
#Override
public Object getItem(int position) {
return mHotels.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}
Dear i suggest to try My this Code
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}
Use The Holder Class