How to stop ListView recycling? - android

I'm an Android beginner and I can't figure out why this is happening.
Activity Screenshot:
Everything works fine except when I scroll down (hence why I think it has to do with recycling)... So when I scroll back up and attempt to undo the vote (red arrow) on the first post, it thinks the post is voted down!
Alternatively, it may also think the down-vote ImageButton drawable is empty - see code). If I don't scroll, it works perfectly.
getView code:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// inflate the list item
convertView = this.inflater.inflate(R.layout.row_layout, parent, false);
// get views
holder.profilePic = (ImageView) convertView.findViewById(R.id.profilePic);
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.day = (TextView) convertView.findViewById(R.id.day);
holder.rating = (TextView) convertView.findViewById(R.id.rating);
holder.textPost = (TextView) convertView.findViewById(R.id.textPost);
holder.ratingUp = (ImageButton) convertView.findViewById(R.id.ratingUp);
holder.ratingDown = (ImageButton) convertView.findViewById(R.id.ratingDown);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Drawable up_clicked = context.getResources().getDrawable(R.drawable.ic_action_rate_up_clicked);
final Drawable up_unClicked = context.getResources().getDrawable(R.drawable.ic_action_rate_up);
final Drawable down_clicked = context.getResources().getDrawable(R.drawable.ic_action_rate_down_clicked);
final Drawable down_unClicked = context.getResources().getDrawable(R.drawable.ic_action_rate_down);
Post post = cityFeed.get(position);
holder.profilePic.setImageResource(post.getDrawableID());
holder.username.setText(post.getUsername());
holder.day.setText(post.getDay());
holder.rating.setText(post.getRating());
holder.textPost.setText(post.getText());
db = new DatabaseHandler(context);
String userVotesUp = null;
userVotesUp = db.getUserVotes("up");
if (userVotesUp == null) {
userVotesUp = "";
}
String userVotesDown = null;
userVotesDown = db.getUserVotes("down");
if (userVotesDown == null) {
userVotesDown = "";
}
String postID = post.getPostID();
if (userVotesUp.contains(postID)) {
holder.ratingUp.setImageDrawable(up_clicked);
} else if (userVotesDown.contains(postID) && userVotesUp != null) {
holder.ratingDown.setImageDrawable(down_clicked);
} else {
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
db.close();
holder.ratingUp.setTag(position);
holder.ratingDown.setTag(position);
holder.ratingUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View convertView) {
// get post details using button tag
int pos = (Integer)convertView.getTag();
Post post = cityFeed.get(pos);
String postID = post.getPostID();
String ratingString = post.getRating();
int ratingValue = Integer.parseInt(ratingString);
RelativeLayout parent = (RelativeLayout)convertView.getParent().getParent();
TextView ratingView = (TextView)parent.findViewById(R.id.rating);
ImageButton up = (ImageButton)parent.findViewById(R.id.ratingUp);
ImageButton down = (ImageButton)parent.findViewById(R.id.ratingDown);
// if the post is not voted down...
if (down.getDrawable() == down_unClicked) {
// if the post is not voted up...
if (up.getDrawable() == up_unClicked) {
up.setImageDrawable(up_clicked);
ratingValue = ratingValue + 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "up");
// else the post is voted up...
} else {
up.setImageDrawable(up_unClicked);
ratingValue = ratingValue - 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "down");
}
// else the post is voted down...
} else {
down.setImageDrawable(down_unClicked);
up.setImageDrawable(up_clicked);
ratingValue = ratingValue + 2;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "upDownAlready");
}
}
});
holder.ratingDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View convertView) {
// get post details using button tag
int pos = (Integer)convertView.getTag();
Post post = cityFeed.get(pos);
String postID = post.getPostID();
String ratingString = post.getRating();
int ratingValue = Integer.parseInt(ratingString);
RelativeLayout parent = (RelativeLayout)convertView.getParent().getParent();
TextView ratingView = (TextView)parent.findViewById(R.id.rating);
ImageButton up = (ImageButton)parent.findViewById(R.id.ratingUp);
ImageButton down = (ImageButton)parent.findViewById(R.id.ratingDown);
// if the post is not voted up...
if (up.getDrawable() == up_unClicked) {
// if the post is not voted down...
if (down.getDrawable() == down_unClicked) {
down.setImageDrawable(down_clicked);
ratingValue = ratingValue - 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "down");
// else the post is voted down...
} else {
down.setImageDrawable(down_unClicked);
ratingValue = ratingValue + 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "up");
}
// else the post is voted up...
} else {
up.setImageDrawable(up_unClicked);
down.setImageDrawable(down_clicked);
ratingValue = ratingValue - 2;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync = new WebServiceAsync(context);
wsAsync.execute(voteTag, postID, "downUpAlready");
}
}
});
return convertView;
}

By considering that your onClickListerners(on holder.ratingUp and holder.ratingDown) works fine, i think the problem is in this portion of your code
if (userVotesUp.contains(postID)) {
holder.ratingUp.setImageDrawable(up_clicked);
} else if (userVotesDown.contains(postID) && userVotesUp != null) {
holder.ratingDown.setImageDrawable(down_clicked);
} else {
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
If i am not wrong, you are doing something like that you have 3 conditions, First is
if (userVotesUp.contains(postID))
that means rating is upvoted and you are setting up_clicked imageDrawable, but you are not setting down_unClicked image drawable to holder.ratingDown.
second condition applies to this
if (userVotesUp.contains(postID))
then you are setting down_clicked drawable to holder.ratingDown, but not setting up_unClicked image drawable to holder.ratingUp.
and third condition is neither upvoted nor downvoted and that seems to be fine. so I think, you have to replace your that portion of code with this
if (userVotesUp.contains(postID)) {
holder.ratingUp.setImageDrawable(up_clicked);
holder.ratingDown.setImageDrawable(down_unClicked);
} else if (userVotesDown.contains(postID) && userVotesUp != null) {
holder.ratingDown.setImageDrawable(down_clicked);
holder.ratingUp.setImageDrawable(up_unClicked);
} else {
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
Hope this helps..!!

Related

ListView Custom adapter TextView initialize inside try catch block not showing

Trying to fetch data from SQLite and initialize some text view data but the problem is that text view does not show the data that initialize inside the try-catch block. Is there any better solution to accomplish this problem?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
AdmissionViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.followup_listview_row_layout, parent, false);
viewHolder = new AdmissionViewHolder();
viewHolder.p_type = (TextView) row.findViewById(R.id.p_type);
viewHolder.pName_Age = (TextView) row.findViewById(R.id.p_name_age);
viewHolder.admissionDateTime = (TextView) row.findViewById(R.id.admission_date_time);
viewHolder.facilityName = (TextView) row.findViewById(R.id.facility_name);
viewHolder.sex = (TextView) row.findViewById(R.id.sex);
viewHolder.followup_Item = (TextView) row.findViewById(R.id.followup);
viewHolder.followupItem = (Button) row.findViewById(R.id.followup_item);
viewHolder.itemInfo = (ImageView) row.findViewById(R.id.item_info);
viewHolder.arrow = (ImageButton) row.findViewById(R.id.arrow_button);
viewHolder.hiddenView = (TableLayout) row.findViewById(R.id.hiddenView);
viewHolder.ps_m = (TextView) row.findViewById(R.id.txt_s_pm);
viewHolder.ps_e = (TextView) row.findViewById(R.id.txt_s_pe);
viewHolder.ps_n = (TextView) row.findViewById(R.id.txt_s_pn);
viewHolder.ts_m = (TextView) row.findViewById(R.id.txt_s_tm);
viewHolder.ts_e = (TextView) row.findViewById(R.id.txt_s_te);
viewHolder.ts_n = (TextView) row.findViewById(R.id.txt_s_tn);
row.setTag(viewHolder);
} else {
viewHolder = (AdmissionViewHolder)row.getTag();
}
AdmissionModel admissionModel = getItem(position);
String pAge = String.format("%dy %dm %dd",admissionModel.AgeYears,admissionModel.AgeMonths,admissionModel.AgeDays);
viewHolder.p_type.setText(admissionModel.PtType);
viewHolder.pName_Age.setText(String.format("%s (%s)",admissionModel.PtName,pAge));
viewHolder.admissionDateTime.setText(String.format("%s %s",admissionModel.HosAdmDate,admissionModel.HosAdmTime));
viewHolder.facilityName.setText(admissionModel.FacilityName);
viewHolder.sex.setText(admissionModel.SexName);
viewHolder.itemInfo.setOnClickListener(this);
viewHolder.itemInfo.setTag(position);
viewHolder.followupItem.setOnClickListener(this);
viewHolder.followupItem.setTag(position);
viewHolder.followup_Item.setOnClickListener(this);
viewHolder.followup_Item.setTag(position);
viewHolder.arrow.setOnClickListener(v -> clickedListener.buttonClicked(admissionModel));
viewHolder.arrow.setTag(position);
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String toDay = formatter.format(date);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String previousDay = formatter.format(cal.getTime());
List<FollowupModel> followupModelList = dbHelper.GetFollowupData(admissionModel.NtfSL,previousDay,toDay);
if(followupModelList.size()>0){
Map<String, List<FollowupModel>> groupByDateList = Stream.of(followupModelList).collect(
Collectors.groupingBy(e->e.FollowupDateyyyMMdd));
for (String dataKey : groupByDateList.keySet()){
if(dataKey.equals(previousDay)){
for (FollowupModel followupModel : groupByDateList.get(dataKey)){
if(followupModel.Type == 1){
viewHolder.ps_m = setPatientStatus(followupModel.PtStatus);
}else if(followupModel.Type == 2){
viewHolder.ps_e = setPatientStatus(followupModel.PtStatus);
}else if(followupModel.Type == 3){
viewHolder.ps_n = setPatientStatus(followupModel.PtStatus);
}
}
}else if(dataKey.equals(toDay)){
for (FollowupModel followupModel : groupByDateList.get(dataKey)){
if(followupModel.Type == 1){
viewHolder.ts_m = setPatientStatus(followupModel.PtStatus);
}else if(followupModel.Type == 2){
viewHolder.ts_e = setPatientStatus(followupModel.PtStatus);
}else if(followupModel.Type == 3){
viewHolder.ts_n = setPatientStatus(followupModel.PtStatus);
}
}
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
if(admissionModel.isTableLayoutVisible){
viewHolder.hiddenView.setVisibility(View.VISIBLE);
viewHolder.arrow.setImageResource(R.drawable.ic_baseline_expand_less_24);
}else{
viewHolder.hiddenView.setVisibility(View.GONE);
viewHolder.arrow.setImageResource(R.drawable.ic_baseline_expand_more_24);
}
return row;
}
Here getView() will be called every time when your layout is inflated .
Below line is heavy operation .
List<FollowupModel> followupModelList = dbHelper.GetFollowupData(admissionModel.NtfSL,previousDay,toDay);
You will have to move this to other class and pass required info as a parameter/list to your custom adapter class

How to get Id of ChildView Items of ExpandableListView in Android

I'm using ExpandapbeListView its work perfect all expand and collapse but I just want to update only particular child item here when I click on plus value should be update in EditText and I don't want to use notifyDatasetChanged() Just want to update only particular field and also i should able to update EditText directly. It can have multiple child item.
Ex: Colgate may have multiple child Item like 100gm,200gm,300gm
So when I click on Plus It should able to get Id of EditText and Update Quantity
#Override
public View getChildView(int productPos, int weightPos, boolean b, View plusMinusView, ViewGroup viewGroup) {
if (plusMinusView == null) {
csHolder = new ChildHolder();
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
plusMinusView = inflater.inflate(R.layout.orders_products_plus_minus_design, null);
}
csHolder.minQty = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitMinQtyTxt);
csHolder.totalPriceTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitTotalPriceTxt);
csHolder.weightTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitWeightTxt);
csHolder.unitPrice = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitUnitPriceTxt);
csHolder.totalTaxTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitTotalTaxTxt);
csHolder.netPriceTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitNetPriceTxt);
csHolder.orderQtyEdit = (CustomEditText) plusMinusView.findViewById(R.id.orderProducts_orderQtyTxt);
csHolder.plusTxt = (RelativeLayout) plusMinusView.findViewById(R.id.orderProducts_plusTxtLay);
csHolder.minusTxt = (RelativeLayout) plusMinusView.findViewById(R.id.orderProducts_minusTxtLay);
plusMinusView.setTag(productPos + "_" + weightPos);
String tag = "weight_" + productPos + "_" + weightPos;
csHolder.orderQtyEdit.setTag(tag);
csHolder.plusTxt.setTag(tag);
csHolder.minusTxt.setTag(tag);
//csHolder.plusTxt.setOnClickListener(weightClick);
//csHolder.minusTxt.setOnClickListener(weightClick);
final View finalPlusMinusView = plusMinusView;
csHolder.plusTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
csHolder.orderQtyEdit.setText("5");
}
});
if(!csStatus.equalsIgnoreCase("DRAFT")) {
csHolder.plusTxt.setVisibility(View.GONE);
csHolder.minusTxt.setVisibility(View.GONE);
csHolder.orderQtyEdit.setEnabled(false);
}
else
{
csHolder.plusTxt.setVisibility(View.VISIBLE);
csHolder.minusTxt.setVisibility(View.VISIBLE);
csHolder.orderQtyEdit.setEnabled(true);
}
try
{
csHolder.orderQtyEdit.addTextChangedListener(new MyTextWatcher(csHolder.orderQtyEdit));
JSONObject weighObj = csMainArr.getJSONObject(productPos).getJSONArray("weights").getJSONObject(weightPos);
if(weighObj.has("unit"))
{
csHolder.weightTxt.setText(weighObj.getString("unit"));
}
if (weighObj.has("order_qty") == false)
weighObj.put("order_qty", "0");
if (weighObj.has("sell_price") == false)
weighObj.put("sell_price", "0");
if (weighObj.has("order_qty") == false)
{
csHolder.totalPriceTxt.setText("0.00");
csHolder.totalTaxTxt.setText("0.00");
csHolder.netPriceTxt.setText("0.00");
}
else {
int qty = 0;
double sellPrice = 0.00;
float tax = 0f;
double totalPrice = 0.00;
double totalTax = 0.00;
double netPrice = 0.00;
if(csStatus.equals("DRAFT")) {
qty = Integer.parseInt(weighObj.getString("order_qty"));
sellPrice = weighObj.getDouble("sell_price");
}
else
{
qty = Integer.parseInt(weighObj.has("qty") ? weighObj.getString("qty"):"0");
sellPrice = weighObj.getDouble("unit_price");
}
tax = Float.parseFloat(csMainArr.getJSONObject(productPos).has("tax") ? csMainArr.getJSONObject(productPos).getString("tax") : "0");
totalPrice = qty*sellPrice;
totalTax = (totalPrice*tax)/100;
netPrice = totalPrice + totalTax;
csHolder.totalPriceTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",totalPrice));
csHolder.totalTaxTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",totalTax));
csHolder.netPriceTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",netPrice));
}
csHolder.orderQtyEdit.isValueChangeMySelf = true;
if(csStatus.equals("DRAFT"))
csHolder.orderQtyEdit.setText(weighObj.getString("order_qty"));
else
csHolder.orderQtyEdit.setText(weighObj.has("qty")?weighObj.getString("qty"):"0");
/*if(weightPos == iClickedItem) {
csHolder.orderQtyEdit.requestFocus();
}
else {
csHolder.orderQtyEdit.clearFocus();
}*/
if(csStatus.equals("DRAFT"))
csHolder.unitPrice.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",weighObj.has("sell_price")?weighObj.getDouble("sell_price") : 0.00));
else
csHolder.unitPrice.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",weighObj.has("unit_price")?weighObj.getDouble("unit_price") : 0.00));
csHolder.minQty.setText(weighObj.has("min_order_qty")?weighObj.getString("min_order_qty"):"0");
//View line = (View) plusMinusView.findViewById(R.id.orderProducts_submitDividerLine);
//if(isLast)
// line.setVisibility(View.GONE);
}
catch(Exception e)
{
}
return plusMinusView;
}

memoryleak in gridview using base adapter and viewholder method

I am using an extended BaseAdapter for my gridview in which I implement ViewHolder methods. also I pass my data with a Cursor to this adapter.
here is my getView()
public View getView(int position, View view, ViewGroup parent) { // inflate the layout for each item of listView
ViewHolder holder;
if (view == null) {
view = inflater.inflate(gridItemId, null);
Log.d("recyvleView", "inflating " + position);
holder = new ViewHolder();
holder.tvAttachment = (ImageView) view.findViewById(R.id.iv_inventory_products_griditems_attachment);
holder.imageCount = (TextView) view.findViewById(R.id.imagecount);
holder.tvItemCode = (TextView) view.findViewById(R.id.tv_inventory_products_griditems_ItemCode);
holder.tvProductName = (TextView) view.findViewById(R.id.tv_inventory_products_griditems_Title);
holder.tvPrice = (RialTextView) view.findViewById(R.id.tv_inventory_products_griditems_Price);
holder.tvRemain = (TextView) view.findViewById( R.id.tv_inventory_products_griditems_Remain);
holder.btnMore =(com.rey.material.widget.Button) view.findViewById(R.id.btn_inventory_products_griditems_More);
holder.btnPlus = (com.rey.material.widget.Button) view.findViewById(R.id.btn_inventory_products_griditems_addOne);
view.setTag(holder);
} else {
Log.d("recyvleView", "restoring " + position);
holder = (ViewHolder) view.getTag();
}
setupView(view, position,holder);
//setupGridView(view,holder);
return view;
}
I noticed that I have a huge memory leak in this so I tried to minimize everything to find the leak, so I commented most of the methods and here is what has been left from my setupView:
private void setupView(View view, int position, ViewHolder holder) {
// move the cursor to required position
cursor.moveToPosition(position);
Log.d("POSITION", String.valueOf(position));
//holder.itemId = cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEM_ID.toString()));
//TSimpleProduct tempProduct = productCatalog.getSimpleProductById(Integer.parseInt(holder.itemId));
//holder.itemGuId = cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_GUID.toString()));
holder.tvItemCode.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMCODE.toString())));
holder.tvProductName.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMDESC.toString())));
/* //Remain
if (cyberSetting.getSettingValue(TCyberSettingKey.SHOWITEMREMAIN).equals("1")) {
textTemp = (mContext.getString(R.string.restrictedInfo));
} else {
if (tempProduct.getDefaultUnitValue() == 2 && tempProduct.isUnitDependent()) {
String titleRemain2 = DatabaseColumnContent.COL_PRODUCT_CURSOR_REMAIN2.toString();
textTemp = cursor.getString(cursor.getColumnIndex(titleRemain2));
}
if (cyberSetting.getSettingValue(TCyberSettingKey.SHOWITEMREMAIN).equals("2")) {
if (textTemp == null) {
textTemp = "0";
}
int t = Integer.parseInt(textTemp);
if (t > 0) {
textTemp = mContext.getString(R.string.productAvailable);
} else {
textTemp = mContext.getString(R.string.productUnAvailable);
}
}
}
holder.tvRemain.setText(textTemp);
//Price
String priceLevel = "0";
try {
Register register = Register.getInstance();
priceLevel = register.getPriceLevel();
} catch (NoDaoSetException e) {
e.printStackTrace();
}
if(!priceLevel.equals("0"))
textTemp = cursor.getString(cursor.getColumnIndex(priceLevel));
else
textTemp = "0.0";
if (tempProduct.getDefaultUnitValue() == 2 && tempProduct.isUnitDependent()) {
double price2;
price2 = TLineItem.convertPrice1ToPrice2(Double.parseDouble(textTemp), tempProduct.isUnit1Bigger(), tempProduct.getUnitCoef());
textTemp = TGeneralTools.ConvertDoubleToEnglishString(price2);
if (tempProduct.getUnitDesc2() != null && !tempProduct.getUnitDesc2().equals(""))
unitDesc = " (" + tempProduct.getCompleteUnitDesc2() + ")";
} else {
if (tempProduct.getUnitDesc1() != null && !tempProduct.getUnitDesc1().equals(""))
unitDesc = " (" + tempProduct.getCompleteUnitDesc1() + ")";
}
holder.priceDef = textTemp;
holder.tvPrice.setText(textTemp + unitDesc);
holder.tvRemain.setText(holder.tvRemain.getText() + unitDesc);
//image
pictureCatalog = TPictureCatalog.getInstance();
String defGuid = "";
if (tempProduct.getHasAttachContent() >= 1 && pictureCatalog.isDownloadedAlbumAvailable()) {
defGuid = pictureCatalog.getDefaultPictureGuid(holder.itemGuId);
if (tempProduct.getHasAttachContent() == 1) {
holder.imageCount.setVisibility(View.GONE);
} else {
holder.imageCount.setVisibility(View.VISIBLE);
holder.imageCount.setText(String.valueOf(tempProduct.getHasAttachContent()));
}
} else {
holder.imageCount.setVisibility(View.GONE);
}
String filename = Environment.getExternalStorageDirectory().getPath()
+ FileAddressContent.APPLICATION_HOME_DIRECTORY
+ FileAddressContent.PICTURES_ROOT_DIRECTORY
//+ FileAddressContent.PICTURES_THUMBS_DIRECTORY.toString()
+ defGuid + FileAddressContent.PICTURES_EXTENSION;
pic = new File(filename);
if (pic.exists())
Picasso.with(mContext)
.load(pic)
.error(R.drawable.noimage)
//.placeholder(R.drawable.loading)
.resize(thumbSize, thumbSize)
.centerInside()
.into(holder.tvAttachment);
else
Picasso.with(mContext)
.load(R.drawable.noimage)
.resize(thumbSize, thumbSize)
.centerInside()
.into(holder.tvAttachment);
holder.tvAttachment.setMinimumHeight(thumbSize);
view.setTag(holder);*/
}
even though almost all of it is commented, these two lines are still leaking, I know cause when I commented them out, no leak was there ... !
holder.tvItemCode.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMCODE.toString())));
holder.tvProductName.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMDESC.toString())));
also this is my holder class :
public class ViewHolder {
public ImageView tvAttachment;
public ImageView ivStatus;
public TextView imageCount;
public TextView tvItemCode;
public TextView tvProductName;
public TextView tvRemain;
public RialTextView tvPrice;
public String priceDef;
public String itemId;
public String itemGuId;
public Button btnMore;
public Button btnPlus;
}
any suggestion, please ?! how can I stop it from leaking ?!
thanks in advance!
EDIT:
I get same result using cursor adapter as well.
Why are you using BaseAdapter? Android API has CursorAdapter and CursorLoader which handles all the cursor stuff. Looks like you are doing something wrong with cursors and this causes memory leaks.

In Android how to prevent updating existing items on notifyDataSetChanged()

I have chat app
In custom array adapter. I set random colors to usernames for each message with this code:
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
nameTextView.setTextColor(Color.parseColor(randomColor));
When new message arrive I add it to listview, and call adapter.notifyDataSetChanged();
This causes recoloring existing usernames in messages each time.
How can I prevent recoloring existing items in listview when calling adapter.notifyDataSetChanged();
Edit 1
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessageElement el = list.get(position);
ViewHolder holder = null;
NewMessagesLabelHolder labelHolder = null;
if (convertView == null) {
convertView = el.getView(inflater, parent);
if (el.isMessage()) {
holder = new ViewHolder();
holder.messageLayout = (RelativeLayout) convertView.findViewById(R.id.message_container);
holder.messageContent = (LinearLayout) convertView.findViewById(R.id.message_content);
holder.bottomIndicator = (LinearLayout) convertView.findViewById(R.id.bottom_indicators);
holder.dateTextView = (TextView) convertView.findViewById(R.id.message_date);
holder.timeAgo = (TextView) convertView.findViewById(R.id.time_ago);
holder.nameTextView = (TextView) convertView.findViewById(R.id.user_name);
holder.likesCountTextView = (TextView) convertView.findViewById(R.id.likes_count);
holder.likesLabelImageView = (ImageView) convertView.findViewById(R.id.likes_label);
holder.spamCountTextView = (TextView) convertView.findViewById(R.id.spam_count);
holder.spamLabelImageView = (ImageView) convertView.findViewById(R.id.spam_label);
holder.avatarImageView = (ImageView) convertView.findViewById(R.id.avatar);
holder.statusImageView = (ImageView) convertView.findViewById(R.id.delivered_label);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress_bar);
holder.errorSign = (ImageView) convertView.findViewById(R.id.error_sign);
holder.spamSign = (ImageView) convertView.findViewById(R.id.spam_sign);
holder.bookmarkLabel = (ImageView) convertView.findViewById(R.id.bookmark_label);
holder.topIndicators = (LinearLayout) convertView.findViewById(R.id.topIndicators);
convertView.setTag(holder);
}
if (el.isNewMessagesLabel()) {
convertView = inflater.inflate(R.layout.chat_new_messages_label_layout, parent, false);
labelHolder = new NewMessagesLabelHolder();
labelHolder.newMessagesLabel = (TextView) convertView.findViewById(R.id.new_messages_label);
convertView.setTag(labelHolder);
}
} else {
if (el.isMessage()) {
holder = (ViewHolder) convertView.getTag();
}
if (el.isNewMessagesLabel()) {
labelHolder = (NewMessagesLabelHolder) convertView.getTag();
}
}
if (el.isMessage()) {
Message currentMessage = (Message) el;
drawMessage(holder, currentMessage, position);
}
if (el.isNewMessagesLabel()) {
NewMessagesLabel messagesLabel = (NewMessagesLabel) el;
drawNewMessagesLabel(labelHolder, messagesLabel);
}
return convertView;
}
private void drawMessage(ViewHolder holder, Message message, int position) {
String date = message.getCreatedAt();
String formattedDate = DateHelper.getInstance(context).formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "HH:mm", date);
String userName = message.getUserName();
holder.likesLabelImageView.setVisibility(View.GONE);
holder.likesCountTextView.setVisibility(View.GONE);
holder.spamCountTextView.setVisibility(View.GONE);
holder.spamLabelImageView.setVisibility(View.GONE);
holder.bookmarkLabel.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.progressBar.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.errorSign.setVisibility(View.GONE);
holder.spamSign.setVisibility(View.GONE);
holder.timeAgo.setVisibility(View.GONE);
holder.timeAgo.setText("");
holder.messageLayout.setAlpha(1f);
holder.topIndicators.setVisibility(View.VISIBLE);
if (message.isNewDay()) {
holder.timeAgo.setVisibility(View.VISIBLE);
String dayDate = DateHelper.getInstance(context).formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "MMMM d, yyyy", date);
//String dayDate = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()), context);
holder.timeAgo.setText(dayDate);
}else{
holder.timeAgo.setVisibility(View.GONE);
}
holder.avatarImageView.setImageResource(R.drawable.com_facebook_profile_default_icon);
if (GrouviDatabaseManager.getInstance(context).getCurrentUser().getId() == message.getUserId()) {
userName = USERNAME_ME;
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_SEND)) {
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_SENDING)) {
holder.progressBar.setVisibility(View.VISIBLE);
}
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_LOCAL)) {
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
if (message.getStatus() == null || message.getStatus().equals(Message.MSG_STATUS_ERROR)) {
holder.messageLayout.setAlpha(0.4f);
holder.errorSign.setVisibility(View.VISIBLE);
}
}
if (message.getSpamCount() > 0 && currentMode == MODE_NORMAL) {
holder.messageLayout.setAlpha(0.4f);
holder.spamSign.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(params);
holder.spamCountTextView.setText(Integer.toString(message.getSpamCount()));
holder.spamCountTextView.setVisibility(View.VISIBLE);
holder.spamLabelImageView.setVisibility(View.VISIBLE);
} else {
holder.messageLayout.setAlpha(1f);
holder.spamSign.setVisibility(View.GONE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(params);
}
if (currentMode == MODE_REPORT) {
String timeAgo = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()));
holder.timeAgo.setText(timeAgo);
holder.timeAgo.setVisibility(View.VISIBLE);
}
if (message.getUserAvatar() != null && !message.getUserAvatar().isEmpty()) {
try {
Picasso
.with(context)
.load(message.getUserAvatar())
.error(R.drawable.com_facebook_profile_default_icon)
.placeholder(R.drawable.com_facebook_profile_default_icon)
.into(holder.avatarImageView);
} catch (Exception e) {
Log.e("Main", e.getMessage(), e);
}
}
colorUsername(holder, message, userName);
holder.nameTextView.setText(userName);
holder.dateTextView.setText(formattedDate);
if (message.getLikesCount()>0) {
holder.likesCountTextView.setText(Integer.toString(message.getLikesCount()));
holder.likesCountTextView.setVisibility(View.VISIBLE);
holder.likesLabelImageView.setVisibility(View.VISIBLE);
}
if (message.isBookmarked()) {
holder.bookmarkLabel.setVisibility(View.VISIBLE);
}
List<MessageComponent> messageComponentList;
messageComponentList = message.getMessageComponents();
drawMessageContent(holder, messageComponentList, message);
holder.nameTextView.setTag(position);
holder.avatarImageView.setTag(position);
holder.nameTextView.setOnClickListener(userClickListener);
holder.avatarImageView.setOnClickListener(userClickListener);
// hang empty onLingClickListener to display context menu when
// long click on whole message
holder.nameTextView.setOnLongClickListener(longClickListener);
holder.avatarImageView.setOnLongClickListener(longClickListener);
}
private void colorUsername(ViewHolder holder, Message message, String userName) {
String randomColor = prevColor;
if (userName.equals(USERNAME_ME)){
randomColor = "#000000";
}else if (message.getUserId()!=prevMsgUserID) {
do {
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
}while (prevColor.equals(randomColor));
}
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
prevColor = randomColor;
prevMsgUserID = message.getUserId();
}
You should keep color in the data class, for example in the Message class, or keep Map in the adapter, that maps Message to Color. Then randomly create a color, when it doesn't present in the map. or use the color, if it present in the map. Using a map is good solution than keeping in the data class, when the color doesn't related to message, when it is not a message property, so in that case, a good solution is a keeping it in the adapter class, in the map, for example
private Map<Message, String> colorsMap = new HashMap<Message, String>();
private void colorUsername(ViewHolder holder, Message message, String userName) {
String randomColor;
if(colorsMap.contains(message)) {
randomColor = colorsMap.get(message);
}
else {
if (userName.equals(USERNAME_ME)){
randomColor = "#000000";
}else if (message.getUserId()!=prevMsgUserID) {
do {
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
}while (prevColor.equals(randomColor));
}
colorsMap.put(message, randomColor);
}
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
}
Basically you have to create a Collection of user data model.
Like ArrayList<UserModel> arr = new ArrayList<UserModel>();
User Model can contain-> userId,userColor,groupId(if group chat supported) and map this model with you DB.
Means you have to make a column in db and add userBgColor in user table and map it with the model and this model in array list and supply the array list to your custom adapter .
Your can use ORMLite for rational mapping .
Then in getView()
{
UserModel model = arr.get(position);
String color = user.getUserColor();
}

Change two textViews inside a listview

Let's say, I have two textviews, TextView A and B.
when I click on A, I need to change both textviews;
When I click on B, I need it to do something else.
I used the setTag(key,A); and setTag(key,B); The textView A changed correctly but the textView B is null. And when I click on B it works.
Here is my code :
public class CustomeAdapterHowComment extends ArrayAdapter<ItemsHowComment> {
Context context;
ItemsHowComment items;
List<LikersComment> likeItems;
List<ItemsHowComment> Items1;
Bitmap bitmap;
String imageUser = "";
String filePath_Image = "/Pictures/jehad/joj/";
String user_id = "1510";
String secret_id = "789654120";
View v;
String onclick = " ";
int x;
String table = "10";
String target = "";
String type = "";
DataBaseHandler dbobj;
public static String server_List_of_Comments = "https://www.ashabe.comt";
public static String server_Comment_like = "https://www.ashabe.com/";
public static String server_Comment_like_remove = "https://www.ashabe.com/";
public static String server_Comment_Delte = "https://www.ashabe.com/";
public static String server_Comment_likers = "https://www.ashabe.com/";
String response;
public CustomeAdapterHowComment(Context context, int textViewResourceId,
List<ItemsHowComment> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("View ", "View>>>>>>>>");
commentholder h = null;
v = convertView;
items = getItem(position);
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
v = vi.inflate(R.layout.how_comment_list, null);
h = new commentholder();
h.img = (ImageView) v.findViewById(R.id.CommentImg);
h.name = (TextView) v.findViewById(R.id.CommentName);
h.commentBody = (TextView) v.findViewById(R.id.CommentBody);
h.like = (TextView) v.findViewById(R.id.CommentLike);
h.numoflike = (TextView) v.findViewById(R.id.CommentNumOfLike);
v.setTag(h);
} else {
h = (commentholder) v.getTag();
}
imageUser = items.getUserId() + items.getRand();
boolean flag_ImagePath = Methods.checkIfImage_DirExists(filePath_Image
+ imageUser);
if (flag_ImagePath == true) {
Log.v("flag_ImagePath", String.valueOf(flag_ImagePath));
bitmap = Methods.displayBitmapImage(imageUser);
h.img.setImageBitmap(bitmap);
}
h.commentBody.setText(items.getCommentBody());
h.name.setText(items.getName());
if (items.getiLike().equals("0")) {
h.like.setText("Like");
}
if (items.getiLike().equals("1")) {
h.like.setText("Unlike");
}
if (items.getNumOfLike().equals("0")) {
h.numoflike.setText("0");
} else {
h.numoflike.setText(items.getNumOfLike());
}
h.name.setOnClickListener(listener);
h.name.setTag(R.id.CommentName, h.name);
h.name.setTag(position);
h.numoflike.setOnClickListener(listener);
h.numoflike.setTag(R.id.CommentNumOfLike, h.numoflike);
h.numoflike.setTag(position);
h.like.setOnClickListener(listener);
h.like.setTag(R.id.CommentLike, h.like);
h.like.setTag(position);
h.img.setOnClickListener(listener);
h.img.setTag(R.id.CommentImg, h.img);
h.img.setTag(position);
return v;
}
private OnClickListener listener = new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View view) {
final int id = view.getId();
type = " ";
CommentEvents t = new CommentEvents();
t.execute();
switch (id) {
case R.id.CommentImg:
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + Items1.get(x).getName());
Toast.makeText(getContext(), Items1.get(x).getName(),
Toast.LENGTH_SHORT).show();
break;
case R.id.CommentName:
// ///profile
try {
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
TextView tv = (TextView) view.getTag(R.id.CommentName);
Log.v("value", x + Items1.get(x).getName());
Toast.makeText(getContext(), Items1.get(x).getName(),
Toast.LENGTH_SHORT).show();
// String user = Items1.get(x).getUserId();
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
}
break;
case R.id.CommentLike:
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + "");
TextView like = (TextView) view.getTag(R.id.CommentLike);
target = Items1.get(x).getCommentId().toString();
type = "like";
CommentEvents s = new CommentEvents();
s.execute(user_id, secret_id, table, target);
if (Items1.get(x).getiLike().equals("0")) {
like.setText("Unlike");
onclick = "true";
} else if (Items1.get(x).getiLike().equals("1")) {
like.setText("Like");
onclick = "true";
}
case R.id.CommentNumOfLike:
// ////howlike
if (onclick.equals("true")) {
x = (Integer) view.getTag();
TextView numofLike = (TextView) view
.getTag(R.id.CommentNumOfLike);
if (numofLike != null) {
if (Items1.get(x).getiLike().equals("0")) {
Log.e("getNumOfLike____unlike", Items1.get(x)
.getNumOfLike());
int num = Integer.parseInt(Items1.get(x)
.getNumOfLike().toString());
num = num + 1;
Log.e("numberunlike", num + "");
numofLike.setText(num + "");
} else if (Items1.get(x).getiLike().equals("1")) {
Log.e("getNumOfLike_______like", Items1.get(x)
.getNumOfLike());
int num = Integer.parseInt(Items1.get(x)
.getNumOfLike().toString());
num = num - 1;
Log.e("numberlike", num + "");
numofLike.setText(num + "");
}
onclick=" "; }
} else {
try {
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + "");
target = Items1.get(x).getCommentId().toString();
type = "numoflike";
CommentEvents r = new CommentEvents();
r.execute(user_id, secret_id, table, target);
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
}
}
}
}
};
Use
View view = v.getParent();
and then try to initialize views with view in onClick
You can set Tag
h.name.setOnClickListener(listener);
h.name.setTag(h.numoflike); // use setTag only once
h.numoflike.setOnClickListener(listener);
h.numoflike.setTag(h.name); // use setTag only once
Then in on click
public void onClick(View v) {
switch(v.getId())
{
case R.id.CommentName :
TextView tv1 = (Button) v.getTag();
TextView tv2 = (TextView) v;
tv1.setText("clicked");
tv2.setText("clicked");
break;
case R.id.CommentNumOfLike :
//do something
break;
}
}
Or in onClick Initialize yours views
View view = v.getParent();
TextView tv = view.findVIewById(R.id.CommentName);
TextView tv1 = view.findVIewById(R.id.CommentNumOfLike);
tv.setText("clicked");
tv1.setText("clicked");

Categories

Resources