Smooth scrolling in listview with multiple row layouts - android
I have a listview which inflates 4 different types of row layouts depending on the position(overriden getViewTypeCount() and getItemViewType(int position) to achieve it) .Each row has an Image and other textviews and buttons . I am loading the image in a seaparate thread . I am using android viewholder pattern to cache the viewholders for each row type .
When I scroll from one kind of row layout to another row layout I observe a jerkiness in scrolling .
Any pointers to improve will be appreciated.
public View getView(int arg0, View arg1, ViewGroup arg2) {
int width = (Utils.getWidth(mCtx)-Utils.dpToPx(40, mCtx)) ;
final FeedViewData mFeedViewData = getFeedViewData().get(arg0);
if( getItemViewType(arg0) == Utils.SMALL_OBJECT_LEFT){
ObjectViewHolder viewHolderLeft = new ObjectViewHolder() ;
float imgWidth = (width-Utils.dpToPx(1, mCtx))*(0.6f);
float descWidth = (width-Utils.dpToPx(1, mCtx))*(0.4f) ;
float rowHeight = (width-Utils.dpToPx(1, mCtx))*(0.6f)*Utils.PRODUCT_PRI_ASPECT_RATIO;
if(arg1==null){
LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
arg1 = inflater.inflate(R.layout.view_multipane_left_image, arg2, false);
viewHolderLeft.row = arg1.findViewById(R.id.rowLayout);
viewHolderLeft.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
viewHolderLeft.objImg = (ImageView)arg1.findViewById(R.id.img_object);
viewHolderLeft.price = (TextView)arg1.findViewById(R.id.text_price);
viewHolderLeft.paneMargin = arg1.findViewById(R.id.pane_margin);
viewHolderLeft.descriptionsLayout = (LinearLayout)arg1.findViewById(R.id.layout_description);
viewHolderLeft.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
viewHolderLeft.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);
viewHolderLeft.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
viewHolderLeft.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);
viewHolderLeft.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
viewHolderLeft.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);
viewHolderLeft.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
viewHolderLeft.userImg = (ImageView)arg1.findViewById(R.id.img_user);
viewHolderLeft.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
viewHolderLeft.timeline = (TextView)arg1.findViewById(R.id.text_timeline);
RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
imgLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
viewHolderLeft.objImg.setLayoutParams(imgLp);
RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
rlLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
rlLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
viewHolderLeft.imagesLayout.setLayoutParams(rlLp);
RelativeLayout.LayoutParams marginLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams(Utils.dpToPx(1,mCtx),(int)rowHeight));
marginLp.addRule(RelativeLayout.RIGHT_OF,R.id.layout_image);
viewHolderLeft.paneMargin.setLayoutParams(marginLp);
RelativeLayout.LayoutParams descLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight));
descLp.addRule(RelativeLayout.RIGHT_OF,R.id.pane_margin);
descLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
viewHolderLeft.descriptionsLayout.setLayoutParams(descLp);
ImageSizeUtils.defineTargetSizeForView(viewHolderLeft.objImg, (int)imgWidth,(int)rowHeight);
arg1.setTag(viewHolderLeft);
}
else{
viewHolderLeft = (ObjectViewHolder)arg1.getTag();
ImageSizeUtils.defineTargetSizeForView(viewHolderLeft.objImg, (int)imgWidth,(int)rowHeight);
}
String mLeftImgUrl = null;
mLeftImgUrl = Utils.m_img_base_url+
mFeedViewData.getProductId() + "/pri-" + mFeedViewData.getFileidn()+".jpg";
try{
ImageLoader.getInstance().displayImage(mLeftImgUrl, viewHolderLeft.objImg,
new SingleImageListener(mFeedViewData,viewHolderLeft.objImg));
}
catch(Exception e){
return null;
}
viewHolderLeft.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));
if(mFeedViewData.getIsLoved())
viewHolderLeft.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
else
viewHolderLeft.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);
viewHolderLeft.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderLeft.likeActionLayout));
viewHolderLeft.shareActionLayout.setOnClickListener(new ShareClickListener(mLeftImgUrl));
viewHolderLeft.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));
viewHolderLeft.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
viewHolderLeft.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
if(!mFeedViewData.getLikeCount().equals("0"))
viewHolderLeft.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
if(!mFeedViewData.getShareCount().equals("0"))
viewHolderLeft.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
if(!mFeedViewData.getRevCount().equals("0"))
viewHolderLeft.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");
int minutes=0;
if(mFeedViewData.getTimestamp()!=""){
long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
minutes = (int)((int) (timeDiff/1000))/60;
}
if(mFeedViewData.getUsername().length()>0){
ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderLeft.userImg,new UserImageListener(viewHolderLeft.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
viewHolderLeft.userActivity.setText(ss);
if(minutes!=0){
viewHolderLeft.timeline.setText( minutes + " minutes ago");
}
viewHolderLeft.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getReview()!=null){
ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderLeft.userImg,new UserImageListener(viewHolderLeft.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
viewHolderLeft.userActivity.setText(ss);
if(minutes!=0){
viewHolderLeft.timeline.setText( minutes + " minutes ago");
}
viewHolderLeft.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getActivity().length()>0){
String activityText;
if(mFeedViewData.getActivity().equals("null"))
activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + "" +" ").toString();
else
activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + "" +" "+ mFeedViewData.getActivity()).toString();
viewHolderLeft.userActivity.setText(activityText);
if(minutes!=0){
viewHolderLeft.timeline.setText( minutes + " minutes ago");
}
viewHolderLeft.userImg.setImageResource(R.drawable.ic_blank_profile);
}
if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
viewHolderLeft.userActivity.setClickable(false);
viewHolderLeft.objImg.setClickable(false);
}
}
else if(getItemViewType(arg0) == Utils.SMALL_OBJECT_RIGHT){
ObjectViewHolder viewHolderRight = new ObjectViewHolder();
float imgWidth = (width-Utils.dpToPx(1, mCtx))*(0.6f);
float descWidth = (width-Utils.dpToPx(1, mCtx))*(0.4f) ;
float rowHeight = (width-Utils.dpToPx(1, mCtx))*(0.6f)*Utils.PRODUCT_PRI_ASPECT_RATIO;
if(arg1==null){
LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
arg1 = inflater.inflate(R.layout.view_multipane_right_image, arg2, false);
viewHolderRight.row = arg1.findViewById(R.id.rowLayout);
viewHolderRight.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
viewHolderRight.objImg = (ImageView)arg1.findViewById(R.id.img_object);
viewHolderRight.price = (TextView)arg1.findViewById(R.id.text_price);
viewHolderRight.paneMargin = arg1.findViewById(R.id.pane_margin);
viewHolderRight.descriptionsLayout = (LinearLayout)arg1.findViewById(R.id.layout_description);
viewHolderRight.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
viewHolderRight.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);
viewHolderRight.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
viewHolderRight.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);
viewHolderRight.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
viewHolderRight.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);
viewHolderRight.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
viewHolderRight.userImg = (ImageView)arg1.findViewById(R.id.img_user);
viewHolderRight.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
viewHolderRight.timeline = (TextView)arg1.findViewById(R.id.text_timeline);
RelativeLayout.LayoutParams descLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight));
descLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
descLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
viewHolderRight.descriptionsLayout.setLayoutParams(descLp);
RelativeLayout.LayoutParams marginLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams(Utils.dpToPx(1,mCtx),(int)rowHeight));
marginLp.addRule(RelativeLayout.RIGHT_OF,R.id.layout_description);
viewHolderRight.paneMargin.setLayoutParams(marginLp);
RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
imgLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
viewHolderRight.objImg.setLayoutParams(imgLp);
RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
rlLp.addRule(RelativeLayout.RIGHT_OF,R.id.pane_margin);
rlLp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
viewHolderRight.imagesLayout.setLayoutParams(rlLp);
ImageSizeUtils.defineTargetSizeForView(viewHolderRight.objImg, (int)imgWidth,(int)rowHeight);
arg1.setTag(viewHolderRight);
}
else{
viewHolderRight = (ObjectViewHolder)arg1.getTag();
ImageSizeUtils.defineTargetSizeForView(viewHolderRight.objImg, (int)imgWidth,(int)rowHeight);
}
String mRightImgUrl = null;
mRightImgUrl = Utils.m_img_base_url+
mFeedViewData.getProductId() + "/pri-" + mFeedViewData.getFileidn()+".jpg";
try{
ImageLoader.getInstance().displayImage(mRightImgUrl, viewHolderRight.objImg,
new SingleImageListener(mFeedViewData,viewHolderRight.objImg));
}
catch(Exception e){
return null;
}
viewHolderRight.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));
if(mFeedViewData.getIsLoved())
viewHolderRight.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
else
viewHolderRight.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);
viewHolderRight.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderRight.likeActionLayout));
viewHolderRight.shareActionLayout.setOnClickListener(new ShareClickListener(mRightImgUrl));
viewHolderRight.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));
viewHolderRight.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
viewHolderRight.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
if(!mFeedViewData.getLikeCount().equals("0"))
viewHolderRight.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
if(!mFeedViewData.getShareCount().equals("0"))
viewHolderRight.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
if(!mFeedViewData.getRevCount().equals("0"))
viewHolderRight.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");
int minutes=0;
if(mFeedViewData.getTimestamp()!=""){
long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
minutes = (int)((int) (timeDiff/1000))/60;
}
if(mFeedViewData.getUsername().length()>0){
ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderRight.userImg,new UserImageListener(viewHolderRight.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
viewHolderRight.userActivity.setText(ss);
if(minutes!=0){
viewHolderRight.timeline.setText( minutes + " minutes ago");
}
viewHolderRight.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getReview()!=null){
ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderRight.userImg,new UserImageListener(viewHolderRight.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
viewHolderRight.userActivity.setText(ss);
if(minutes!=0){
viewHolderRight.timeline.setText( minutes + " minutes ago");
}
viewHolderRight.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getActivity().length()>0){
String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
viewHolderRight.userActivity.setText(activityText);
if(minutes!=0){
viewHolderRight.timeline.setText( minutes + " minutes ago");
}
viewHolderRight.userImg.setImageResource(R.drawable.ic_blank_profile);
}
if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
viewHolderRight.userActivity.setClickable(false);
viewHolderRight.objImg.setClickable(false);
}
}
else if(getItemViewType(arg0) == Utils.MAGAZINE_OBJECT){
MagazineViewHolder viewHolderMag = new MagazineViewHolder();
float imgWidth = width/2;
float descWidth = width;
float rowHeight = ((width)/2)*Utils.MAG_ASPECT_RATIO;
if(arg1==null){
LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
arg1 = inflater.inflate(R.layout.view_magazine, arg2, false);
viewHolderMag.row = arg1.findViewById(R.id.rowLayout);
viewHolderMag.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
viewHolderMag.magImgLeft = (ImageView)arg1.findViewById(R.id.img_object_1);
viewHolderMag.magImgRight = (ImageView)arg1.findViewById(R.id.img_object_2);
viewHolderMag.price = (TextView)arg1.findViewById(R.id.text_price);
viewHolderMag.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
viewHolderMag.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);
viewHolderMag.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
viewHolderMag.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);
viewHolderMag.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
viewHolderMag.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);
viewHolderMag.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
viewHolderMag.userImg = (ImageView)arg1.findViewById(R.id.img_user);
viewHolderMag.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
viewHolderMag.timeline = (TextView)arg1.findViewById(R.id.text_timeline);
LinearLayout.LayoutParams imgLp1 = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams((int)imgWidth,(int)rowHeight));
imgLp1.gravity = Gravity.CENTER;
viewHolderMag.magImgLeft.setLayoutParams(imgLp1);
viewHolderMag.magImgRight.setLayoutParams(imgLp1);
RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)descWidth,(int)rowHeight));
viewHolderMag.imagesLayout.setLayoutParams(rlLp);
ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgLeft,(int) imgWidth,(int)rowHeight);
ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgRight,(int) imgWidth,(int)rowHeight);
arg1.setTag(viewHolderMag);
}
else{
viewHolderMag = (MagazineViewHolder)arg1.getTag();
ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgLeft,(int) imgWidth,(int)rowHeight);
ImageSizeUtils.defineTargetSizeForView(viewHolderMag.magImgRight,(int) imgWidth,(int)rowHeight);
}
String mImgUrlLeft = Utils.m_img_mag_base_url+mFeedViewData.getFileidn().split("-")[1]+".jpg";
String mImgUrlRight = Utils.m_img_mag_base_url+mFeedViewData.getFileidn().split("-")[2];
try{
ImageLoader.getInstance().displayImage(mImgUrlLeft, viewHolderMag.magImgLeft,
new MagazineImageListener(viewHolderMag.magImgLeft));
ImageLoader.getInstance().displayImage(mImgUrlRight, viewHolderMag.magImgRight,
new MagazineImageListener(viewHolderMag.magImgRight));
}
catch(Exception e){
return null;
}
viewHolderMag.imagesLayout.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));
viewHolderMag.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
viewHolderMag.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
//TODO: Remove once mag has price
viewHolderMag.price.setVisibility(View.GONE);
if(!mFeedViewData.getLikeCount().equals("0"))
viewHolderMag.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
if(!mFeedViewData.getShareCount().equals("0"))
viewHolderMag.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
if(!mFeedViewData.getRevCount().equals("0"))
viewHolderMag.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");
if(mFeedViewData.getIsLoved())
viewHolderMag.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
else
viewHolderMag.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);
viewHolderMag.shareActionLayout.setOnClickListener(new ShareClickListener(mFeedViewData.getMagImage()));
viewHolderMag.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderMag.likeActionLayout));
viewHolderMag.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));
int minutes=0;
if(mFeedViewData.getTimestamp()!=""){
long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
minutes = (int)((int) (timeDiff/1000))/60;
}
if(mFeedViewData.getUsername().length()>0){
ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderMag.userImg,new UserImageListener(viewHolderMag.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
viewHolderMag.userActivity.setText(ss);
if(minutes!=0){
viewHolderMag.timeline.setText( minutes + " minutes ago");
}
viewHolderMag.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getReview()!=null){
ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderMag.userImg,new UserImageListener(viewHolderMag.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
viewHolderMag.userActivity.setText(ss);
if(minutes!=0){
viewHolderMag.timeline.setText( minutes + " minutes ago");
}
viewHolderMag.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getActivity().length()>0){
String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
viewHolderMag.userActivity.setText(activityText);
if(minutes!=0){
viewHolderMag.timeline.setText( minutes + " minutes ago");
}
viewHolderMag.userImg.setImageResource(R.drawable.ic_blank_profile);
}
if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
viewHolderMag.userActivity.setClickable(false);
viewHolderMag.imagesLayout.setClickable(false);
}
}
else if(getItemViewType(arg0) == Utils.SCRAP_OBJECT){
ObjectViewHolder viewHolderScrap = new ObjectViewHolder();
float imgWidth = width;
float rowHeight = (width)*Utils.SCRAPBOOK_ASPECT_RATIO;
if(arg1==null){
LayoutInflater inflater = ((Activity)mCtx).getLayoutInflater();
arg1 = inflater.inflate(R.layout.view_single_pane, arg2, false);
viewHolderScrap.row = arg1.findViewById(R.id.rowLayout);
viewHolderScrap.imagesLayout = (RelativeLayout)arg1.findViewById(R.id.layout_image);
viewHolderScrap.objImg = (ImageView)arg1.findViewById(R.id.img_object);
viewHolderScrap.price = (TextView)arg1.findViewById(R.id.text_price);
viewHolderScrap.likeActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_like);
viewHolderScrap.likeCount = (TextView)arg1.findViewById(R.id.text_like_count);
viewHolderScrap.shareActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_share);
viewHolderScrap.shareCount = (TextView)arg1.findViewById(R.id.text_share_count);
viewHolderScrap.commentActionLayout = (RelativeLayout)arg1.findViewById(R.id.layout_action_comment);
viewHolderScrap.commentCount = (TextView)arg1.findViewById(R.id.text_share_comment);
viewHolderScrap.activityLayout = (LinearLayout)arg1.findViewById(R.id.layout_activity);
viewHolderScrap.userImg = (ImageView)arg1.findViewById(R.id.img_user);
viewHolderScrap.userActivity = (TextView)arg1.findViewById(R.id.text_activity);
viewHolderScrap.timeline = (TextView)arg1.findViewById(R.id.text_timeline);
RelativeLayout.LayoutParams imgLp1 = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
viewHolderScrap.objImg.setLayoutParams(imgLp1);
RelativeLayout.LayoutParams rlLp = new RelativeLayout.LayoutParams(
new RelativeLayout.LayoutParams((int)imgWidth,(int)rowHeight));
viewHolderScrap.imagesLayout.setLayoutParams(rlLp);
ImageSizeUtils.defineTargetSizeForView(viewHolderScrap.objImg,(int) imgWidth,(int)rowHeight);
arg1.setTag(viewHolderScrap);
}
else{
viewHolderScrap = (ObjectViewHolder)arg1.getTag();
ImageSizeUtils.defineTargetSizeForView(viewHolderScrap.objImg,(int) imgWidth,(int)rowHeight);
}
String mNormalImgUrl = null;
mNormalImgUrl = Utils.m_img_scrap_base_url + mFeedViewData.getScrapId() + ".png";
try{
ImageLoader.getInstance().displayImage(mNormalImgUrl, viewHolderScrap.objImg,
new SingleImageListener(mFeedViewData,viewHolderScrap.objImg));
}
catch(Exception e){
return null;
}
viewHolderScrap.objImg.setOnClickListener(new ProdImageOnClickListener(mFeedViewData));
if(mFeedViewData.getIsLoved())
viewHolderScrap.likeActionLayout.setBackgroundColor(mCtx.getResources().getColor(R.color.auth_btn_color_normal));
else
viewHolderScrap.likeActionLayout.setBackgroundResource(R.drawable.selector_pink_button);
viewHolderScrap.likeActionLayout.setOnClickListener(new LikeActionListener(mFeedViewData,viewHolderScrap.likeActionLayout));
viewHolderScrap.shareActionLayout.setOnClickListener(new ShareClickListener(mNormalImgUrl));
viewHolderScrap.commentActionLayout.setOnClickListener(new OnCommentClickListener(mFeedViewData));
viewHolderScrap.price.setTypeface(Utils.getFontForRupeeSymb(mCtx));
viewHolderScrap.price.setText(Html.fromHtml( "` " + "</font>" + "<b>" + mFeedViewData.getPrice() + "</b>"));
//TODO:Remove once scrap has price
viewHolderScrap.price.setVisibility(View.GONE);
if(!mFeedViewData.getLikeCount().equals("0"))
viewHolderScrap.likeCount.setText(" (" + mFeedViewData.getLikeCount() + ")");
if(!mFeedViewData.getShareCount().equals("0"))
viewHolderScrap.shareCount.setText(" (" + mFeedViewData.getShareCount() + ")");
if(!mFeedViewData.getRevCount().equals("0"))
viewHolderScrap.commentCount.setText(" (" + mFeedViewData.getRevCount() + ")");
int minutes=0;
if(mFeedViewData.getTimestamp()!=""){
long timeDiff = System.currentTimeMillis() - Long.parseLong(mFeedViewData.getTimestamp());
minutes = (int)((int) (timeDiff/1000))/60;
}
if(mFeedViewData.getUsername().length()>0 && (!mFeedViewData.getActivity().equals("null")) && mFeedViewData.getActivity().length()>0){
ImageLoader.getInstance().displayImage(mFeedViewData.getTnPic(), viewHolderScrap.userImg,new UserImageListener(viewHolderScrap.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getUsername() + "</b>" + " "+ mFeedViewData.getActivity()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getDoer()), 0, mFeedViewData.getUsername().length(), Spanned.SPAN_COMPOSING);
viewHolderScrap.userActivity.setText(ss);
if(minutes!=0){
viewHolderScrap.timeline.setText( minutes + " minutes ago");
}
viewHolderScrap.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getReview()!=null){
ImageLoader.getInstance().displayImage(mFeedViewData.getReview().getTnUrl(), viewHolderScrap.userImg,new UserImageListener(viewHolderScrap.userImg));
String activityText = Html.fromHtml("<b>" + mFeedViewData.getReview().getReviewerName() + "</b>" + " "+ mFeedViewData.getReview().getReview()).toString();
SpannableString ss = new SpannableString(activityText);
ss.setSpan(new MyClickableSpan(activityText, mFeedViewData.getReview().getId()), 0, mFeedViewData.getReview().getReviewerName().length(), Spanned.SPAN_COMPOSING);
viewHolderScrap.userActivity.setText(ss);
if(minutes!=0){
viewHolderScrap.timeline.setText( minutes + " minutes ago");
}
viewHolderScrap.userActivity.setMovementMethod(LinkMovementMethod.getInstance());
}
else if(mFeedViewData.getActivity().length()>0){
String activityText = Html.fromHtml("<b>" + "A user: "+ "</b>" + " "+ mFeedViewData.getActivity()).toString();
viewHolderScrap.userActivity.setText(activityText);
if(minutes!=0){
viewHolderScrap.timeline.setText( minutes + " minutes ago");
}
viewHolderScrap.userImg.setImageResource(R.drawable.ic_blank_profile);
}
if(feedType == Utils.ADAPTER_FEED_TUTORIAL){
viewHolderScrap.userActivity.setClickable(false);
viewHolderScrap.objImg.setClickable(false);
}
}
return arg1;
}
Make sure you do not load or process any images in Adapter.getView() method. If you need to do so, do it in a separate AsyncTask. If you download images from Internet you should better use some helper libraries like Volley or Picasso
I use this library to load images in list view, I never had a problem:Android Image Loader
Related
Android sharedpreferences getstring
code : editor.putString("linkid",a); editor.apply(); final Intent intent1 = new Intent(getActivity(), thirdreq.class); startActivity(intent1); for(int d=0;d<Integer.valueOf(dcountpeople[myid]);d++){ did2[d] = pref.getString("did2" + "[" + d + "]",null); dformname2[d] = pref.getString("dformname2" + "[" + d + "]", null); dregdate2[d] = pref.getString("dregdate2" + "[" + d + "]", null); Log.i("Res:", did2[d] + ":" + dformname2[d] + ":" + dregdate2[d]); txtshowpeoplearray[d].setVisibility(View.VISIBLE); txtpeopleshowdatearray[d].setVisibility(View.VISIBLE); btnpeopleviewarray[d].setVisibility(View.VISIBLE); btnpeopledeletearray[d].setVisibility(View.VISIBLE); txtshowpeoplearray[d].setText(dformname2[d]); txtpeopleshowdatearray[d].setText(dregdate2[d]); } these codes into a Button click listener and pref.getstrings works Properly when runs for 2 times(button clicked for 2 times) !
Why text show in listview after scroll?
Below is my calender day view adapter.Now what I want that data is coming from server but its showing when I scroll listview. I want to show data when activity launch whithout scroll. How can I achieve this ? public class SearchListAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private List<Details> movieItems; public SearchListAdapter(Activity activity, List<Details> movieItems) { this.activity = activity; this.movieItems = movieItems; } #Override public int getCount() { return HOURS_PER_DAY; } #Override public Object getItem(int location) { return movieItems.get(location); } #Override public long getItemId(int position) { return position; } #Override public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); convertView = (View) inflater.inflate(R.layout.list_item_appointment, listView, false); TextView hourTV = (TextView) convertView.findViewById(R.id.hourTV); TextView amTV = (TextView) convertView.findViewById(R.id.amTV); hourTV.setTextColor(Color.parseColor("#2c3c53")); amTV.setTextColor(Color.parseColor("#2c3c53")); final LinearLayout eventsLL = (LinearLayout) convertView.findViewById(R.id.eventsLL); int val = position % 24; Typeface face = Typeface.createFromAsset(getAssets(), "fonts/centurygothic.ttf"); hourTV.setTypeface(face); amTV.setTypeface(face); if (val < 10) { hourTV.setText(String.valueOf((val + 6) + ":" + "00")); } else { hourTV.setText(String.valueOf((val + 6) + ":" + "00")); } if (val == 7) hourTV.setText("1" + ":" + "00"); if (val == 8) hourTV.setText("2" + ":" + "00"); if (val == 9) hourTV.setText("3" + ":" + "00"); if (val == 10) hourTV.setText("4" + ":" + "00"); if (val == 11) hourTV.setText("5" + ":" + "00"); if (val == 12) hourTV.setText("6" + ":" + "00"); if (val == 13) hourTV.setText("7" + ":" + "00"); if (val == 14) hourTV.setText("8" + ":" + "00"); if (val == 15) hourTV.setText("9" + ":" + "00"); if (val == 16) hourTV.setText("10" + ":" + "00"); if (val == 17) hourTV.setText("11" + ":" + "00"); if ((position + 6 >= 0) && (position + 6 < 12)) amTV.setText("am"); else amTV.setText("pm"); if (details_list.size() > 0) { for (int i = 0; i < details_list.size(); i++) { String[] innerData2 = details_list.get(i).getTime().split(":"); str = innerData2[0]; try { int s = ((position) % 24) + 6; if ((s) == Integer.parseInt(str)) { TextView rowTextView = new TextView(AppointmentDayView.this); indexmap_aptid.put(position, details_list.get(i).getAppointmentId()); indexmap_clocid.put(position, details_list.get(i).getClientLocationId()); indexmap_appDate.put(position, details_list.get(i).getStartAppointmentDate()); rowTextView.setPadding(10, 20, 10, 20); rowTextView.setBackgroundResource(R.drawable.borderdayview); rowTextView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); rowTextView.setTextSize(14); rowTextView.setTypeface(face); rowTextView.setTypeface(Typeface.DEFAULT_BOLD); String start_time = "", end_time = ""; try { String dt = details_list.get(i).getTime(); String end = details_list.get(i).getEndtime(); DateFormat df = new SimpleDateFormat("hh:mm:ss"); DateFormat dft = new SimpleDateFormat("hh:mm a"); Date dts = df.parse(dt); Date end_t = df.parse(end); start_time = dft.format(dts); end_time = dft.format(end_t); } catch (ParseException e) { e.printStackTrace(); } rowTextView.setText("Name : " + details_list.get(i).getName() + "\n" + "Time " + start_time + " To " + end_time + "\n" + "Reason : " + details_list.get(i).getReason() + " "); eventsLL.addView(rowTextView); } for (int k = 0; k < details_list.size(); k++) { String in[] = details_list.get(k).getTime().split(":"); String out[] = details_list.get(k).getEndtime().split(":"); int stime = Integer.parseInt(in[0]); int end = Integer.parseInt(out[0]); int start = stime + 1; while (start <= end) { int q = ((position) % 24) + 6; if ((q) == start) { TextView rowTextView = new TextView(AppointmentDayView.this); rowTextView.setPadding(10, 20, 10, 20); rowTextView.setBackgroundResource(R.drawable.borderdayview); rowTextView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); rowTextView.setTextSize(14); rowTextView.setTypeface(face); rowTextView.setTypeface(Typeface.DEFAULT_BOLD); eventsLL.addView(rowTextView); } start++; } } } catch (ArrayIndexOutOfBoundsException e) { } } } return convertView; } }
Get Toolbar in dynamically added Text view in Linear Layout
In my app I am fetching the order details from the mysql db and based on the number of rows in db I am dynamically adding the TextView in my LinearLayout in the app(if there are 6 rows then 6 text views will be displayed in the app) In doing the toolbar is disabled from the Activity and due to this i am NOT able to go back to my parent activity I am NOT sure but I think using setLayoutParams is the culprit here. Please help! public class OrderHistory extends AppCompatActivity { private EditText editTextId; private Button buttonGet; private TextView textViewResult; private ProgressDialog loading; private ScrollView scrollView; private LinearLayout linearLayout; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order_history); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); getData(); //get data from the db } private void getData() { //getting data from db } private void showJSON(String response){ try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray(Constants.JSON_ARRAY); this.scrollView = (ScrollView) findViewById(R.id.scrollableContents); this.linearLayout = (LinearLayout) findViewById(R.id.linear); this.linearLayout.setOrientation(LinearLayout.VERTICAL); TextView[] t1 = new TextView[result.length()]; TextView[] t2 = new TextView[result.length()]; TextView[] t3 = new TextView[result.length()]; TextView[] t4 = new TextView[result.length()]; TextView[] t5 = new TextView[result.length()]; ImageView[] img = new ImageView[result.length()]; if(result.length()!=0) { for (int i = 0; i < result.length(); i++) { JSONObject collegeData = result.getJSONObject(i); int orderTotal = Integer.parseInt(collegeData.getString(Constants.KEY_AMOUNT).split("\\ ")[0]) - 49; int total = orderTotal + 49; switch (collegeData.getString(Constants.KEY_CCAvenueOrderStatus)) { case "Success": t1[i] = new TextView(this); t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); //when this is execute the TOOLBAR disappears #i guess String success = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " was received by Horoscope Daily !"; t1[i].setTextColor(getResources().getColor(R.color.white)); t1[i].setTextSize(17); t1[i].setText(Html.fromHtml("<h4>" + success + "</h4>" + "Your transaction was successfull<br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>")); this.linearLayout.addView(t1[i]); break; case "Failure": t1[i] = new TextView(this); t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); String failure = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " failed !"; t1[i].setTextColor(getResources().getColor(R.color.white)); t1[i].setTextSize(17); t1[i].setText(Html.fromHtml("<h4>" + failure + "</h4>" + "Your payment has been declined by your bank.Please contact your bank for any queries.If money has been deducted from your account,your bank will inform us within 48 hrs and we will refund the same<br><br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>")); this.linearLayout.addView(t1[i]); break; case "Aborted": t1[i] = new TextView(this); t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); String aborted = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " failed !"; t1[i].setTextColor(getResources().getColor(R.color.white)); t1[i].setTextSize(17); t1[i].setText(Html.fromHtml("<h4>" + aborted + "</h4>" + "Your payment has been declined by your bank as the OTP(one time password) entered is incorrect.Please try again with the correct OTP or contact your bank for any queries.<br><br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>")); this.linearLayout.addView(t1[i]); break; default: } t3[i] = new TextView(this); t3[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); t3[i].setTextColor(getResources().getColor(R.color.white)); t3[i].setTextSize(17); t3[i].setText(Html.fromHtml("<h4>You have below Items in your order</h4> <br>" + collegeData.getString(Constants.KEY_ITEM) + " " + "₹ " + orderTotal + " " + collegeData.getString(Constants.KEY_RATTI) + " Ratti" + " " + collegeData.getString(Constants.KEY_QUANTITY) + " Quantity" + "<br>")); this.linearLayout.addView(t3[i]); switch (collegeData.getString(Constants.KEY_ITEM)) { case "Coral": String uri = "#drawable/ic_coral_moonga"; // where myresource (without the extension) is the file int imageResource = getResources().getIdentifier(uri, null, getPackageName()); img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; Drawable res = getResources().getDrawable(imageResource); img[i].setImageDrawable(res); this.linearLayout.addView(img[i]); break; case "Opal": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_opal", null, getPackageName()))); this.linearLayout.addView(img[i]); break; case "Emerald": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_emerald_panna", null, getPackageName()))); this.linearLayout.addView(img[i]); break; case "Pearl": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_pearl_moti", null, getPackageName()))); this.linearLayout.addView(img[i]); break; case "Ruby": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_ruby_manikya", null, getPackageName()))); this.linearLayout.addView(img[i]); break; case "Yellow Sapphire": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_yellow_sapphire_pikhraj", null, getPackageName()))); this.linearLayout.addView(img[i]); break; case "Blue Sapphire": img[i] = new ImageView(this); img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); img[i].getLayoutParams().height = 200; img[i].getLayoutParams().width = 200; img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("#drawable/ic_blue_sapphire", null, getPackageName()))); this.linearLayout.addView(img[i]); break; default: break; } t4[i] = new TextView(this); t4[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); t4[i].setTextColor(getResources().getColor(R.color.white)); t4[i].setTextSize(17); t4[i].setText(Html.fromHtml("<br><h4>Payment Details</h4> CC Tracking ID <br>" + collegeData.getString(Constants.KEY_CCAvenueTacking_id) + "<br>")); this.linearLayout.addView(t4[i]); t5[i] = new TextView(this); t5[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); t5[i].setText(Html.fromHtml("<h4>Summary</h4> <br>" + "Order Total" + " " + "₹ " + orderTotal + "<br>" + "Shipping" + " " + "₹ 49" + "<br>" + "Total" + " " + "₹ " + total + "<br>")); t5[i].setTextColor(getResources().getColor(R.color.white)); t5[i].setTextSize(17); this.linearLayout.addView(t5[i]); View v = new View(this); v.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 5 )); v.setBackgroundColor(Color.parseColor("#B3B3B3")); LinearLayout.LayoutParams margin = (LinearLayout.LayoutParams) v.getLayoutParams(); margin.setMargins(0, 50, 0, 50); v.setLayoutParams(margin); this.linearLayout.addView(v); } if (this.scrollView.getParent() != null) ((ViewGroup) this.scrollView.getParent()).removeView(this.scrollView); setContentView(this.scrollView); } else{ Toast.makeText(getApplicationContext(), "No Order History\n"+"Our database indicate that you don't have any orders yet !", Toast.LENGTH_SHORT).show(); // hide the progress dialog } } catch (JSONException e) { e.printStackTrace(); } // textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc); } }
Remove this code it is not needed: if (this.scrollView.getParent() != null) ((ViewGroup)this.scrollView.getParent()).removeView(this.scrollView); setContentView(this.scrollView); Move reading from DB to AsyncTask - because you are blocking UI thread on call to getData() and Application freezes durring loading. Also you need think about using proper container like ListView or RecycleView
how to initialize Time variable android
i searched a lot to find how initialize Time variables in my Android project, i have tried to use Set(); methode but doesn't work, i need your help thank's ` here is my source code : TableRow tableRow; Time HourProgram; int H = 12000, M = 1200; int averageConsultationTime = 30; // String // TableSettinHourgHeader=""+HourBegen+"H"+averageConsultationTime+"|" // +HourBegen+"H"+averageConsultationTime+"|" #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.table); Log.i("OK", "onCreateOK"); Log.i("OK", "HMOK"); TextView DynamicButton = new TextView(MainActivity.this); tableRow = (TableRow) findViewById(R.id.tr1); LayoutParams layoutparams = new TableRow.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); DynamicButton.setLayoutParams(layoutparams); String ss = converteur(H, M); Log.i("OK", "converteur(H, M)OK" + ss); DynamicButton.setText("Assalam Aleikum " + ss); Log.i("OK", "setTextOK" + ss); tableRow.addView(DynamicButton); } #SuppressWarnings("unused") private String converteur(int timeBegen, int averageConsultationTime) { String sTb = "" + String.valueOf(timeBegen).toString(); String sAct = "" + String.valueOf(this.averageConsultationTime).toString(); // Toast.makeText(this, // ""+timeBegen+"H"+averageConsultationTime,Toast.LENGTH_LONG); return "timeBegen : " + sTb + " averageConsultationTime : " + averageConsultationTime; } `
For java.sql.Time It should be Time HourProgram = new Time(System.currentTimeMillis()); Also, if you prefer Calendar then, Time HourProgram = new Time(Calendar.getInstance().getTimeInMillis());
Radio Group remembers old items
In my "FilterChoice" activity, I create the layout dynamically. I create a Radio Group and add Radio Buttons in it depending on the size of an ArrayList. There's a list in my "Filters" activity. The radio buttons are different depending on the filters clicked. When I click "Prices" filter, it views six items on the "FilterChoice" activity. Then pressing back button, when I click "Categories" filter, the checkedId's in the onCheckedChanged() starts from seven, instead of one. Why does it not start from one in the new activity call? How to solve it? Here's my "FilterChoice" activity. ` public class FilterChoice extends AppCompatActivity { RadioButton rb; String filter; String selection, domain, temp; RadioGroup rg; public static ArrayList<KeyValuePair> cate_old; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(FilterChoice.this); LinearLayout.LayoutParams lrp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); linearLayout.setLayoutParams(lrp); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // rb = new RadioButton(FilterChoice.this); // rb.setText("All"); // rg.addView(rb); Intent intent = getIntent(); filter = intent.getExtras().getString("filter"); if (filter.equals("Price Range")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.prices.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.price_s_sel + "-" + SearchResults.price_e_sel + " (" + SearchResults.price_val + ")"); rg.addView(rb); } else { int it; for (it = 0; it < SearchResults.prices.size() - 1; it++) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.prices.get(it).key + "-" + SearchResults.prices.get(it + 1).key + " (" + SearchResults.prices.get(it).value + ")"); rg.addView(rb); } rb = new RadioButton(FilterChoice.this); rb.setText("Above" + SearchResults.prices.get(it).key + " (" + SearchResults.prices.get(it).value + ")"); rg.addView(rb); } } else if (filter.equals("Sites")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.site.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.site_sel + " (" + SearchResults.site_val + ")"); rg.addView(rb); // } } else { StringTokenizer st; for (int i = 0; i < SearchResults.site.size(); i++) { st = new StringTokenizer(SearchResults.site.get(i).key, "."); if (st.hasMoreTokens()) { domain = st.nextToken(); } rb = new RadioButton(FilterChoice.this); rb.setText(domain + " (" + SearchResults.site.get(i).value + ")"); rg.addView(rb); } } } else if (filter.equals("Categories")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.cate.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.cate_sel + " (" + SearchResults.cate_val + ")"); rg.addView(rb); // } } else { for (int i = 0; i < SearchResults.cate.size(); i++) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.cate.get(i).key + " (" + SearchResults.cate.get(i).value + ")"); rg.addView(rb); } } } else if (filter.equals("Colors")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.cols.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.cols_sel + " (" + SearchResults.cols_val + ")"); rg.addView(rb); } else { for (int i = 0; i < SearchResults.cols.size(); i++) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.cols.get(i).key + " (" + SearchResults.cols.get(i).value + ")"); rg.addView(rb); } } } else if (filter.equals("Brands")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.brands.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.brand_sel + " (" + SearchResults.brand_val + ")"); rg.addView(rb); } else { for (int i = 0; i < SearchResults.brands.size(); i++) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.brands.get(i).key + " (" + SearchResults.brands.get(i).value + ")"); rg.addView(rb); } } } else if (filter.equals("Sub Categories")) { rg = new RadioGroup(FilterChoice.this); rg.setLayoutParams(lp); if (SearchResults.subcate.size() == 1) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.subcate_sel + " (" + SearchResults.subcate_val + ")"); rg.addView(rb); } else { for (int i = 0; i < SearchResults.subcate.size(); i++) { rb = new RadioButton(FilterChoice.this); rb.setText(SearchResults.subcate.get(i).key + " (" + SearchResults.subcate.get(i).value + ")"); rg.addView(rb); } } } linearLayout.addView(rg); setContentView(linearLayout); rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { #Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (filter.equals("Price Range")) { } else if (filter.equals("Sites")) { Log.e("checkedId", "" + checkedId); if (SearchResults.site.size() > 1) { selection = SearchResults.site.get(checkedId - 1).key; temp = SearchResults.site.get(0).key; SearchResults.site_sel = selection; SearchResults.site_val = SearchResults.site .get(checkedId - 1).value; SearchResults.site_id = checkedId; } } else if (filter.equals("Categories")) { Log.e("checkedId", "" + checkedId); if (SearchResults.cate.size() > 1) { selection = SearchResults.cate.get(checkedId - 1).key; temp = SearchResults.cate.get(0).key; SearchResults.cate_sel = selection; SearchResults.cate_val = SearchResults.cate .get(checkedId - 1).value; SearchResults.cate_id = checkedId; } } else if (filter.equals("Colors")) { Log.e("checkedId", "" + checkedId); if (SearchResults.cols.size() > 1) { selection = SearchResults.cols.get(checkedId - 1).key; temp = SearchResults.cols.get(0).key; SearchResults.cols_sel = selection; SearchResults.cols_val = SearchResults.cols .get(checkedId - 1).value; SearchResults.cols_id = checkedId; } } else if (filter.equals("Brands")) { Log.e("checkedId", "" + checkedId); if (SearchResults.brands.size() > 1) { selection = SearchResults.brands.get(checkedId - 1).key; temp = SearchResults.brands.get(0).key; SearchResults.brand_sel = selection; SearchResults.brand_val = SearchResults.brands .get(checkedId - 1).value; SearchResults.brand_id = checkedId; } } else if (filter.equals("Sub Categories")) { Log.e("checkedId", "" + checkedId); if (SearchResults.subcate.size() > 1) { selection = SearchResults.subcate.get(checkedId - 1).key; temp = SearchResults.subcate.get(0).key; SearchResults.subcate_sel = selection; SearchResults.subcate_val = SearchResults.subcate .get(checkedId - 1).value; SearchResults.subcate_id = checkedId; } } Intent intent = new Intent(FilterChoice.this, Filters.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } } `
I have not yet figured out the behavior you describe, but I would point out that you can set the IDs of the RadioButtons you're creating dynamically. For example, in your "Categories" block: for (int i = 0; i < SearchResults.cate.size(); i++) { rb = new RadioButton(FilterChoice.this); rb.setId(i + 1); rb.setText(SearchResults.cate.get(i).key + " (" + SearchResults.cate.get(i).value + ")"); rg.addView(rb); } This will set IDs from 1 to SearchResults.cate.size(), which I believe is what you're going for.