Save State of RecyclerView - android

I am making a app with RecylerView populated with data form an online server. It has a custom layout with a favorite button that when clicked the icon changes.I have a problem trying to save state of a selected view on the RecyclerView. The RecyclerView does not save the selected state on scrolling back up. Kindly help.
Model Class
package com.smartdevelopers.kandie.nicedrawer.newsModel;
/**
* Created by 4331 on 25/09/2015.
*/
public class Latest {
public String excerpt;
public String articleImage;
public String articleUrl;
public boolean mfavourite;
public boolean isFavourite(){
return mfavourite;
}
public String getArticleUrl() {
return articleUrl;
}
public void setArticleUrl(String articleUrl) {
this.articleUrl = articleUrl;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getArticleImage() {
return articleImage;
}
public void setArticleImage(String articleImage) {
this.articleImage = articleImage;
}
}
Adapter
package com.smartdevelopers.kandie.nicedrawer.newsAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartdevelopers.kandie.nicedrawer.R;
import com.smartdevelopers.kandie.nicedrawer.ReadArticleActivity;
import com.smartdevelopers.kandie.nicedrawer.newsModel.Latest;
import com.smartdevelopers.kandie.nicedrawer.util.SharedPreferenceRecycler;
import java.util.HashMap;
import java.util.List;
/**
* Created by 4331 on 29/09/2015.
*/
public class OtherNewsAdapter extends RecyclerView.Adapter<OtherNewsAdapter.ViewHolder> {
private List<Latest> feedItemList;
private Context mContext;
private static final String TAG = "ActivityGplus";
SharedPreferenceRecycler sharedPreference = new SharedPreferenceRecycler();
public OtherNewsAdapter(Context context, List<Latest> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_other_news, null);
ViewHolder mh = new ViewHolder(v);
return mh;
}
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final Latest feedItem = feedItemList.get(i);
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.placeholder)
.showImageOnFail(R.drawable.placeholder)
.showImageOnLoading(R.drawable.placeholder).build();
//download and display image from url
imageLoader.displayImage(feedItem.getArticleImage(), viewHolder.thumbnail, options);
// Glide.with(mContext).load(feedItem.getArticleImage())
// .error(R.drawable.placeholder)
// .placeholder(R.drawable.placeholder)
// .into(viewHolder.thumbnail);
viewHolder.title.setText(Html.fromHtml(feedItem.getExcerpt()));
viewHolder.articleUrl.setText(Html.fromHtml(feedItem.getArticleUrl()));
viewHolder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ReadArticleActivity.class);
intent.putExtra("articleUrl", viewHolder.articleUrl.getText().toString());
mContext.startActivity(intent);
}
});
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(feedItem)) {
viewHolder.favImage.setImageResource(R.drawable.heart_red);
viewHolder.favImage.setSelected(true);
viewHolder.favImage.setTag("red");
hashMap.get(i);
}
else {
viewHolder.favImage.setImageResource(R.drawable.heart_grey);
viewHolder.favImage.setSelected(false);
viewHolder.favImage.setTag("grey");
}
viewHolder.title.setTag(viewHolder);
viewHolder.thumbnail.setId(R.id.otherImage);
viewHolder.articleUrl.setTag(viewHolder);
}
#Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
OtherNewsRowHolder holder = (OtherNewsRowHolder) view.getTag();
int position = holder.getPosition();
Latest feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getExcerpt(), Toast.LENGTH_SHORT).show();
}
};
/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Latest checkProduct) {
boolean check = false;
List<Latest> favorites = sharedPreference.getFavorites(mContext);
if (favorites != null) {
for (Latest product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView thumbnail, favImage;
protected TextView title,articleUrl;
public ViewHolder(View itemView) {
super(itemView);
this.thumbnail = (ImageView) itemView.findViewById(R.id.otherImage);
this.title = (TextView) itemView.findViewById(R.id.otherExcerpt);
this.articleUrl=(TextView)itemView.findViewById(R.id.otherUrl);
this.favImage = (ImageView) itemView.findViewById(R.id.imgbtn_favorite);
favImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String tag = favImage.getTag().toString();
if(!favImage.isSelected()) {
if (tag.equalsIgnoreCase("grey")) {
sharedPreference.addFavorite(mContext, feedItemList.get(getItemCount() - 1));
// Toast.makeText(mContext, mContext.getResources().getString(R.string.add_favr),
// Toast.LENGTH_SHORT).show();
//SnackBack
SnackbarManager.show(
Snackbar.with(mContext)
.text(R.string.add_favr)
.textColor(Color.WHITE)
.color(Color.RED)
.duration(Snackbar.SnackbarDuration.LENGTH_SHORT));
//End of SnackBack
favImage.setTag("red");
favImage.setImageResource(R.drawable.heart_red);
}
}
else {
sharedPreference.removeFavorite(mContext, feedItemList.get(getItemCount() - 1));
favImage.setTag("grey");
favImage.setImageResource(R.drawable.heart_grey);
//SnackBack
SnackbarManager.show(
Snackbar.with(mContext)
.text(R.string.remove_favr)
.textColor(Color.WHITE)
.color(Color.RED) .duration(Snackbar.SnackbarDuration.LENGTH_SHORT).animation(false));
//End of SnackBack
}
}
}
}

I ran into this problem a couple of days ago - the reason is that you have not updated your feedItemList with the applicable changes. If you update the list by setting some of the member variables (like setExcerpt()) of the Latest class once it has change, your recyclerview will work properly.

Related

This error is coming while doing intent Cannot resolve method 'putExtra(java.lang.String, <lambda parameter>)'?

I am getting this error when doing intent. I don't know why it is coming. I need to go to the fragment to activity. I need to go to the next activity with the api in this application. I have tried many times and I am not getting the answer.**Cannot resolve method 'putExtra(java.lang.String, <lambda parameter>)'**I have given the image below How to do fragment to activity intent i am new android devloper
package com.kannada.newspaper.india.utils;
import static com.kannada.newspaper.india.Constant.EXTRA_OBJC;
import static com.kannada.newspaper.india.Constant.getApiUrl;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.kannada.newspaper.india.AppConfig;
import com.kannada.newspaper.india.Constant;
import com.kannada.newspaper.india.MainActivity;
import com.kannada.newspaper.india.R;
import com.kannada.newspaper.india.activities.ActivityCategoryDetail;
import com.kannada.newspaper.india.adapters.GalleryAdapter;
import com.kannada.newspaper.india.model.Category;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class FragmentCategory extends Fragment {
private Call<CallbackHome> callbackCall = null;
SharedPref sharedPref;
private View root_view;
public static final String EXTRA_OBJC = "key.EXTRA_OBJC";
private GalleryAdapter adapterCategory;
private GridView gridView;
private List<Category> mensWears;
private GalleryAdapter adapter;
private Category category;
public FragmentCategory() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
requestAction();
category = (Category) getActivity().getIntent().getSerializableExtra(Constant.EXTRA_OBJC);
// Inflate the layout for this fragment
root_view = inflater.inflate(R.layout.fragment_phones,container,false);
((TextView) root_view.findViewById(R.id.txt_title_category)).setText(getResources().getString(R.string.home_title_category));
return root_view;
}
private void requestAction() {
new Handler().postDelayed(this::requestHomeData, Constant.DELAY_TIME);
}
private void requestHomeData() {
this.callbackCall = RestAdapter.createAPI(getApiUrl).getHome(AppConfig.REST_API_KEY);
this.callbackCall.enqueue(new Callback<CallbackHome>() {
public void onResponse(Call<CallbackHome> call, Response<CallbackHome> response) {
CallbackHome responseHome = response.body();
if (responseHome == null || !responseHome.status.equals("ok")) {
return;
}
displayData(responseHome);
}
public void onFailure(Call<CallbackHome> call, Throwable th) {
Log.e("onFailure", th.getMessage());
if (!call.isCanceled()) {
}
}
});
}
private void displayData(CallbackHome responseHome) {
displayCategory(responseHome.category);
}
private void displayCategory(List<Category> list) {
GridView gridView = (GridView) root_view.findViewById(R.id.gridHolder);
adapterCategory = new GalleryAdapter(getActivity(), list);
gridView.setAdapter(adapterCategory);
GalleryAdapter.setOnItemClickListener((v, obj, position) -> {
Intent intent = new Intent(getActivity(), ActivityCategoryDetail.class);
intent.putExtra(EXTRA_OBJC, obj);
startActivity(intent);
});
LinearLayout lyt_category = root_view.findViewById(R.id.lyt_category);
if (list.size() > 0) {
// lyt_category.setVisibility(View.VISIBLE);
} else {
// lyt_category.setVisibility(View.GONE);
}
}
}
GalleryAdapter adapter
public class GalleryAdapter extends BaseAdapter {
private Context context;
private List<Category> mensWears;
public GalleryAdapter(Context context, List<Category> mensWears) {
this.context = context;
this.mensWears = mensWears;
}
public static void setOnItemClickListener(Object o) {
}
#Override
public int getCount() {
return mensWears.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i,View view,ViewGroup viewGroup) {
final Category mensWear = mensWears.get(i);
if (view == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.custom_gallery_layout, null);
}
//For text
TextView prdId = view.findViewById(R.id.category_name);
ImageView imageView = view.findViewById(R.id.category_image);
// prdId.setText(prdId.toString());
Picasso.get()
.load(getApiUrl + "/upload/category/" + mensWears.get(i).category_image())
.placeholder(R.drawable.ic_thumbnail)
.into(imageView);
prdId.setText(mensWears.get(i).getItemName());
// //For images
// final ImageView imageView = view.findViewById(R.id.name);
// if(!TextUtils.isEmpty(mensWear.getItemName())){
//
//// Picasso.with(context).load(imageUrlFromServer+mensWear.category_image())
//// .into(imageView);
return view;
}
}
You cannot put Object type in putExtra , it has to be either serailized, string, double and other primitive type.
You can do like this:
Category category = (Category)adapter.getItemAtPosition(pos);
or this should also work:
Category category = (Category) obj
then,
intent.putExtra(EXTRA_OBJC,category)
Note: Your Category class should implement parcelable or serilizable to be passed as an intent.
public class Category implements Serializable {
..........}

Android RecyclerView and custom Adapter position elements changes

I have a RecylcerView filled with a Custom Adapter recylerView.setAdapter(myAdapter), and the adaper is filled with different elements by an ArrayList. In the ViewHolder i overrided the
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int position) method. In the custom ViewHolder i put a button. The button has a click listener on it. When i click the button i read the variable position and i am facing that the variable changes every time i click the same button.
Why this happens?
My Adapter is like this:
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.model.query.Where;
import com.amplifyframework.datastore.generated.model.Comments;
import com.amplifyframework.datastore.generated.model.Likes;
import com.amplifyframework.datastore.generated.model.Posts;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.firebase.auth.FirebaseAuth;
import java.net.URL;
import java.util.LinkedList;
import de.hdodenhof.circleimageview.CircleImageView;
import static com.amazonaws.mobile.auth.core.internal.util.ThreadUtils.runOnUiThread;
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHolder>
{
private LinkedList<Posts> usersPostsList;
private FirebaseAuth mAuth;
private Context context;
//private Boolean likeChecker;
private String currentUserId;
private MainActivity mainActivity;
private int currentPosition;
public PostsAdapter(Context context, String currentUserId, MainActivity mainActivity)
{
this.context = context;
this.currentUserId = currentUserId;
this.mainActivity = mainActivity;
}
public PostsAdapter(LinkedList<Posts> usersPostsList, Context context)
{
this.usersPostsList = usersPostsList;
this.context = context;
}
public void setMessages(LinkedList<Posts> usersPostsList) {
this.usersPostsList = usersPostsList;
}
public static class PostViewHolder extends RecyclerView.ViewHolder
{
View mView;
public static ImageButton LikePostButton, CommentPostButton;
public TextView DisplayNoOfLikes, modifyPost;
private TextView DisplayNoOfComments;
String currentUserID;
ExpandableTextView PostDescription;
private Context context;
public PostViewHolder(#NonNull View itemView, Context context)
{
super(itemView);
mView = itemView;
this.context = context;
LikePostButton = (ImageButton) mView.findViewById(R.id.like_button);
CommentPostButton = (ImageButton) mView.findViewById(R.id.comment_button);
DisplayNoOfLikes = (TextView) mView.findViewById(R.id.display_no_of_likes);
DisplayNoOfComments = (TextView) mView.findViewById(R.id.display_no_of_comments);
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
modifyPost = mView.findViewById(R.id.modify_post);
}
public void setLikeButtonStatus(final String PostKey) {
Amplify.DataStore.query(
Likes.class, Where.matches(Likes.POST_ID.eq(PostKey.trim()).and(Likes.SENDER.eq(currentUserID.trim()))),
items -> {
int countLikes = 0;
if (!items.hasNext()) {
LikePostButton.setImageResource(R.drawable.ic_star_border);
}
while (items.hasNext()) {
Likes item = items.next();
countLikes++;
LikePostButton.setImageResource(R.drawable.ic_star_fill);
Log.i("Amplify", "Id " + item.getId());
}
int finalCountLikes = countLikes;
DisplayNoOfLikes.setText(finalCountLikes + (" Likes"));
},
failure -> Log.e("Amplify", "Could not query DataStore", failure)
);
}
public void setFullname(String fullname)
{
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setProfileimage(Context ctx, String profileimage)
{
CircleImageView image = (CircleImageView) mView.findViewById(R.id.post_profile_image);
Amplify.Storage.getUrl(profileimage,
result -> {
URL url = result.getUrl();
runOnUiThread(() -> Glide.with(ctx)
.load(url)
.apply(new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true))
.into(image));
},
error -> Log.i("Amplify", "error while retrieving url"));
}
public void setTime(String time)
{
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date)
{
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description)
{
PostDescription = mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(Context ctx, String postimage, String id)
{
ImageView postImageInner = mView.findViewById(R.id.post_image);
Amplify.Storage.getUrl(postimage,
result -> {
URL url = result.getUrl();
runOnUiThread(() -> Glide.with(ctx)
.load(url)
.apply(new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true))
.into(postImageInner));
Log.i("Amplify---------", "url ok----");
},
error -> Log.e("Amplify---------", "error while retrieving url: " + error.getCause().toString()));
postImageInner.setOnClickListener(v -> {
Intent clickPostIntent = new Intent(context, ClickPostActivity.class);
clickPostIntent.putExtra("PostKey", id);
clickPostIntent.putExtra("postImagePath", postimage);
context.startActivity (clickPostIntent);
});
}
public void setCountry(String country)
{
TextView CountryName = (TextView) mView.findViewById(R.id.post_country_name);
CountryName.setText(country);
}
public void setCity(String city)
{
TextView City = (TextView) mView.findViewById(R.id.post_city_name);
City.setText("- " + city);
}
public void setCommentStatus(final String PostKey) {
Amplify.DataStore.query(
Comments.class, Where.matches(Comments.POST_ID.eq(PostKey.trim())),
items -> {
int i = 0;
while (items.hasNext()) {
Comments comments = items.next();
i++;
int finalI = i;
runOnUiThread(() -> DisplayNoOfComments.setText(finalI + " comments"));
Log.i("amplify?", "comment id: " + comments.getId());
}
},
failure -> Log.e("Amplify", "Could not query DataStore", failure)
);
}
}
#NonNull
#Override
public PostViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View V = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_posts_layout, parent,false);
mAuth = FirebaseAuth.getInstance();
return new PostViewHolder(V, context);
}
#Override
public int getItemViewType(int position) {
currentPosition = position;
return position;
}
#Override
public void onBindViewHolder(#NonNull PostViewHolder viewHolder, int position) {
viewHolder.setFullname(usersPostsList.get(position).getFullname());
viewHolder.setTime(usersPostsList.get(position).getTime());
viewHolder.setDate(usersPostsList.get(position).getDate());
viewHolder.setDescription(usersPostsList.get(position).getDescription());
viewHolder.setProfileimage(context, usersPostsList.get(position).getProfileimage());
viewHolder.setPostimage(context, usersPostsList.get(position).getPostimage(), usersPostsList.get(position).getId());
viewHolder.setCountry(usersPostsList.get(position).getCountry());
viewHolder.setCity(usersPostsList.get(position).getCity());
if( viewHolder.PostDescription.originalText.length() > 100 ) {
viewHolder.PostDescription.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!((ExpandableTextView)v).read) {
((ExpandableTextView)v).expandText();
((ExpandableTextView)v).read = true;
} else {
((ExpandableTextView)v).truncateText();
((ExpandableTextView)v).read = false;
}
}
});
}
viewHolder.CommentPostButton.setOnClickListener(v -> {
Intent commentsIntent = new Intent(context, CommentActivity.class);
commentsIntent.putExtra("PostKey",usersPostsList.get(currentPosition).getId());
context.startActivity(commentsIntent);
});
viewHolder.LikePostButton.setOnClickListener(v -> {
Amplify.DataStore.query(
Likes.class, Where.matches(Likes.POST_ID.eq(usersPostsList.get(currentPosition).getId().trim())
.and(Likes.SENDER.eq(currentUserId.trim()))
.and((Likes.RECEIVER.eq(usersPostsList.get(currentPosition).getUid())))
),
items -> {
if (items.hasNext()) {
Likes item = items.next();
Amplify.DataStore.delete(item,
deleted -> Log.i("Amplify", "Deleted item."),
failure -> Log.e("Amplify", "Delete failed.", failure)
);
Log.i("Amplify", "Id " + item.getId());
} else {
Likes likes = Likes.builder()
.receiver(usersPostsList.get(currentPosition).getUid().trim())
.sender(currentUserId.trim())
.postId(usersPostsList.get(currentPosition).getId().trim())
.value("true")
.build();
Amplify.DataStore.save(
likes,
success -> Log.i("Amplify", "Item updated: " + success.item().getId()),
error -> Log.e("Amplify", "Could not save item to DataStore", error)
);
}
},
failure -> {
Log.e("Amplify", "Could not query DataStore", failure);
}
);
notifyDataSetChanged();
});
viewHolder.modifyPost.setOnClickListener(v -> {
Intent intent = new Intent(context, ClickPostActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("PostKey", usersPostsList.get(currentPosition).getId());
intent.putExtra("postImagePath", usersPostsList.get(currentPosition).getPostimage());
context.startActivity(intent);
});
}
#Override
public int getItemCount() {
return usersPostsList.size() ;
}
}
Seems that method getItemViewType is called after the click on LikePostButton so that : viewHolder.LikePostButton.setOnClickListener(v -> { gets an older currentPosition variable value
Thanks to everyone helping me to understand
you can override getItemViewType(position:Int) method and replace the line of super to return it with only position like this in Kotlin:
override fun getItemViewType(position: Int): Int {
return position
}
in Java :
#Override
public int getItemViewType(int position) {
return position;
}
Thanks, i solved it setting the listener on the button in the ViewHolder and then just calling getAbsoluteAdapterPosition() to get the correct position

How to customize Adapter for SwipeCard?

I am making a flash card app with SwipeCard
This is my FlashCardActivity:
import android.os.Bundle;
import android.text.style.TtsSpan;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.adapter.CardListAdapter;
import org.koreanlab.fabloading.basickit.BasicCompatActivity;
import org.koreanlab.fabloading.basickit.remote.RemoteService;
import org.koreanlab.fabloading.basickit.remote.ServiceGenerator;
import org.koreanlab.fabloading.item.CardItem;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class FlashCardActivity extends BasicCompatActivity {
private String TAG = getClass().getSimpleName();
private ArrayList<CardItem> cardList;
private ArrayAdapter<String> cardAdapter;
private CardListAdapter cardListAdapter;
private int i;
CardItem newCard;
#BindView(R.id.frame)
SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashcard);
ButterKnife.bind(this);
cardList = new ArrayList<>();
Log.d(TAG, "cardList created");
// get Two Words;
cardList.add(getCardItem());
cardList.add(getCardItem());
Log.d(TAG, "added Card");
cardListAdapter = new CardListAdapter(this, R.layout.card_item, cardList);
Log.d(TAG, "adapter created: " + (cardListAdapter == null ? "cardAdapter is NULL" : "cardAdapter is not NULL"));
flingContainer.setAdapter(cardListAdapter);
Log.d(TAG, "adapter set: " + (flingContainer == null ? "flingContainer is NULL" : "flingContainer is not NULL"));
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
cardList.remove(0);
cardListAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
makeToast(FlashCardActivity.this, "Left!");
}
#Override
public void onRightCardExit(Object dataObject) {
makeToast(FlashCardActivity.this, "Right!");
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
Log.d(TAG, "onAdapterAboutToEmpty: "+itemsInAdapter);
cardList.add(getCardItem());
cardListAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
makeToast(FlashCardActivity.this, "Clicked!");
}
});
}
#OnClick(R.id.right)
public void right() {
/**
* Trigger the right event manually.
*/
flingContainer.getTopCardListener().selectRight();
}
#OnClick(R.id.left)
public void left() {
flingContainer.getTopCardListener().selectLeft();
}
public CardItem getCardItem() {
// No problem here.
return newCard;
}
}
Notice that, getCardItem() just get one card. First of all, it calls two cards and the cardList has two cards when activity has created. And after that I'd like to get just one card after swipe. getCardItem() has no problem. I can see that I receives the data from my Server.
This is my custom CardListAdapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.item.CardItem;
import java.util.ArrayList;
import java.util.List;
public class CardListAdapter extends ArrayAdapter {
private final String TAG = this.getClass().getSimpleName();
private Context context;
private int cardResId;
private int frontResId;
private int backResId;
private List<CardItem> cardList;
private LayoutInflater mInflater;
//this, R.layout.card_item, R.id.card_front, R.id.card_back, cardList
public CardListAdapter(Context context, int cardResId, ArrayList<CardItem> cardList) {
super(context, cardResId);
this.context = context;
this.cardResId = cardResId;
this.cardList = cardList;
mInflater = LayoutInflater.from(context);
}
public void setItem(CardItem newItem) {
Log.d(TAG, "setItem");
for (int i = 0; i < cardList.size(); i++) {
CardItem item = cardList.get(i);
if (item.seq == newItem.seq) {
cardList.set(i, newItem);
break;
}
}
}
#Override
public int getCount() {
Log.d(TAG, "getVIew");
return 0;
}
public int getPosition(CardItem item) {
Log.d(TAG, "getPosition = "+item);
return cardList.indexOf(item);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(cardResId, parent,false);
holder = new ViewHolder();
Log.d(TAG, "getView");
holder.frontTV = convertView.findViewById(R.id.card_front);
holder.backTV = convertView.findViewById(R.id.card_back);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.frontTV.setText((String)getItem(position));
holder.backTV.setText((String)getItem(position));
return convertView;
}
static class ViewHolder
{
TextView frontTV, backTV;
}
}
I checked many blogs and other Q&A, but I haven't figured it out how to solve this problem. That activity keeps going back after receiving 'one word' data from the Server and It doesn't show any error messages.

Checking if a list view adapter contains a string

I wanna check my adapter before adding new item and if there is a duplicate item then ignore it.I tried this method but didn't work for me.
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (radioGroup.getCheckedRadioButtonId()){
case R.id.Tshirt:
radio="Tshirt";
break;
case R.id.Shoes:
radio="Shoes";
break;
default:
radio="Pants";
break;
}
if (!txtname.getText().toString().isEmpty() && !txtdes.getText().toString().isEmpty() )
list.add(new products(txtname.getText().toString(), radio, txtdes.getText().toString(), R.drawable.shop));
myadapter adapter = new myadapter(MainActivity.this, list);
lstCountries.setAdapter(adapter);
}
});
You can simply check whether your list contains a certain String and / or object already.
if(!list.contains(...) {
list.add(..);
}
You can check this thread for more information:
Check if a String is in an ArrayList of Strings
If order is not important , then you could use HashSet instead of a list, a type of SET, these are the collections that do not allow duplicate values but do not preserve insertion order too.
Java HashSet
Example usage:-
HashSet<String> set=new HashSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
Here is my code:
Mainactivity.java
package com.example.esi.shopapp;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lstCountries;
public Button btnadd;
private EditText txtname;
private EditText txtdes;
private RadioGroup radioGroup;
String radio = "";
private List<products> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstCountries = (ListView) findViewById(R.id.lists);
btnadd = findViewById(R.id.button);
txtname =findViewById(R.id.txt_name);
txtdes =findViewById(R.id.txt_desc);
radioGroup = findViewById(R.id.categorize);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (radioGroup.getCheckedRadioButtonId()){
case R.id.Tshirt:
radio="Tshirt";
break;
case R.id.Shoes:
radio="Shoes";
break;
default:
radio="Pants";
break;
}
if (!txtname.getText().toString().isEmpty() && !txtdes.getText().toString().isEmpty() )
if (!list.contains(txtname.getText().toString()))
list.add(new products(txtname.getText().toString(), radio, txtdes.getText().toString(), R.drawable.shop));
myadapter adapter = new myadapter(MainActivity.this, list);
lstCountries.setAdapter(adapter);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.clear_form:
txtname.setText("");
txtdes.setText("");
break;
case R.id.clear_list:
lstCountries.setAdapter(null);
break;
case R.id.setting:
break;
case R.id.exit:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Are you sure to exit?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
}
return super.onOptionsItemSelected(item);
}
}
myadapter.java
package com.example.esi.shopapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class myadapter extends BaseAdapter {
private Context context;
private List<products> list;
public myadapter(Context context, List<products> list) {
this.context = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup root) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_layout, root, false);
}
TextView txtCountryName = convertView.findViewById(R.id.txt_product_name);
TextView txtCountryContinent = convertView.findViewById(R.id.txt_product_desc);
TextView txtype = convertView.findViewById(R.id.txt_product_type);
ImageView imgCountryFlag = convertView.findViewById(R.id.img_country_flag);
txtCountryName.setText(list.get(position).getName());
txtCountryContinent.setText(list.get(position).getDescribe());
txtype.setText(list.get(position).getType());
imgCountryFlag.setImageResource(list.get(position).getFlag());
return convertView;
}
}
products.java
package com.example.esi.shopapp;
public class products {
private String name;
private String describe;
private String type;
private int flag;
public products(String name, String describe, String type, int flag) {
this.name = name;
this.describe = describe;
this.type = type;
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Your logic is wrong. You are creating a new instance of the adapter in each item click. You should move
myadapter adapter = new myadapter(MainActivity.this, list);
lstCountries.setAdapter(adapter);
to the onCreate method. Make adapter a global variable and
change your current code to this.
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.Tshirt:
radio = "Tshirt";
break;
case R.id.Shoes:
radio = "Shoes";
break;
default:
radio = "Pants";
break;
}
if (!txtname.getText().toString().isEmpty() && !txtdes.getText().toString().isEmpty()) {
products product = new products(txtname.getText().toString(), radio, txtdes.getText().toString(), R.drawable.shop)
if (!list.contains(product)) {
list.add(product);
adapter.notifyDataSetChanged();
}
}
}
});

GridView Always Scroll To The Top After Success Load Next Page

I face a difficulty when working using recyclerview and Gridview. The problem is when my application success load next page (next data), the recyclerview always back to the top.
I want the recyclerview start from last index.
Example : First page load 10 items, after success loan next page, the recycler view start scrolling from item 8.
For resolve that problem, i have tried all the solution on stackoverflow still get nothing.
Here are my code :
package com.putuguna.sitehinduapk.adapters;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.putuguna.sitehinduapk.R;
import com.putuguna.sitehinduapk.activities.DetailBlogPostActivity;
import com.putuguna.sitehinduapk.models.listposts.ItemPostModel;
import com.putuguna.sitehinduapk.utils.GlobalFunction;
import com.putuguna.sitehinduapk.utils.GlobalVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class ListPostAdapter extends RecyclerView.Adapter<ListPostAdapter.ViewHolder>{
private List<ItemPostModel> mListPost;
private Context mContext;
public ListPostAdapter(List<ItemPostModel> mListPost, Context mContext) {
this.mListPost = mListPost;
this.mContext = mContext;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.adapter_item_list_post, null);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ItemPostModel post = mListPost.get(position);
List<String> listURL = extractUrls(post.getContentPosting());
Glide.with(mContext)
.load(listURL.get(0))
.into(holder.ivImagePost);
holder.tvTitle.setText(post.getTitle());
holder.llItemPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TO DO OnClick
}
});
List<String> listLabel = post.getListLabel();
String labelPost="";
for(int i=0; i<listLabel.size();i++){
labelPost += "#"+listLabel.get(i) + " ";
}
holder.tvLabel.setText(labelPost);
if ((position >= getItemCount() - 1))
load();
}
public abstract void load();
#Override
public int getItemCount() {
return mListPost.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView ivImagePost;
public TextView tvTitle;
public LinearLayout llItemPost;
public TextView tvLabel;
public ViewHolder(View itemView) {
super(itemView);
ivImagePost = (ImageView) itemView.findViewById(R.id.iv_image_post);
tvTitle = (TextView) itemView.findViewById(R.id.tv_blog_title);
llItemPost = (LinearLayout) itemView.findViewById(R.id.ll_item_post);
tvLabel = (TextView) itemView.findViewById(R.id.textview_label_adapter);
}
}
public static List<String> extractUrls(String input) {
List<String> result = new ArrayList<String>();
Pattern pattern = Pattern.compile(
"\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
"(\\w+:\\w+#)?(([-\\w]+\\.)+(com|org|net|gov" +
"|mil|biz|info|mobi|name|aero|jobs|museum" +
"|travel|[a-z]{2}))(:[\\d]{1,5})?" +
"(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
}
This code of MainActivity.java
private void getListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getListPost(GlobalVariable.APP_KEY_V3);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
/**
* this method used for post (next page)
*/
private void getNextListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getNexPageListPost(GlobalVariable.APP_KEY_V3,nextPageToken);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView2(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onRefresh() {
mListPost.clear();
getListPost();
}
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
getNextListPost();
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
/**
* this method used for set view (next page)
* #param listpost
*/
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
List<ItemPostModel> itemNextPost = listpost.getListItemsPost();
// itemNextPost.addAll(mListPost);
mListPost.addAll(itemNextPost);
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
an
}
Any suggestion will be appreciated.
The problem with the code is, your resetting the complete data again into your adapter and setting it to RecyclerView again, That's why it is re instanced everything in RecyclerView and i.e scrolling to top. Instead of that you can try something like just add /append the updated data into the list ( where you holds the data ) and then just call the adapter.notifyDataSetChanged(); method. automatically it will add the updated data and you no need to set the recycler view again.
Probably for your case you need to change your initDataView2() mehtod like below
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());\
mPostAdapter .notifyDataSetChanged();
}
For nextPageToken thing you can move into your main code onCreate() or the initDataView() method where you already have the adapter initialization
like this,
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
I hope it will work :)

Categories

Resources