I have an ImageView in a ListView, the ImageView has a default image and while rendering, I asynchronously download images and load them into ImageViews. It works perfectly, but when I scroll down two items and scroll back, then the default images in the first and second row changing to the image of the sixth and seventh row.
I read these topics, but didn't find the solution.
Image change when i scroll gridview
My images changing during scroll in listview android
ListView images changing During Scroll
public class RssAdapter extends BaseAdapter {
private final List<SyndEntry> items;
private static Context context;
private static Map<String, Bitmap> mBitmapCache = new HashMap<String, Bitmap>();
public RssAdapter(Context context, List<SyndEntry> items) {
this.items = items;
this.context = context;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int id) {
return id;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = View.inflate(context, R.layout.rss_item, null);
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemPubDate = (TextView) convertView.findViewById(R.id.itemPubDate);
holder.itemImg = (ImageView) convertView.findViewById(R.id.itemImg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemTitle.setText(items.get(position).getTitle());
holder.itemPubDate.setText(items.get(position).getPublishedDate().toString());
List<SyndEnclosure> encls = items.get(position).getEnclosures();
if(!encls.isEmpty()){
holder.imageUrl = encls.get(0).getUrl();
}
if (!encls.isEmpty() && holder.imageUrl != null && !holder.imageUrl.equals("null")) {
holder.setImage(holder.imageUrl);
}
return convertView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemPubDate;
ImageView itemImg;
String imageUrl;
public void setImage(String imageUrl) {
this.imageUrl = imageUrl;
Bitmap imageBitmap = mBitmapCache.get(imageUrl);
if(imageBitmap!=null){
itemImg.setImageBitmap(imageBitmap);
} else {
AsyncHttpClient client = new AsyncHttpClient();
client.get(imageUrl, null, fileHandler);
}
}
FileAsyncHttpResponseHandler fileHandler = new FileAsyncHttpResponseHandler(context) {
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, File response) {
Bitmap imageBitmap = BitmapFactory.decodeFile(response.getPath());
itemImg.setImageBitmap(imageBitmap);
mBitmapCache.put(imageUrl, imageBitmap);
}
};
}
I tried to set a boolean variable to check whether my image was set or not and also tried to use SharedPreferences, but this part of my code didn't run when the image changing.
if(!encls.isEmpty()){
holder.imageUrl = encls.get(0).getUrl();
}
if (!encls.isEmpty() && holder.imageUrl != null && !holder.imageUrl.equals("null")) {
holder.setImage(holder.imageUrl);
}
It seems I solved the problem finally, I post the solution for the future. So, at this part of code
} else {
holder = (ViewHolder) convertView.getTag();
}
the holder object is not that which has to be rendered for the given row. Thus we have to set the parameters of the holder object for the required values. And it was ok, but I didn't set the image related parameters to null if there was no image. So, the two else part were missing:
if(!encls.isEmpty()){
holder.imageUrl = encls.get(0).getUrl();
} else {
holder.imageUrl = null;
}
if (!encls.isEmpty() && holder.imageUrl != null && !holder.imageUrl.equals("null")) {
holder.setImage(holder.imageUrl);
} else {
holder.itemImg.setImageResource(R.mipmap.news_icon);
}
Related
I have a little bit problem.In my activity there is two Adapter one is for color selection and another is for size selection. While i clicked one of the item of color then recently the available size adapter should be change but i got problem in size adapter. it changes only when i click the size item. I research and try to solve problem but it doesnt works for me.
Here is my code.
AddToCartActivity.java
public class AddToCartActivity extends BaseActivity{
#Override
protected int getLayout() {
return R.layout.activity_add_to_cart;
}
#Override
protected void init() {
//api called here
}
// response of api
#Override
public void productDetail(ProductCommonModel productCommonModel,
ArrayList<ProductChildModel> productChildModels, HashMap<Integer,
ArrayList<ChildAttributeModel>> childWithAttribute, HashMap<Integer,
ArrayList<ChildImageModel>> childWithImages,
ArrayList<com.hazesoft.dokan.singleproductdetail.model.ColorModel>
colorModels, ArrayList<SizeModel> sizeModels,
ArrayList<RelatedProductModel> relatedProductModels) {
this.productCommonModel = productCommonModel;
this.productChildModels = productChildModels;
this.childWithAttribute = childWithAttribute;
this.childWithImages = childWithImages;
this.colorModels = colorModels;
this.sizeModels = sizeModels;
this.relatedProductModels = relatedProductModels;
tvProductName.setText(productCommonModel.getName());
if (productCommonModel.getSpecialPrice() == 0) {
tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
tvDiscount.setVisibility(View.GONE);
tvSpecialPrice.setVisibility(View.GONE);
} else {
tvSpecialPrice.setText(getString(R.string.rs) + productCommonModel.getSpecialPrice());
tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
tvSellingPrice.setPaintFlags(tvSellingPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tvDiscount.setText(productCommonModel.getDiscount() + getString(R.string.percentage));
}
setChildDetail(childWithAttribute, productChildModels);
setColorModel(colorModels);
setSizeModel(sizeModels);
quantity = Integer.parseInt(tvQuantityCart.getText().toString());
}
// setcolor adapter
private void setColorModel(ArrayList<ColorModel> colorModels) {
MyColorGridViewAdapter adapter = new MyColorGridViewAdapter(this, colorModels);
gvColor.setAdapter(adapter);
gvColor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelectedPostion(position);
adapter.notifyDataSetChanged();
}
});
}
// set size adapter
private void setSizeModel(ArrayList<SizeModel> sizeModels) {
sizeCustomModels = new ArrayList<>();
for(int i=0;i<sizeModels.size();i++){
sizeCustomModels.add(new SizeCustomModel(sizeModels.get(i).getAttName(),0));
}
setCustomSizeModelToAdapter(sizeCustomModels);
}
// this is code when i click color and change the size adapter but size doesnt change recently only changes when i click any item of the size
public void getSelectedC0lor(String color) {
selectedColor = color;
selectedSize=null;
sizeCustomModels = new ArrayList<>();
availableSize = new ArrayList<>();
for (int i = 0; i < skuColorSIzeList.size(); i++) {
if (skuColorSIzeList.get(i).getColor().equals(selectedColor)) {
availableSize.add(skuColorSIzeList.get(i).getSize());
}
}
for(int i=0;i<sizeModels.size();i++){
String size = null;
int status=0;
for(int j=0;j<availableSize.size();j++){
if(sizeModels.get(i).getAttName().equals(availableSize.get(j))){
size = sizeModels.get(i).getAttName();
status = 1;
break;
}else {
size = sizeModels.get(i).getAttName();
status = 0;
}
}
sizeCustomModels.add(new SizeCustomModel(size,status));
}
sizeRecylerAdapter.getNewModel(sizeCustomModels);
/*sizeRecylerAdapter = new MyCustomSizeRecylerAdapter(sizeCustomModels,this);
rvSize.setAdapter(sizeRecylerAdapter);
sizeRecylerAdapter.notifyDataSetChanged();*/
/*setCustomSizeModelToAdapter(sizeCustomModels);*/
}
}
MyColorGridViewAdapter.java
public class MyColorGridViewAdapter extends BaseAdapter {
Context context;
List<ColorModel> colorModelList;
String select_color;
boolean ch =false;
int checkPosition = -1;
public MyColorGridViewAdapter(Context context, List<ColorModel> colorModelList) {
this.context = context;
this.colorModelList = colorModelList;
}
public void setSelectedPostion(int postion){
this.checkPosition = postion;
}
#Override
public int getCount() {
return colorModelList.size();
}
#Override
public Object getItem(int position) {
return colorModelList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.custom_color_list_item,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(colorModelList.get(position).getImage()).into(holder.ivImage);
holder.tvColorName.setText(colorModelList.get(position).getAttName());
if(checkPosition==position){
holder.ivChecked.setVisibility(View.VISIBLE);
select_color = colorModelList.get(position).getAttName();
if( context instanceof AddToCartActivity){
((AddToCartActivity) context).getSelectedC0lor(select_color);
}
}else {
holder.ivChecked.setVisibility(View.GONE);
}
if(colorModelList.size()==1){
holder.ivChecked.setVisibility(View.VISIBLE);
select_color = colorModelList.get(position).getAttName();
if( context instanceof AddToCartActivity){
((AddToCartActivity) context).getSelectedC0lor(select_color);
}
}
return convertView;
}
class ViewHolder{
#BindView(R.id.view)
LinearLayout view;
#BindView(R.id.tv_color_name)
TextViewHelper tvColorName;
#BindView(R.id.iv_image)
ImageView ivImage;
#BindView(R.id.iv_checked)
ImageView ivChecked;
public ViewHolder(View view) {
ButterKnife.bind(this,view);
}
}
}
MyCustomSizeRecylerAdapter.java
public class MyCustomSizeRecylerAdapter extends RecyclerView.Adapter<MyCustomSizeRecylerAdapter.MyViewHolder> {
ArrayList<SizeCustomModel> sizeModels;
Context context;
int checkPosition = -1;
String selectedSize;
public MyCustomSizeRecylerAdapter(ArrayList<SizeCustomModel> sizeModels, Context context) {
this.sizeModels = sizeModels;
this.context = context;
}
public void getNewModel(ArrayList<SizeCustomModel> customModels) {
sizeModels.clear();
this.sizeModels = customModels;
selectedSize = null;
Log.d("sizemodel", "getNewModel: " + new Gson().toJson(sizeModels));
notifyDataSetChanged();
}
public void getSelectedPosition(int position) {
checkPosition = position;
notifyDataSetChanged();
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.size_adapter, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_sizeName.setText(sizeModels.get(position).getSize());
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
if (sizeModels.get(position).getStock_Status() == 0) {
holder.ll_mainview.setClickable(false);
holder.ll_sizeAdapter.setBackgroundResource(R.color.blue_700);
} else if (sizeModels.get(position).getStock_Status() == 1) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
if (checkPosition == position) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
selectedSize = sizeModels.get(position).getSize();
if (context instanceof AddToCartActivity) {
((AddToCartActivity) context).getSelectSize(selectedSize);
}
} else {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.tv_black));
}
if (sizeModels.size() == 1) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
selectedSize = sizeModels.get(position).getSize();
if (context instanceof AddToCartActivity) {
((AddToCartActivity) context).getSelectSize(selectedSize);
}
}
}
}
#Override
public int getItemCount() {
return sizeModels.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.tv_sizeName)
TextView tv_sizeName;
#BindView(R.id.ll_sizeAdapter)
LinearLayout ll_sizeAdapter;
#BindView(R.id.main_view)
LinearLayout ll_mainview;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
At first both adapter will set data and after click color item the size adapter must be change but it only changes when i click any of the item. adapter.notifyDataSetChanged() doesnt work here for me.
Both Adapter set
When i click color item but doesnt change size adapter
when i click size item only change size adapter
Use Interface to bridge with two adapter and communicate with each other.
I apologize upfront if the question is too long. Here we go:
There are two activities: Main and Detail Activity.
Main Activity is basically a GridView.
Detail Activity is basically shows the clicked item's detail information. I am passing selected item's id (pid) from the Main to the Detail Activity.
I am facing an issue as follows. Initially, I have 3G connection (cellular connection) and clicked on the first item and see the corresponding item detail in the Detail Activity, it works perfectly fine, and go back to the Main Activity, then clicked on the second item, then unfortunately it still shows me the first item in the DetailActivity that I clicked initially.
I switched from 3g to wifi while app is on the active and open. No matter what I click, it still shows me the first item that I clicked initially.
But when I delete the app and reinstall it and get either wifi access only, the app works perfectly fine.
In the following implementation, Connection URL (PRODUCT_DETAIL_URL) is http, not https. I am using Volley library for the network connection.
DetailActivity.java
private void productDetailInit() {
// it is http
StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
jsonObject = response;
loadJsonData();
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("id", productID);
return params;
}
};
RetryPolicy policy = new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);
CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
}
CustomVolleyRequest.java
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
private class BitmapCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
mCache = new LruCache<>(20);
}
#Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
// scaling bitmap for avoiding too much big images
Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
mCache.put(url, scaled);
}
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
Adapter.java
class ProductMainAdapter extends ArrayAdapter<ImageRecord> {
private ImageLoader mImageLoader;
private String jsonObject;
ProductMainAdapter(Context context) {
super(context, R.layout.grid_item);
mImageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
}
#NonNull
#Override
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
convertView.setBackgroundResource(R.drawable.round_gridview);
holder.priceTagImage = (ImageView) convertView.findViewById(R.id.priceTag_IV);
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
holder.priceTagRL = (RelativeLayout) convertView.findViewById(R.id.priceTag_RL);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
ImageRecord imageRecord = getItem(position);
holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openProductDetail(position);
}
});
holder.textView.setText(imageRecord != null ? imageRecord.getTitle() : null);
holder.priceTagRL.setRotation(0);
return convertView;
}
private class ViewHolder{
TextView textView;
ImageView priceTagImage;
NetworkImageView imageView;
RelativeLayout priceTagRL;
}
private void openProductDetail(int position) {
try {
ImageRecord imr = getItem(position);
String productID = imr != null ? imr.getId() : "0";
Intent intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("pid", productID);
getContext().startActivity(intent);
} catch (Exception e) {
Log.e("openProductDetail", "exception", e);
}
}
I wonder what I am missing/ doing wrong in the provided implementation. It has been taking almost 2-3 months, I could not able to handle that issue. Has anyone ever faced a similar situation? Any suggestion or comment is highly appreciated.
You could just kill the activities with finish(); when the other one loads.
I was also having the same problem. In my case in onClick method the position was not correct.
Then I used to set the position as a tag to the specific view which has OnClickListener set and my problem solved.
It may be due to the position is declared as final. So remove the final keyword and try like this.
holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
holder.imageView.setTag(position);
holder.priceTagRL.setRotation(0);
And in onClick method
openProductDetail((int)view.getTag());
Check if the below link can be helpful:
https://stackoverflow.com/a/33682366/2798289 (3G connection might actually be slow that you to have increase the threshold of timeout)
Also, there is a chance the position reference in the adapter can come wrong in some edge case (irrespective of your actual issue). You can try the below alternative method..
Use gridView.setOnItemClickListener
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//do your logic based on the position
}
});
Note: In case if GridView is not able to observe the item click event, you have to set android:descendantFocusability="blocksDescendants" to the child root view under gridview. This might happen basically because the GridView items should not be clickable by itself as it will observe the click event.
Reference: https://developer.android.com/guide/topics/ui/layout/gridview.html
Hey in this program I have set a GridView according to the text it has but when running the app the app lags alot on the emulator as well as on the device. And the error
"The application may be doing too much work on its main thread." is given
I guess thats what causing the problem here.
Here is my ImageAdapter file
public class ImageAdapter extends BaseAdapter {
static class ViewHolder {
ImageView imageView;
TextView textView;
}
private Context context;
String mobile;
private final String[] mobileValues;
int[] imagesArr = new int[]{R.drawable.badminton, R.drawable.cricket, R.drawable.basketball, R.drawable.carrom,
R.drawable.handball, R.drawable.humanfoosball, R.drawable.kabaddi, R.drawable.khokho, R.drawable.chess,
R.drawable.longjump, R.drawable.streetsoccer, R.drawable.shotput, R.drawable.volleyball, R.drawable.tugofwar,
R.drawable.tabletennis, R.drawable.handball, R.drawable.rellayrace};
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View row = convertView;
if (row == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//row = new View(context);
// get layout from grid_itemxml.xml
row = inflater.inflate(R.layout.grid_item, null);
// set value into textview
holder.textView = (TextView) row.findViewById(R.id.grid_item_label);
holder.textView.setText(mobileValues[position]);
// set image based on selected text
holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
mobile = mobileValues[position];
if (mobile.equals("Badminton")) {
holder.imageView.setImageResource(imagesArr[0]);
}
if (mobile.equals("Cricket")) {
holder.imageView.setImageResource(imagesArr[1]);
}
if (mobile.equals("Basketball")) {
holder.imageView.setImageResource(imagesArr[2]);
}
if (mobile.equals("Carrom")) {
holder.imageView.setImageResource(imagesArr[3]);
}
if (mobile.equals("Handball")) {
holder.imageView.setImageResource(imagesArr[4]);
}
if (mobile.equals("Human Foosball")) {
holder.imageView.setImageResource(imagesArr[5]);
}
if (mobile.equals("Kabaddi")) {
holder.imageView.setImageResource(imagesArr[6]);
}
if (mobile.equals("Khokho")) {
holder.imageView.setImageResource(imagesArr[7]);
}
if (mobile.equals("Chess")) {
holder.imageView.setImageResource(imagesArr[8]);
}
if (mobile.equals("Longjump")) {
holder.imageView.setImageResource(imagesArr[9]);
}
if (mobile.equals("Streetsoccer")) {
holder.imageView.setImageResource(imagesArr[10]);
}
if (mobile.equals("Shotput")) {
holder.imageView.setImageResource(imagesArr[11]);
}
if (mobile.equals("Volleyball")) {
holder.imageView.setImageResource(imagesArr[12]);
}
if (mobile.equals("Tugofwar")) {
holder.imageView.setImageResource(imagesArr[13]);
}
if (mobile.equals("Table Tennis")) {
holder.imageView.setImageResource(imagesArr[14]);
}
if (mobile.equals("Relayrace")) {
holder.imageView.setImageResource(imagesArr[15]);
}
} else {
holder = (ViewHolder) row.getTag();
}
return row;
}
#Override
public int getCount() {
return mobileValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
The images inside your project are taking too much time to load the images, though the android memory resources are limited to app.There are couple of solution that you can try
Keep image size low (downside will be bad quality on bigger screens).
Compress image to required screen resolution size at run time.
The easy way is use image loading libraries. Try this link for details
e.g
Picasso.with(context).load(R.drawable.drawableName).fit().centerCrop().into(imageViewFit)
I have created an app with a listview with arrayadapter that the user can dynamically populate. I store the info entered as a json string in the preferences and when I refresh the app, I get the list with the entries. The thing is that I want the image next to each entry to change after a network operation. The problem I'm facing seems to be that since the elements in the list are added dynamically, I dont seem to find a good way neither to update the imageview on the onPostExecute() method, either to be able to target each entry specifically since they share the same layout ids.
Here is my getView() method inside my adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_item, null);
holder = new ViewHolder();
holder.deviceName = (TextView) convertView
.findViewById(R.id.txt_pc_name);
holder.deviceIp = (TextView) convertView
.findViewById(R.id.txt_pdt_desc);
holder.devicePort = (TextView) convertView
.findViewById(R.id.txt_pdt_price);
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.imgbtn_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Devices device = (Devices) getItem(position);
holder.deviceName.setText(device.getName());
holder.deviceIp.setText(device.getIpOnline());
holder.devicePort.setText(device.getPortOnline() + "");
return convertView;
}
Here is my AsyncTask:
public class Connection extends AsyncTask<String, Void, String> {
private String ipOnline;
private int portOnline;
private String ipWol;
private int portWol;
private String macAddress;
private ImageView img;
private Context context;
public interface AsyncResponse {
void processFinish(String output);
}
public AsyncResponse delegate = null;
public Connection(Context mContext, ImageView Img,String IpOnline, int PortOnline, String IpWol, int PortWol, String MacAddress, AsyncResponse delegate) {
ipOnline = IpOnline;
portOnline = PortOnline;
ipWol = IpWol;
portWol = PortWol;
macAddress = MacAddress;
context = mContext;
// inflate = Inflate;
img = Img;
// spin = spinner;
this.delegate = delegate;
}
public int status;
#Override
protected String doInBackground(String... arg0) {PreferenceManager.getDefaultSharedPreferences(lastContext);
try {
Socket echoSocket = new Socket();
echoSocket.connect(new InetSocketAddress(ipOnline,portOnline),2000);
if(echoSocket.isConnected())
status = 1;
} catch (Exception e) {
status = 0;
}
if(status == 0)
return "0";
else
return "1";
}
#Override
protected void onProgressUpdate(Void... values) {}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ImageView img = (ImageView) activity.findViewById(R.id.imgbtn_favorite);
if (status == 0)
img.setImageResource(android.R.drawable.presence_offline);
else
img.setImageResource(android.R.drawable.presence_online);
delegate.processFinish(result);
}
}
And here is my call to it:
new Connection(activity, img, product.getIpOnline(), Integer.parseInt(product.getPortOnline()), product.getIpWol(), Integer.parseInt(product.getPortWol()), product.getMacAddress(), new Connection.AsyncResponse() {
#Override
public void processFinish(String output) {
}
}).execute();
You can use view holder pattern or recyclerview .
You need to store the reference of image view in holder and can update the image with the help of this reference instead of id of view
I came up with a solution.
I created a public variable on my adapter and I'm adding all the images:
public List<ImageView> allImages = new ArrayList<ImageView>();
public List<ImageView> getAllImages(){
return this.allImages;
}
this.allImages.add((ImageView) convertView
.findViewById(R.id.imgbtn_favorite));
Then on my fragmented activity onCreateView method I deployed a delayed runnable:
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
updateStatus();
}
}, 500);
the updateStatus() method initializes the images variable and begins network checks to determine which image to use. Then it applies it accordingly.
public void updateStatus() {
List<ImageView> images = deviceListAdapter.getAllImages();
if(count > 0 && images.size() > 0) {
for (int i = 0; i < deviceListAdapter.getCount() ; i++) {
Devices product = (Devices) deviceListAdapter.getItem(i);
if((TextUtils.isDigitsOnly(product.getPortOnline()) && TextUtils.isDigitsOnly(product.getPortWol())) && (!product.getPortOnline().isEmpty() && !product.getPortWol().isEmpty())) {
new Connection(activity, images.get(i), product.getIpOnline(), Integer.parseInt(product.getPortOnline()), product.getIpWol(), Integer.parseInt(product.getPortWol()), product.getMacAddress(), new Connection.AsyncResponse() {
#Override
public void processFinish(Boolean output) {
}
}).execute();
}
}
}
}
It might not be optimal but feel free to add a better solution.
I am trying to show a lot of images from my server. There is no conventional link since i am using AMazon s3 services. Here is my code to download the images. Since i need to reduce image size, is this better way of achiving smooth scroll or do i need to do something else
public class PinsListAdapter extends BaseAdapter
{
private Activity mContext;
private ArrayList<PingModel> pings = new ArrayList<PingModel>();
private LayoutInflater inflater;
public PinsListAdapter(Activity context, ArrayList<PingModel> pings)
{
super();
this.mContext = context;
this.pings = pings;
inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return pings.size();
}
#Override
public Object getItem(int arg0)
{
return null;
}
#Override
public long getItemId(int arg0)
{
return 0;
}
private static class ViewHolder
{
public ImageView vidImgIndicator;
public ImageView pinImg;
public ImageView progress;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent)
{
final PingModel ping = pings.get(position);
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.adapter_pin_row, parent, false);
holder.pinImg = (ImageView) convertView.findViewById(R.id.pinImg);
holder.progress = (ImageView) convertView.findViewById(R.id.progress);
holder.vidImgIndicator = (ImageView) convertView.findViewById(R.id.vidImgIndicator);
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.rotating_img);
holder.progress.setAnimation(anim);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
final ViewHolder mainHolder = holder;
holder.vidImgIndicator.setVisibility(View.GONE);
final String url = ping.getLocalMediaUrl(mContext);
if (url != null) /* Image is already placed */
{
if (ping.mediaAttachmentType == PingModel.PING_MEDIA_ATTACHMENT_TYPE_PHOTO)
{
if(ping.thumbnail == null)
{
ping.thumbnail = ImageUtils.getBitmapFromFile(url, 80);
}
}
else if (ping.mediaAttachmentType == PingModel.PING_MEDIA_ATTACHMENT_TYPE_VIDEO)
{
if(ping.thumbnail == null)
{
//ping.thumbnail = ThumbnailUtils.createVideoThumbnail(url, MediaStore.Images.Thumbnails.MINI_KIND );
}
if (ping.thumbnail != null)
mainHolder.vidImgIndicator.setVisibility(View.VISIBLE);
}
mainHolder.pinImg.setImageBitmap(ping.thumbnail);
}
else
{
holder.pinImg.setImageDrawable(null);
if (ping.isMediaBeingDownloaded == false)
{
AppManager.getInstance().executor.execute(new Runnable()
{
public void run()
{
ping.isMediaBeingDownloaded = true;
ApiManager.getInstance().pingManager.downloadMediaOfPingFromServer(ping);
ping.isMediaBeingDownloaded = false;
if (ping.mediaAttachmentType == PingModel.PING_MEDIA_ATTACHMENT_TYPE_PHOTO)
{
ping.thumbnail = ImageUtils.getBitmapFromFile(url, 80);
}
else if (ping.mediaAttachmentType == PingModel.PING_MEDIA_ATTACHMENT_TYPE_VIDEO)
{
ping.thumbnail = ThumbnailUtils.createVideoThumbnail(url, MediaStore.Images.Thumbnails.MINI_KIND);
}
mContext.runOnUiThread(new Runnable()
{
#Override
public void run()
{
notifyDataSetChanged();
}
});
}
});
}
}
return convertView;
}
}
Please note the executers. Is this correct and logical way of doing it or am i doing it totally wrong and i need to make any other cache type thing?
image downloading, caching, storing in a scrolling list is a very very complex situation that I advise nobody to do themselves.
Instead you should do what most apps do, use a 3rd party library specialised for the job I'll point you to 3 of the current "best" ones, pick which ever you prefer.
Picasso - https://github.com/square/picasso
Glide - https://github.com/bumptech/glide
Fresco - https://github.com/facebook/fresco
You must always load images on a background thread even the thumbnails, unless they are already in memory.
You can use Picasso and implement your own RequestHandler which downloads the image from S3, it will give you more performance and flexibility.