Helo. I'm experiencing strange behavior with my app. It crashes on litsview item flip animation with this error:
06-02 17:26:16.748 22310-23383/com.dealy.android A/OpenGLRenderer﹕ Error: Ambient Vertex Buffer overflow!!! used 286, total 284
I don't use any of OpenGL libs or smthing, so it's strange. Also, crash only occurs on Nexus 6 with android 5.1.
The code of list item here:
package com.dealy.android.adapter;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.annotation.TargetApi;
import android.content.Context;
import android.location.Location;
import android.os.Build;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.squareup.picasso.Picasso;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import database.HelperFactory;
import com.dealy.android.DealyApplication;
import com.dealy.android.R;
import com.dealy.android.util.SimpleAnimationEndListener;
import dto.GeoPoint;
import dto.ImageDTO;
import special.dto.Special;
import venue.dto.Venue;
/**
* Created by Beyka on 13.01.2015.
*/
public class SpecialAdapter extends BaseAdapter {
private static final DecimalFormat DEFAULT_DISTANCE_FORMAT = new DecimalFormat("##0.0");
private final AnimatorSet fbo;
private final AnimatorSet fbi;
private Context context;
private LayoutInflater inflater;
private List<Special> values;
private SparseBooleanArray flipArray;
private GeoPoint lastLocation;
Tracker t;
public SpecialAdapter(Context context, List<Special> values, GeoPoint lastLocation) {
fbo = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.card_flip_right_out);
fbi = (AnimatorSet)AnimatorInflater.loadAnimator(context, R.animator.card_flip_right_in);
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.values = values;
flipArray = new SparseBooleanArray();
this.lastLocation = lastLocation;
}
public void setLastLocation(GeoPoint lastLocation) {
this.lastLocation = lastLocation;
notifyDataSetChanged();
}
#Override
public int getCount() {
return values.size();
}
#Override
public Special getItem(int position) {
return values.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_special, parent, false);
holder = new Holder(convertView);
convertView.setTag(holder);
} else{
holder = (Holder)convertView.getTag();
}
Special special = getItem(position);
fillHolder(holder, special, position);
return convertView;
}
private void fillHolder(final Holder holder, Special special, final int position) {
holder.front.setVisibility(flipArray.get(position, false) ? View.GONE : View.VISIBLE);
holder.back.setVisibility(!flipArray.get(position, false) ? View.GONE : View.VISIBLE);
t = ((DealyApplication) context.getApplicationContext()).getTracker(DealyApplication.TrackerName.APP_TRACKER);
holder.more.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View v) {
t.send(new HitBuilders.EventBuilder().setCategory("Test Catogory").setAction("Нажатие на подробности").build());
fbi.setTarget(holder.back);
fbo.addListener(new SimpleAnimationEndListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onAnimationEnd(Animator animation) {
flipArray.put(position, true);
holder.front.setVisibility(View.GONE);
holder.back.setVisibility(View.VISIBLE);
fbi.start();
}
});
fbo.setTarget(holder.front);
fbo.start();
}
});
holder.goBack.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View v) {
t.send(new HitBuilders.EventBuilder().setCategory("Test Catogory").setAction("Нажатие на вернуться").build());
fbi.setTarget(holder.front);
fbo.addListener(new SimpleAnimationEndListener() {
#Override
public void onAnimationEnd(Animator animation) {
flipArray.put(position, false);
holder.back.setVisibility(View.GONE);
holder.front.setVisibility(View.VISIBLE);
fbi.start();
}
});
fbo.setTarget(holder.back);
fbo.start();
}
});
holder.backTitle.setText(special.getName());
holder.description.setText(special.getDescription());
if(special.getImageLink() == "" || special.getImageLink() == null){
for(ImageDTO image : special.getImages()){
if(image.getWidth() >= 640){
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + image.getUrl()).fit().into(holder.logo);;
}
}
}
else {
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.getImageLink()).fit().into(holder.logo);
}
Venue venue = new Venue();
try {
venue = HelperFactory.getHelper().getVenueDAO().getVenueById(special.getVenues().get(0));
} catch (SQLException e) {
e.printStackTrace();
}
final float[] distanceInMeters = new float[1];
try {
Location.distanceBetween(lastLocation.getLat(), lastLocation.getLng(), special.venue.getLat(), special.venue.getLng(), distanceInMeters);
} catch(NullPointerException e){
}
double distanceInKm = distanceInMeters[0] / 1000.0;
holder.distance.setText(context.getString(R.string.kilometers, DEFAULT_DISTANCE_FORMAT.format(distanceInKm)));
holder.backDistance.setText(context.getString(R.string.kilometers, DEFAULT_DISTANCE_FORMAT.format(distanceInKm)));
if(distanceInKm == 0){
holder.distance.setVisibility(TextView.GONE);
holder.backDistance.setVisibility(TextView.GONE);
}
if(special.venue.getLogoLink() != null) {
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getLogoLink()).fit().into(holder.place);
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getLogoLink()).fit().into(holder.backPlace);
}
else{
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getImages().get(0)).fit().into(holder.place);
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getImages().get(0)).fit().into(holder.backPlace);
}
}
class Holder {
#InjectView(R.id.front)
View front;
#InjectView(R.id.logoImage)
ImageView logo;
#InjectView(R.id.place)
ImageView place;
#InjectView(R.id.distanceText)
TextView distance;
#InjectView(R.id.more)
Button more;
#InjectView(R.id.back)
View back;
#InjectView(R.id.goBack)
Button goBack;
#InjectView(R.id.backTitle)
TextView backTitle;
#InjectView(R.id.backPlace)
ImageView backPlace;
#InjectView(R.id.backDistance)
TextView backDistance;
#InjectView(R.id.description)
TextView description;
#InjectView(R.id.card)
RelativeLayout card;
private Holder(View view) {
ButterKnife.inject(this, view);
}
}
}
i have resolve this, the reason is : you had open the hardwareAccelerated attribute , you must close it in your manifest.xml like this
android:hardwareAccelerated = "false"
......
>
This problem is caused by more than 6.0 of Android's system automatically open the default hardware acceleration, but sometime we had no need to used it, Because most of the hardware capacity is still insufficient.
hope can help you!
#high sea company `s answer is right.
Thank you!!!
You can try like this:
in AndroidManifest.xml application:
<application android:hardwareAccelerated="false" ...>
in Activity :
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
In layout XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="2dp"
android:layerType="software" //**close hardwareAccelerated**
android:paddingRight="2dp" >
in View:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Related
In my android app I'm trying to implement mopub native ads. Before using these native ads my app was functioning correctly as expected. Now it gives outofbound exceptions and incorrect items when list item is clicked. I'm using recyclerview and using infinite scrolling, it gets item from my api and displays item correctly, but when clicking on item it gives incorrect item.
Here is my recyclerview adapter below.
package com.example.adapters;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.MyApplication;
import com.example.R;
import com.example.events.ErrorLoadingFeed;
import com.example.events.FeedLoaded;
import com.example.events.LoadMoreFeed;
import com.example.model.Post;
import com.example.model.Pagination;
import com.example.ui.InfoActivity;
import com.example.utils.MyAppConstants;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import java.util.List;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Header;
import retrofit.client.Response;
/**
* Created by starwar on 08/09/15.
*/
public class PostRecycleAdapter extends RecyclerView.Adapter<com.example.adapters.PostRecycleAdapter.PostViewHolder> implements Callback<List<Post>> {
private Context mContext;
private List<Post> mPosts;
// Allows to remember the last item shown on screen
private int lastPosition = -1;
// Pagination
private Pagination mPagination;
public PostRecycleAdapter(Context context, List<Post> posts) {
mContext = context;
mPosts = posts;
}
#Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
PostViewHolder viewHolder = new PostViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(PostViewHolder holder, int position) {
holder.bindPost(mPosts.get(position));
// Here you apply the animation when the view is bound
setAnimation(holder.mPostName, position);
//check for last item
if ((position >= getItemCount() - 1)) {
// Loading next set of list items on scroll
if (mPagination != null && !mPagination.getOutOfRange() && mPagination.getNextPage() != null){
MyApplication.bus.post(new LoadMoreFeed(mPagination));
}
}
}
#Override
public int getItemCount() {
return mPosts == null ? 0 : mPosts.size();
}
/**
* Here is the key method to apply the animation
*/
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
public class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mPostName;
public ImageView mAnchorImage;
public PostViewHolder(View itemView) {
super(itemView);
mPostName = (TextView) itemView.findViewById(R.id.postName);
mAnchorImage = (ImageView) itemView.findViewById(R.id.anchorImage);
itemView.setOnClickListener(this);
}
public void bindPost(Post post) {
mPostName.setText(post.getName());
String postImage = post.getPostImage();
Picasso.with(mContext)
.load(postImage)
.placeholder(R.drawable.avatar_empty)
.into(mAnchorImage);
}
#Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
Post post = mPosts.get(position);
int postId = post.getId();
String postName = post.getName();
Intent intent = new Intent(mContext, InfoActivity.class);
intent.putExtra(MyAppConstants.POST_ID, postId);
intent.putExtra(MyAppConstants.POST_NAME, postName);
mContext.startActivity(intent);
}
}
#Override
public void success(List<Post> posts, Response response) {
if (mPosts == null) {
mPosts = posts;
} else {
mPosts.addAll(posts);
}
notifyDataSetChanged();
List<Header> headerList = response.getHeaders();
for(Header header : headerList) {
if ("X-Pagination".equals(header.getName())) {
Gson gson = new Gson();
mPagination = gson.fromJson(header.getValue(), Pagination.class);
}
}
MyApplication.bus.post(new FeedLoaded(mPagination));
}
#Override
public void failure(RetrofitError error) {
Log.d("Call", " : Failed => " + error);
MyApplication.bus.post(new ErrorLoadingFeed(error));
}
public void clearData() {
if (mPosts != null) {
mPosts.clear();
notifyDataSetChanged();
}
}
public List<Post> getPosts() {
return mPosts;
}
}
And I'm using guide given here to show ads
https://dev.twitter.com/mopub/native/native-android-sdk-integration
Please help. what I can/should do to resolve this.
Thanks in advance.
I know this is an old question, but I was looking for the answer for a couple of hours. Probably I'll save someone's time with this solution.
If you will look carefully documentation here: https://github.com/mopub/mopub-android-sdk/wiki/Native-Ads-with-Recycler-View you will find this:
If you register any data observers on your original Adapter, you should instead register them on the MoPubRecyclerAdapter so they will receive messages with the adjusted position of content items. If you do not do this, the positions you receive for insertion and removal messages may be inaccurate. Be sure to check isAd(position) and use MoPubRecyclerAdapter#getOriginalPosition if you need the position of the item in your local adapter.
So, to get real position, it is necessary to use MoPubRecyclerAdapter.getOriginalPosition(position_in_your_adapter) method.
I am writing a code in Android to refresh a Gridview in every 2 mins. I searched a lot in the web but i don't find any specific technique to do it.
Java Code
MainActivity.java
package com.example.mycustomgridrefresh;
import java.util.ArrayList;
import android.os.Bundle;
import android.widget.GridView;
import android.app.Activity;
public class MainActivity extends Activity
{
GridView gd;
MyGrid mg;
ArrayList<String> abc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gd = (GridView)findViewById(R.id.gd);
abc = new ArrayList<String>();
for(int i=0;i<100;i++)
{
abc.add(String.valueOf(i));
}
mg = new MyGrid(this,this,abc);
gd.setAdapter(mg);
}}
MyGrid.java
package com.example.mycustomgridrefresh;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyGrid extends BaseAdapter
{
ArrayList<String> abc;
Activity activity;
public MyGrid(Activity activity , Context cont,ArrayList<String> abc)
{
super();
this.abc = abc;
this.activity = activity;
}
#Override
public int getCount()
{
return abc.size();
}
#Override
public Object getItem(int arg0)
{
return abc.get(arg0);
}
#Override
public long getItemId(int arg0)
{
return 0;
}
public class ViewHolder
{
public TextView txt;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(arg1==null)
{
view = new ViewHolder();
arg1 = inflator.inflate(R.layout.mygrid, null);
view.txt = (TextView) arg1.findViewById(R.id.txt);
arg1.setTag(view);
}
else
{
view = (ViewHolder) arg1.getTag();
}
view.txt.setText(abc.get(arg0));
return arg1;
}
}
This above Grid is printing 100 numbers is 10 rows & columns. I want to change the value of the grid cells for each 2mins. The value should to change to +1.
Please suggest me some good solution.
Thanks in Advance !!!
To refresh the values you need to call in MyGrid:
notifyDataSetChanged()
To do any task each 2 mins you need to create other thread, something like this:
public void startRunnable(){
Runnable r = new Runnable() {
#Override
public void run() {
while(needToRefresh){
//add one...
this.notifyDataSetChanged();
Thread.sleep(2000);
}
}
};
this.activity.runOnUiThread(r);
}
Don't forget to stop the needToRefresh condition onPause, onStop.
EDIT: Because notifyDataSetChanged() need to be call in UI thread, you need to call runnable using this.activity.runOnUiThread
See there: How to use notifyDataSetChanged() in thread
I have a problem with a listView that I made that has a ratingbar and a texview element.Its a very simple program that is meant to displays the list and stores the value of the stars that the user enters into the ratingBar. Unfortunately for some reason when I scroll up and down the rows that go out of the screen, when scrolled back in have their stars changed back to their intial value.The code is below. If anyone could help that would be great. Thanks.
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends Activity implements RatingBar.OnRatingBarChangeListener {
private String[] textList = {"Pikachu","Raichu","Raticate","Ratata","Absol","Abra","Kadabra","Pidgey","Mime","Meowth","Mew","Squirtle","Blastoise","Goop","Kilobyte","Armada"};
private ArrayList<String> list = new ArrayList<String>();
ListView listView;
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_main);
for(int i=0; i<textList.length; i++)
{
list.add(i,textList[i]);
}
listView = (ListView)findViewById(R.id.listRow);
listView.setAdapter(new setListView(list));
}
class setListView extends ArrayAdapter<String>
{
public setListView(ArrayList<String> list)
{
super(MainActivity.this, R.layout.custom_layout, R.id.text, list);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.custom_layout, parent, false);
}
RatingBar ratingBar = (RatingBar)row.findViewById(R.id.rating);
RatingBarInfo rating;
rating = (RatingBarInfo)ratingBar.getTag();
if(rating == null)
{
rating = new RatingBarInfo(position);
ratingBar.setTag(rating);
}
ratingBar.setRating(rating.getRating());
TextView text = (TextView)row.findViewById(R.id.text);
text.setText(list.get(position));
return(row);
}
}
class RatingBarInfo
{
float rating;
int position;
public RatingBarInfo(int positionTemp)
{
rating = 2.0f;
position = positionTemp;
}
public void setRating(float ratingTemp)
{
rating = ratingTemp;
}
public float getRating()
{
return rating;
}
public int getPosition()
{
return position;
}
}
public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser)
{
RatingBarInfo ratingChange = (RatingBarInfo)ratingBar.getTag();
ratingChange.setRating(rating);
ratingBar.setTag(ratingChange);
((ArrayAdapter<String>)listView.getAdapter()).notifyDataSetChanged();
}
}
You forgot to set OnRatingBarChangeListener on your RatingBar.
Just add this line into your getView() method and it should works well.
ratingBar.setOnRatingBarChangeListener(MainActivity.this)
I have defined onCheckedChanged for the checkbox in my listview.
When i click on the check box to check / uncheck it this function is getting invoked.
But when i setthe state of the check box from code like
check.setChecked(true);
the onCheckedChanged is not getting invoked.
Please help.
Adapter file :
package com.idg.project.adapters;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.idg.project.R;
import com.idg.project.activities.ScanListActivity;
import com.idg.project.activities.SearchResultActivity;
import com.idg.project.adapters.WishListAdapter.ViewHolder;
import com.idg.project.entity.ScannedProduct;
public class ScanListAdapter extends BaseAdapter {
private Context context;
private List<ScannedProduct> productList;
protected LayoutInflater mInflater;
Button showOrHideButton;
static public int count = 0;
String barcodeForSelectedRow;
String formatForSelectedRow;
OnItemClickListener rowListener;
Activity parentActivity;
boolean isWishList;
public ScanListAdapter(Context context, List<ScannedProduct> objects,
Button button, Activity parentActivity) {
super();
this.productList = objects;
this.context = context;
this.mInflater = LayoutInflater.from(context);
showOrHideButton = button;
this.parentActivity = parentActivity;
this.isWishList = isWishList;
}
public int getCount() {
return productList.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int pos = position;
Log.i("checkboxflag at : ", pos+"is"+(productList.get(pos).getCheckboxflag()));
Log.i("getview : fresh", "getview"+pos);
convertView = mInflater.inflate(R.layout.product_list_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.productid);
holder.text1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(parentActivity,
SearchResultActivity.class);
intent.putExtra("barcode", productList.get(pos)
.getBarcode());
intent.putExtra("format", productList.get(pos).getFormat());
intent.putExtra("IsScan", false);
Log.i("", "" + productList.get(pos).getBarcode());
parentActivity.startActivity(intent);
Log.i("", "" + pos);
}
});
holder.text2 = (TextView) convertView.findViewById(R.id.price);
// holder.text2.setOnClickListener(listener);
holder.image = (ImageView) convertView
.findViewById(R.id.productimageid);
convertView.setTag(holder);
// holder.image.setOnClickListener(listener);
holder.text1.setText(productList.get(position).getTitle());
holder.text2.setText(productList.get(position).getPrice().toString());
if (productList.get(position).getSmallImage() != null) {
byte[] bb = (productList.get(position).getSmallImage());
holder.image.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0,
bb.length));
} else {
holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.highlight_disabled);
}
// holder.image.setImageBitmap(Utils.loadBitmap(productList.get(position).getSmallImage()));
final CheckBox check = (CheckBox) convertView
.findViewById(R.id.checkbox);
check.setClickable(true); // to remove anything carried over from prev convert view
if(productList.get(pos).getCheckboxflag()==1)
{
Log.i("CheckBox set checked",""+pos);
check.setChecked(true);
}
else{
Log.i("CheckBox set unchecked",""+pos);
check.setChecked(false);
}
setWishListItemsInScanList(pos, convertView);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i("OnclickListener","Current Position"+pos);
if (check.isChecked()
&& productList.get(pos).getWishListFlag() == 0) {
if(check.isClickable()){
Log.i("CheckBox check",""+pos);
ScanListActivity.updateCheckBoxSelection(1, pos);
ScanListAdapter.count++;
}
} else if (!check.isChecked()
&& productList.get(pos).getWishListFlag() == 0){
if(check.isClickable()){
ScanListActivity.updateCheckBoxSelection(0, pos);
ScanListAdapter.count--;
Log.i("CheckBox UNcheck",""+pos);
}
}
if (ScanListAdapter.count == 0) {
// showOrHideButton.setClickable(false);
// showOrHideButton.setVisibility(View.GONE);
showOrHideButton.setEnabled(false);
} else {
// showOrHideButton.setVisibility(View.VISIBLE);
showOrHideButton.setEnabled(true);
}
}
});
return convertView;
}
private void setWishListItemsInScanList(int pos, View convertView) {
if (productList.get(pos).getWishListFlag() == 1) {
Log.i("CheckBox set checked from wish list",""+pos);
CheckBox check = (CheckBox) convertView.findViewById(R.id.checkbox);
check.setClickable(false);
check.setChecked(true);
}
}
static class ViewHolder {
TextView text1;
ImageView image;
TextView text2;
}
}
List activity file :
package com.idg.project.activities;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.idg.project.R;
import com.idg.project.adapters.WishListAdapter;
import com.idg.project.adapters.ScanListAdapter;
import com.idg.project.entity.ScannedProduct;
import com.idg.project.services.ScannedProductDataAccessManager;
public class ScanListActivity extends BaseActivity {
static Button scanlist;
ScanListAdapter listAdapter;
static List<ScannedProduct> productList;
/* Notes for the Developer :
* For tracking the checked items Checkboxflag
* is maintained.
* Point1 : Select all will just set this flag in the local list and then call notifyDatachange of the adapter
* within adapter the check box is set or reset based on this flag for each row
*
* Point 2: When individual rows are selected , there is an onclick of the check box is invoked
* Here the Checkboxflag of the local list is set /unset . Also we need a way to knpw the select all button is
* to enabled or diabled. for that Count variable is updated here.
* Now Important point is these two actions shoulnt be taking place if the checkbox state change due to select all
* So there is a special check of isclickable in the onclicklistener
*
* Point 3: In scan list the items in the wish list are to be marked. This again needs special logic.
* This is done in the adapter code by checking all the rows whose wishListFlag is 1 and making it non clickable
*
* Important : Listview has the concept of ViewGroup and each view group is usually the rows fitting in the display screen
* so when we scroll, the viewGropu changes.
* Convertview is get reused for view groups. So need to careful undesired values that will be carried to next viewgroup*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.scan_list);
productList = new ArrayList<ScannedProduct>();
productList = getProductList();
for(int i=0;i<productList.size();i++){
Log.i("checkboxflag at : ", i+"is"+(productList.get(i).getCheckboxflag()));
}
final ListView lv = (ListView) findViewById(R.id.list);
scanlist = (Button) findViewById(R.id.addtowishlist);
scanlist.setEnabled(false);
listAdapter = new ScanListAdapter(this, productList, scanlist, this);
lv.setAdapter(listAdapter);
}
private List<ScannedProduct> getProductList() {
List<ScannedProduct> productList = new ArrayList<ScannedProduct>();
ScannedProductDataAccessManager productDataBaseManager = new ScannedProductDataAccessManager(
getApplicationContext());
String[] colList = { "title", "smallImage", "price" };
productList = productDataBaseManager.fetchAllProducts();
return productList;
}
static boolean selectFlag = false;
public void selectAll(View view) {
ListView listView = (ListView) findViewById(R.id.list);
view = findViewById(R.id.select_all);
if (selectFlag == false) {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
productList.get(i).setCheckboxflag(1);
}
view.setBackgroundResource(R.drawable.login_remme_dwn_btn);
selectFlag = true;
TextView text=(TextView) findViewById(R.id.select);
text.setText("Unselect All");
scanlist.setEnabled(true);
} else {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
productList.get(i).setCheckboxflag(0);
}
view.setBackgroundResource(R.drawable.login_remme_up_btn);
selectFlag = false;
TextView text=(TextView) findViewById(R.id.select);
text.setText("Select All");
scanlist.setEnabled(false);
}
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged(); // we are only setting the flags in the list
// so need to notify adapter to reflect same on checkbox state
//listView.refreshDrawableState();
}
public void addToWishList(View view) {
ListView listView = (ListView) findViewById(R.id.list);
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
ScannedProduct product = productList.get(i);
if (product.getWishListFlag() == 0 && product.getCheckboxflag()==1) {
product.setWishListFlag(1);
new ScannedProductDataAccessManager(getApplicationContext())
.updateProduct(product, "title",
new String[] { product.getTitle() });
product.setCheckboxflag(0);
//ScanListAdapter.count--;
}
Log.i("ScanList selected", product.getTitle());
}
Toast.makeText(getApplicationContext(),
"Added selected items to Wish List", Toast.LENGTH_SHORT).show();
scanlist.setEnabled(false);
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
}
static public void updateCheckBoxSelection(int flag,int pos){ // when individual row check box is checked/ unchecked
// this fn is called from adapter to update the list
productList.get(pos).setCheckboxflag(flag);
}
}
Since your checkbox is inside listview, so you need to call notifyDataSetChanged method on your list's adapter to refresh it's contents.
update
instead of ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();, try calling listAdapter.notifyDataSetChanged();
I got the answer / bug in my code
i am not reusing convertview so its every time a new holder.
I am changing the flag of the checkbox and then assigning a statechange listener for the checkbox
thus its not getting invoked
when i changed the order to assign checkchangelistener before actually changing the state , its working as expected. The listener is getting called.
thanks all of you
I have a ListView that has one image and two lines of texts for each element (organized by a RelativeLayout). It works ok, but it's too slow and I know where the problem comes from!
This is the getView() method for the custom adapter that I'm using:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.cover = (ImageView) convertView.findViewById(R.id.app_icon);
mViewHolder.title = (TextView) convertView.findViewById(R.id.selection);
mViewHolder.description = (TextView) convertView.findViewById(R.id.app_short_description);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
// Here is the origin of the issue !
final Feed currentFeed = getItem(position);
mViewHolder.title.setText(currentFeed.getTitle());
mViewHolder.description.setText(currentFeed.getDescription());
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, currentFeed.getThumbnailUrl());
}
} catch(Exception e) {
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView title;
TextView description;
ImageView cover;
}
So I have done some manual benchmarking and it appears that allocating an instance of Feed is the source of this slowness:
final Feed currentFeed = getItem(position);
I know this because I have written another version of this to compare the two:
// Here is the origin of the issue !
//final Feed currentFeed = getItem(position);
mViewHolder.title.setText("Title");
mViewHolder.description.setText("Description");
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, "some url");
}
} catch(Exception e) {
e.printStackTrace();
}
This one was way smoother (even with the downloadThumbnail() method working).
I also precise that there are only 15 items on my ListView.
I know that allocating objects is very expensive because of garbage collection but I can't any other way to do it!
Any idea?
Thanks!
EDIT
Don't mind too much about the downloadThumbnail() method, it already does some caching. And actually even without any picture, it's still slow.
When user scrolls the list, getView gets called on the adapter. Make sure that you dont do same things repeatedly, for example generating thumbnail. If number of items is limited (for example video content), then you can create all views and keep it ready for get view. Otherwise you may have to implement cacheing.
Below code shows an adapter and listView implementation, where in all listviews are created and stored in memory. Since this is meant for video browsing, memory does not pose any issue. (limited number of content, max 100)
Video List Adapter
import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class VideoListAdapter extends BaseAdapter {
private Context mContext = null;
private HashMap<String, VideoListItem> mHashedItems = new HashMap<String, VideoListItem>();
private static final String TAG = "VideoListAdapter";
public static final int VIDEO_CONTENT_ID = 0;
public static final int VIDEO_CONTENT_TITLE = 1;
public static final int VIDEO_CONTENT_DURATION = 2;
public static final int VIDEO_CONTENT_RESOLUTION = 3;
public static final int VIDEO_CONTENT_MIME = 4;
private Cursor mCursorForVideoList = null;
private ContentResolver mContentResolver = null;
private int mListCount = 0;
VideoListAdapter(Context context, ContentResolver cr) {
mContext = context;
mContentResolver = cr;
Log.i(TAG, "In the Constructor");
mCursorForVideoList =
mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.TITLE,
MediaStore.Video.VideoColumns.DURATION,
MediaStore.Video.VideoColumns.RESOLUTION
},
null,
null,
null);
mListCount = mCursorForVideoList.getCount();
}
#Override
public int getCount() {
return mListCount;
}
#Override
public Object getItem(int arg0) {
return getVideoListItem(arg0);
}
#Override
public long getItemId(int position) {
//Log.i(TAG, "position : " + position);
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Log.i(TAG, "GetView :: Position : " + position);
return getVideoListItem(position);
}
private VideoListItem getVideoListItem(int position)
{
//Log.i(TAG, "getVideoListItem :: Position : " + position);
String key = Integer.toString(position);
VideoListItem item = mHashedItems.get(key);
if(item == null)
{
//Log.i(TAG, "New getVideoListItem :: Position : " + position);
mCursorForVideoList.moveToPosition(position);
mHashedItems.put(key, new VideoListItem(mContext, mContentResolver, mCursorForVideoList));
}
return mHashedItems.get(key);
}
};
Video List View
import java.util.Formatter;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
class VideoListItem extends LinearLayout
{
private static final String TAG = "VideoListAdapter";
private ImageView mThumbnail = null;
private TextView mDuration = null;
private TextView mTitle = null;
private TextView mResolution = null;
private LayoutInflater mLayoutFactory = null;
private long mContentId = 0;
public VideoListItem(Context context, ContentResolver cr, Cursor cursor) {
super(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.setMargins(10, 10, 10, 10);
mLayoutFactory = LayoutInflater.from(context);
View thisView = mLayoutFactory.inflate(R.layout.videolistitem, null);
addView(thisView);
mThumbnail = (ImageView) findViewById(R.id.thumbnail);
mDuration = (TextView) findViewById(R.id.DDuration);
mTitle = (TextView) findViewById(R.id.DTitle);
mResolution = (TextView) findViewById(R.id.DResolution);
mThumbnail.setLayoutParams(new LinearLayout.LayoutParams(144, 144));
Resources r = this.getResources();
Bitmap bMap = MediaStore.Video.Thumbnails.getThumbnail(cr, cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID), MediaStore.Video.Thumbnails.MINI_KIND, null);
if(bMap != null)
{
mThumbnail.setImageBitmap(Bitmap.createScaledBitmap(bMap, 128, 128, true));
}
else
{
mThumbnail.setImageDrawable(r.getDrawable(R.drawable.error));
}
mThumbnail.setPadding(16, 16, 16, 16);
mTitle.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_TITLE));
mTitle.setSingleLine();
mTitle.setTextColor(Color.GREEN);
mResolution.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_RESOLUTION));
mResolution.setSingleLine();
mResolution.setTextColor(Color.RED);
mDuration.setText(stringForTime(cursor.getInt(VideoListAdapter.VIDEO_CONTENT_DURATION)));
mDuration.setSingleLine();
mDuration.setTextColor(Color.CYAN);
mContentId = cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID);
}
public long getContentId()
{
return mContentId;
}
private StringBuilder mFormatBuilder = null;
private Formatter mFormatter = null;
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
};
Shash
Don't allocate or store the Feed object in your View holder and instead only store the position (position). When you need to reference the object then grab the reference index from the ViewHolder and act accordingly.
Edit
Of course I missed that you're using the object later on... You might also create a number of minimal, static methods for your Feed object that only return specific things, such as the title, etc. Then call these methods in your getView method to set the UI elements without full creation of the Feed itself.