I have a list view and items have an image button and a ProgressBar.
I what the ProgressBar to be updated(Progress set) from a Thread.
Whenever getView in list adapter be invoked list view items will be instantiated
and the reference to the ProgressBar will change.so i put the last reference of ProgressBar in a HashMap in the adapter and when i want to update ProgressBar i use it as the last reference.
But it works differently depending on number of items.when the size of items is too much and list view is in scroll mode it works properly but with one or two items that fit in one page without scrolling, ProgressBar will get stuck but with selecting one item, ProgressBar continues with no problem.
It seems after last getView and next notifyDataSetChanged() that may be caused by list selection or Scrolling, ProgressBar's reference had been changed out of getView() so i can not have the last one(or no,i don't know!).
I also tried notifyDataSetChanged() on every setProgress but it is not efficient and also blocks imageButton OnClickListener.
here is adapter code:
public class ChatAdapter extends BaseAdapter {
private List<Message> messagesItems;
private Map<Message,ProgressWheel> ProgressBars =new HashMap<>();//to have last reference to the ProgressBar corresponding to each listItem
public Map<Message,Boolean> showProgress=new HashMap<>();//for some controlling
public Map<Message,Boolean> active=new HashMap<>();//for some controlling
public Map<Message,View> layouts=new HashMap<>();//for some controlling
public Map<Message,View> views=new HashMap<>();//for some controlling
final static android.os.Handler mHandler=new android.os.Handler();
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Message currentMessage = (Message) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if(convertView==null) {
convertView = mInflater.inflate(R.layout.chat_row_right, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder=(ViewHolder) convertView.getTag();
}
ImageButton sendButton= viewHolder.SkipButton;
View progressLayout= convertView.findViewById(R.id.proressLayout);
//ProgressWheel is a custom progressBar
ProgressWheel progressWheel = viewHolder.progressBar;
ProgressBars.put(currentMessage, progressWheel);
layouts.put(currentMessage,progressLayout);
views.put(currentMessage, convertView);
if(showProgress.get(currentMessage)!=null) {
if (showProgress.get(currentMessage)) {
progressLayout.setVisibility(View.VISIBLE);
sendButton.setTag(currentMessage);
//ProgressBarHandler.start just runs a thread and increments progress in background by calling setDelayProgress from this class
if (active.get(currentMessage) == null) {
active.put(currentMessage, true);
ProgressBarHandler progressBarHandler = new ProgressBarHandler(currentMessage, this,this);
progressBarHandler.start(10);
notifyDataSetChanged();
} else if (active.get(currentMessage) == false) {
active.put(currentMessage, true);
ProgressBarHandler progressBarHandler = new ProgressBarHandler(currentMessage, this,this);
progressBarHandler.start(10);
notifyDataSetChanged();
}
} else {
progressLayout.setVisibility(View.GONE);
}
}else
progressLayout.setVisibility(View.GONE);
return convertView;
}
public void setDelayProgress(final Message message, final int progress, final long durationMillis){
mHandler.post(new Runnable() {
#Override
public void run() {
ProgressBars.get(message).setProgress(progress);
// notifyDataSetChanged();// is cause the code extremely inefficient But working !
}
});
}
final static class ViewHolder {
public TextView message;
public ProgressWheel progressBar;
public ImageButton SkeepButton;
public ViewHolder(View convertView){
this.message = (TextView) convertView
.findViewById(R.id.chatMessage);
this.progressBar = (ProgressWheel) convertView
.findViewById(R.id.delayProgress);
this.SkeepButton = (ImageButton) convertView
.findViewById(R.id.sendb);
}
}
}
Your suggestions would be appreciated,Thanks
Related
I'm making an app using TMDB API and have gotten stuck at a small issue.
TMDB API shows seasons and episodes which are empty, basically, those are yet to air but since those are empty, the app shows a blank item that I'm trying to get rid of.
Here's my adapter:
public class SeasonAdapter extends RecyclerView.Adapter<SeasonAdapter.ViewHolder> {
private final List<Season> seasons;
private final Context context;
private final RequestOptions requestOptions;
public SeasonAdapter(List<Season> seasons, Context context) {
this.seasons = seasons;
this.context = context;
requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.poster_placeholder).error(R.drawable.poster_placeholder);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_season_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final Season season = seasons.get(position);
holder.tvTitle.setText(season.getSeasonTitle());
if (season.getSeasonDate() != null && !season.getSeasonDate().isEmpty()) {
holder.tvDate.setText(context.getResources().getString(R.string.aired_on) + season.getSeasonDate());
} else {
holder.tvDate.setVisibility(View.GONE);
}
if (season.getSeasonEpisodes() == 0) {
seasons.remove(position);
}
holder.tvEpisodes.setText(String.valueOf(season.getSeasonEpisodes()) + context.getResources().getString(R.string.total_episodes));
Glide.with(context).load(season.getSeasonImageURL()).apply(requestOptions).into(holder.ivPoster);
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(context, EpisodeActivity.class);
intent.putExtra("title", season.getShowTitle());
intent.putExtra("seasonTitle", season.getSeasonTitle());
intent.putExtra("seasonNo", season.getSeasonNo());
intent.putExtra("tvId", season.getTvId());
v.getContext().startActivity(intent);
});
}
#Override
public int getItemCount() {
return seasons.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView ivPoster;
public TextView tvTitle, tvDate, tvEpisodes;
public ViewHolder(#NonNull View itemView) {
super(itemView);
ivPoster = itemView.findViewById(R.id.ivSeasonPoster);
tvTitle = itemView.findViewById(R.id.tvSeasonTitle);
tvDate = itemView.findViewById(R.id.tvSeasonAired);
tvEpisodes = itemView.findViewById(R.id.tvSeasonEpisodes);
//Poster Corners
ivPoster.setClipToOutline(true);
}
}
}
I tried doing this:
if (season.getSeasonEpisodes() == 0) {
seasons.remove(position);
}
It does seem to hide the season which has no episodes but if a show has multiple seasons without episodes, my app crashes so I figured this isn't the right solution so any help is appreciated.
I suggest performing that removal logic in the constructor of the adapter rather than in onBind. onBind happens as the recycler view is finalising the details of each view holder immediately before it's shown to the user. You want to do as little as possible logic in here to keep the recycler view performant.
Inside the constructor (or even before the list is passed in) you should perform a loop and remove those items that don't meet the criteria before setting the instance variable.
It's been a long time since I wrote code in java and so I'd end up with unhelpful incorrect syntax if I tried to do it here.
My program is supposed to show a list of Apps and their data usage. If you click that app, it starts a new activity page and gives more information about that app. Everything works fine on the initial page and all apps show accurate information, but after I scroll down it messes up - the "more info" page shows a different app than the one I clicked.
I'm pretty sure the problem is around the onclick event not being binded to the holder? However I can't figure out what to do there. It should be noted that the problem is fixed if i get rid of the if(convertView == null)/else condition, but I know that is bad practice since we don't want to keep re-generating the previous items
Here is the getView code in my customAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final DataUsageModel model;
model = getItem(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.customlayout, null);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentMoreInfo = new Intent(view.getContext(),MoreInfoActivity.class);
intentMoreInfo.putExtra("wifiUsage",model.getWifiUsage());
intentMoreInfo.putExtra("mobileUsage",model.getMobileUsage());
intentMoreInfo.putExtra("appName",model.getName());
intentMoreInfo.putExtra("pname",model.getPname());
context.startActivity(intentMoreInfo);
}
});
holder.nameText = (TextView) convertView.findViewById(R.id.textViewWord);
holder.totalUsageText = (TextView) convertView.findViewById(R.id.textViewDescription);
holder.imageView = (ImageView) convertView.findViewById(R.id.iconImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.nameText.setText(model.getName());
long usageInMB = (model.getWifiUsage() + model.getMobileUsage())/(1024*1024);
holder.totalUsageText.setText(String.valueOf(usageInMB) + " MB");
holder.imageView.setImageDrawable(model.getImageRes());
return convertView;
}
public class ViewHolder {
TextView nameText;
TextView totalUsageText;
ImageView imageView;
}
Here is my code for the new activity started:
public class MoreInfoActivity extends AppCompatActivity {
TextView wifiUsage;
TextView mobileUsage;
ImageView icon;
TextView appName;
Button backButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.more_info_layout);
wifiUsage = findViewById(R.id.moreInfoWifi);
mobileUsage = findViewById(R.id.moreInfoMobile);
appName = findViewById(R.id.moreInfoAppName);
icon = findViewById(R.id.moreInfoIcon);
backButton = (Button)findViewById(R.id.moreInfoBack);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
appName.setText(getIntent().getStringExtra("appName"));
wifiUsage.setText("Wifi Usage: " + String.valueOf(getIntent().getLongExtra("wifiUsage",0)/(1024*1024)) + " MB");
mobileUsage.setText("Mobile Usage: " + String.valueOf(getIntent().getLongExtra("mobileUsage",0)/(1024*1024)) + " MB");
try {
icon.setImageDrawable(getPackageManager().getApplicationIcon(getIntent().getStringExtra("pname")));
}catch(PackageManager.NameNotFoundException e){
e.printStackTrace();
}
You should set OnClickListener for every iteration of the getView method, not just when convertView == null.
In your case, the current data position is not synced with the current view (holder) position for every getView iteration
Furthermore, it is a very bad practice to start the activity from the adapter itself.
The adapter role is to bind view to data, and that's all.
I have written a small app that has a ListView with a custom adapter. Each row contains some Buttons, which will change background color when clicked, and I got the list items to be clickable as well by putting
android:descendantFocusability="blocksDescendants"
in the xml of the list items. But now I have this weird bug where clicking on the list item reverts all clicked Buttons back to their original colorless state. How can I get the Buttons to keep their color?
Details:
Part of the custom adapter:
View.OnClickListener onButtonClicked = new View.OnClickListener() {
#Override
public void onClick(View button) {
View listItem = (View) button.getParent();
final long DBid = (long) listItem.getTag();//database ID
final Button b = (Button) button;
sqldataDataSource datasource = new sqldataDataSource(context);
datasource.open();
datasource.updateButton(DBid);
datasource.close();
b.setBackgroundColor(0xFF386F00);
}
};
As you can see, I change the background color AND change the database entry, so when the whole list is reloaded, the Button keeps its color (another part of my custom adapter):
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);
Button b = (Button) rowView.findViewById(R.id.HRlistB);
b.setOnClickListener(onButtonClicked);
if(!(values.get(i).getB().equals(""))){
b.setBackgroundColor(0xFF386F00);
}
return rowView;
}
This works fine when going to another activity and coming back to this one. The buttons are created colored as expected.
So my guess was that the list is recreated from the original listItem array when an item is clicked, which is why I tried to fix this by reloading my database, like so (from my activity):
#Override
protected void onStart() {
super.onStart();
datasource = new sqldataDataSource(this);
datasource.open();
listItems = datasource.getOnlyRoutes(id);//this works fine
Collections.sort(listItems, HallenRoute.vergleichen());
if (mListView == null) {
mListView = (ListView) findViewById(R.id.listViewHalle);
}
adapter=new customAdapter(this, listItems);
setListAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int pos, long nid) {
listItems.get(pos).increaseCount();
datasource.updateCountHR(listItems.get(pos));
listItems = datasource.getOnlyRoutes(id);//fix I tried, doesn't work
Collections.sort(listItems, HallenRoute.vergleichen());
adapter.notifyDataSetChanged();
}
});
}
But this doesn't work.
How can I get the ListView to either not reload on ItemClick or reload properly (i.e. from database)?
You don't have to reload the whole data for every Button click.
In your Button click you're just updating the data base and not your adapter dataset values, this is why you always get the old background color.
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);
Button b = (Button) rowView.findViewById(R.id.HRlistB);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View button) {
View listItem = (View) button.getParent();
final long DBid = (long) listItem.getTag();//database ID
final Button b = (Button) button;
sqldataDataSource datasource = new sqldataDataSource(context);
datasource.open();
datasource.updateButton(DBid);
datasource.close();
//b.setBackgroundColor(0xFF386F00); no need for this line, getView() method will take care of the background
//update your adapter dataset, eg: values.get(i).setB("newColor");
notifyDataSetChanged(); // to refresh your adapter
}
});
if(!(values.get(i).getB().equals(""))){
b.setBackgroundColor(0xFF386F00);
}
return rowView;
}
PS: It's better if you save your "database ID" in your Model object not as a View tag.
I'm trying to set an onClick listener on my ImageView in the Adapter of my GridView. However, weird thing happens: The content of the onClick function affects also some other Views in my GridView.
There is a good reason that I don't do the click listener on my GridView, so I need a solution for this via the ImageAdapter.
The logcat is called only once I click, but for some reason, other ImageViews are affected by this function.
Here's relevant code:
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
holder.tickImageView = (ImageView) convertView.findViewById(R.id.tickImageView);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
holder.imageViewLayout = (LinearLayout)convertView.findViewById(R.id.imageViewLayout);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView imageView = (ImageView)v;
int id = imageView.getId();
imageView.setVisibility(View.GONE);/*
if(!thumbnailsselection[id]){
Log.d(Global.TAG, "CLICK");
holder.tickImageView.setVisibility(View.VISIBLE);
holder.imageViewLayout.setBackgroundResource(R.drawable.imageview_selected);
thumbnailsselection[id] = true;
}
else{
holder.tickImageView.setVisibility(View.GONE);
holder.imageViewLayout.setBackgroundResource(R.drawable.imageview_unselected);
thumbnailsselection[id] = false;
}
*/
}
});
holder.imageView.setId(position);
holder.imageViewLayout.setId(position);
holder.tickImageView.setId(position);
holder.imageView.setImageBitmap(thumbnails[position]);
return convertView;
}
class ViewHolder {
ImageView imageView;
LinearLayout imageViewLayout;
ImageView tickImageView;
int id;
}
In baseAdapters, view are recycled. This means that if you set a view to invisible, you will add some other view invisible when you will scroll.
To avoid that, be sure to set again the visibility of yout view in the getView method:
holder.imageView.setVisibility(View.VISIBLE)
holder.imageView.setOnClickListener(new OnCl...
Also you will have to store each visibility state, in order to reassing to visible or invisible.
I have a GridView with custom View in it, which is a Button and a TextView. I defined the setOnItemClickListener but it looks like it never invoked, please see peaces of code below.
gridview = (GridView) findViewById(R.id.main_gridview);
gridview.setAdapter(new GridAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "gadsfadsf",
Toast.LENGTH_SHORT).show();
Log.d("Main", "onItemClick");
}
});
The marked answer is kind of a hack. Instead of setting an onclicklistener to the button just ensure, that the ButtonView and the TextView has the following property:
android:clickable="false"
I had the same issue. While I've not yet figured out why it never gets invoked, I can propose a workaround.
Instead of setting the onClickListener on your GridView, set it on the Button itself inside your GridAdapter, inside your getView() method.
That worked for me!
It could be that some items in your GridView are stealing focus. Try adding these attributes to any elements you have inside the grid:
android:focusable="false"
android:focusableInTouchMode="false"
Instead of setting the onClickListener on your GridView,
set it on the Button itself inside your GridAdapter, inside your getView() method.
That worked for me!
I had the same problem, the event grid.itemClickListener was never launched.
In my case I had two listeners: grid.itemClickListener and another clickListener attached to a Button within the item's layout.
After fiddling with the layout for a while, I realized that if there was a widget, within the item's layout, with focusable=true, then itemClickListener was never launched. The clickListener attached to the Button worked well though.
Maybe that was your case. Anyway, I think this information might be useful to other users running into the same problem.
Thanx to CodingUser
what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.
So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)
so what i m trying to say is:
public View getView(int position, View convertView, ViewGroup parent) {
//Here row is a view and we can set OnClickListener on this
final View row;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
//Here we inflate the layout to view (linear in my case)
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
//Now get the id or whatever needed
row.setId(position);
// Now set the onClickListener
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Clicked" + row.getId() + "!!",
Toast.LENGTH_SHORT).show();
}
});
return row;
}
You can set OnClick for view in Adapter of GridView .It work for me .
public View getView(final int position, View convertView, ViewGroup parent) {
ObjMenuVideo objListVideo = mListMenuVideo.get(position);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_video_of_kind, null);
holder.tv_number_views = (TextView) convertView
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_number_views.setText(String.valueOf(objListVideo.getViews()));
convertView.setId(position);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent menuVideoIntent = new Intent(mContext,
ActivityDetailVideo.class);
mContext.startActivity(menuVideoIntent);
}
});
return convertView;
}