I do some request from a WordPress page API to get some content and for every content, I get a picture. And I display them on RecyclerView. But they do not show without doing first a scroll of RecyclerView then they start to show one by one.
Here is the NewsActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
back_button = findViewById(R.id.toolbar_back_button);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
toolbarTxt = findViewById(R.id.tolbar_text_view);
toolbarTxt.setText("News");
recyclerView = findViewById(R.id.news_recyclerview);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(true);
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
merrLajmet();
} else {
Toast.makeText(this, "You do not have internet connection", Toast.LENGTH_SHORT).show();
}
}
Here's how I get the contents.
public void merrLajmet(){
Uri baseUri = Uri.parse(NEWS_REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
Request.Method.GET, uriBuilder.toString(), null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
listLajmet = Query.shfaqLajmet(response);
for (Lajmi lajmi : listLajmet) {
merrFoton(lajmi);
}
mAdapter.setLajmi(listLajmet);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Error
}
});
if (listLajmet.isEmpty()) {
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
}
And the get the photo of from specific content.
public void merrFoton(final Lajmi lajmi) {
Uri baseUri = Uri.parse(IMAGE_REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendPath(String.valueOf(lajmi.getFeatureMedia()));
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, uriBuilder.toString(), null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
String imageUrl = Query.shfaqFoton(response);
if (imageUrl == ""){
imageUrl = String.valueOf(R.drawable.news_photo1);
}
lajmi.setImage(imageUrl);
//mAdapter.setLajmi(listLajmet);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(NewsActivity.this, "Nuk ka te image " +
//error.networkResponse.toString(), Toast.LENGTH_SHORT).show();
}
});
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
And finally, here's my adapter class.
public class LajmiAdapter extends RecyclerView.Adapter<LajmiAdapter.MyViewHolder>{
private List<Lajmi> mLajmiList = new ArrayList<>();
private Context ctx;
public LajmiAdapter(Context ctx) {
this.ctx = ctx;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView mTitle, mCategory;
ImageView mImage,mColor;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTitle = itemView.findViewById(R.id.news_text_view_titulli);
mCategory = itemView.findViewById(R.id.news_text_view_kategoria);
mImage = itemView.findViewById(R.id.news_image_main);
mColor = itemView.findViewById(R.id.news_image_small);
}
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Lajmi lajmi = mLajmiList.get(position);
Intent intent = new Intent(ctx, SinglenewsActivity.class);
intent.putExtra("title", lajmi.getTitle());
intent.putExtra("category", lajmi.getCategory());
intent.putExtra("image", lajmi.getImage());
intent.putExtra("color",lajmi.getColor());
intent.putExtra("description",lajmi.getDescription());
ctx.startActivity(intent);
}
}
#Override
public LajmiAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
return new LajmiAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(LajmiAdapter.MyViewHolder holder, int position) {
Lajmi lajmi = mLajmiList.get(position);
holder.mTitle.setText(lajmi.getTitle());
Picasso.get()
.load(lajmi.getImage())
.resize(400, 300)
.onlyScaleDown()
.into(holder.mImage);
}
public void setLajmi(List<Lajmi> lajmiList) {
mLajmiList = lajmiList;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
if(mLajmiList == null)
return 0;
else
return mLajmiList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
When I open NewsActivity, I expect all the images to show on the item of RecyclerView without doing a scroll first.
When you are scrolling the list, it gets the updated items and hence gets the updated list of images which you fetched from your server. I think you are missing a notifyDataSetChanged call here.
#Override
public void onResponse(JSONObject response) {
String imageUrl = Query.shfaqFoton(response);
if (imageUrl == ""){
imageUrl = String.valueOf(R.drawable.news_photo1);
}
lajmi.setImage(imageUrl);
// Add the notifyDataSetChanged here
mAdapter.notifyDataSetChanged();
}
However, in this case, as you are fetching your images one by one, each time you are fetching an image, the adapter will be notified and hence the RecyclerView will be reloaded accordingly. Its kind of annoying in the case when you are scrolling through items and the list gets to the top each time a new image is fetched from the server as you are calling notifyDataSetChanged() each time.
I would like to suggest implementing the whole image fetching a bit differently. Get the count of the images to be downloaded first. Then get all the images URLs and save them in your list. Then call notifyDataSetChanged once you are finished downloading all the image URLs.
Hope that helps!
notifyDataSetChanged(): Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.(https://developer.android.com/reference/android/widget/BaseAdapter)
If you are fetching images one by one, refreshing the view might not be the best solution. If you don't want it to scroll to top of the page whenever a new image downloaded, you can use notifyItemRangeInserted(int positionStart, int itemCount)
Also please have a look at this question
Related
I have an issue where notifyDataSetChanged() in a response call will blank out the recyclerview but if the Adapter is initiated manually with a onClick, the recyclerview works. I have tested that the List has the items inside before calling notifyDataSetChanged() so i'm not sure what's wrong here.
[Main Activity] This works but i have to manually click the bnQuery.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = API_client.getClient().create(APIInterface.class);
etCoin = (EditText) findViewById(R.id.etCoin);
bnQuery = (Button) findViewById(R.id.bnQuery);
rcvMain = findViewById(R.id.rcvMain);
getCoinData("2");
//initRCV_Main();
bnQuery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//getCoinData("2");
initRCV_Main();
}
});
}
private void initRCV_Main() {
rcvMainAdp = new rcvMainAdapter(cList);
rcvMain.setAdapter(rcvMainAdp);
rcvMain.setLayoutManager(new LinearLayoutManager(this));
}
private void getCoinData(String coinLimit){
Call<cInfoPack> call = apiInterface.doGetCoinData(coinLimit);
call.enqueue(new Callback<cInfoPack>() {
#Override
public void onResponse(Call<cInfoPack> call, Response<cInfoPack> response) {
cInfoPack list = response.body();
List<cData> listSorter = new ArrayList<>();
listSorter.addAll(list.getData());
Collections.sort(listSorter, new SortbyVolChg());
cList.clear();
cList = listSorter;
System.out.println("list " + list.getData().get(0).getQuote());
System.out.println("listSorter " + listSorter.get(0).getQuote());
System.out.println("cList " + cList.get(0).getQuote());
//rcvMainAdp.notifyDataSetChanged();
}
#Override
public void onFailure(Call<cInfoPack> call, Throwable t) {
Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_SHORT).show();
Log.d("XXXX", t.getLocalizedMessage());
call.cancel();
}
});
}
[Main Activity] If i initiate the recyclerview during onCreate and use the notifyDataSetChanged() during getCoinData, I get a blank recycleview. system.out shows that the lists all contain information in them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = API_client.getClient().create(APIInterface.class);
etCoin = (EditText) findViewById(R.id.etCoin);
bnQuery = (Button) findViewById(R.id.bnQuery);
rcvMain = findViewById(R.id.rcvMain);
getCoinData("2");
initRCV_Main();
bnQuery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//getCoinData("2");
//initRCV_Main();
}
});
}
private void initRCV_Main() {
rcvMainAdp = new rcvMainAdapter(cList);
rcvMain.setAdapter(rcvMainAdp);
rcvMain.setLayoutManager(new LinearLayoutManager(this));
}
private void getCoinData(String coinLimit){
Call<cInfoPack> call = apiInterface.doGetCoinData(coinLimit);
call.enqueue(new Callback<cInfoPack>() {
#Override
public void onResponse(Call<cInfoPack> call, Response<cInfoPack> response) {
cInfoPack list = response.body();
List<cData> listSorter = new ArrayList<>();
listSorter.addAll(list.getData());
Collections.sort(listSorter, new SortbyVolChg());
cList.clear();
cList = listSorter;
System.out.println("list " + list.getData().get(0).getQuote());
System.out.println("listSorter " + listSorter.get(0).getQuote());
System.out.println("cList " + cList.get(0).getQuote());
rcvMainAdp.notifyDataSetChanged();
}
#Override
public void onFailure(Call<cInfoPack> call, Throwable t) {
Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_SHORT).show();
Log.d("XXXX", t.getLocalizedMessage());
call.cancel();
}
});
}
[Adapter]
public class rcvMainAdapter extends RecyclerView.Adapter<rcvMainAdapter.ViewHolder> {
private List<cData> idxCoin;
//ItemClickListener itemClickListener;
rcvMainAdapter(List<cData> data) {this.idxCoin = data;}
#NonNull
#NotNull
#Override
public ViewHolder onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.rcv_main,parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull #NotNull ViewHolder holder, int position) {
cData cdata = idxCoin.get(position);
TextView tvSym = holder.tvSymbol;
tvSym.setText(cdata.getSymbol());
TextView tvQuo = holder.tvQuote;
BigDecimal tvQuote_BD = new BigDecimal(cdata.getQuote().getuSD().getPrice().toString());
tvQuote_BD.setScale(6, RoundingMode.DOWN);
tvQuo.setText(tvQuote_BD.toString());
TextView tvV24 = holder.tvVolume24;
BigDecimal tvVolume24_BD = new BigDecimal(cdata.getQuote().getuSD().getVolume24h().toString());
BigInteger tvVolume24_BI = tvVolume24_BD.toBigInteger();
tvV24.setText(tvVolume24_BI.toString());
}
#Override
public int getItemCount() {
return idxCoin.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvSymbol, tvQuote, tvVolume24;
public ViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
tvSymbol = itemView.findViewById(R.id.tvSymbol);
tvQuote = itemView.findViewById(R.id.tvQuote);
tvVolume24 = itemView.findViewById(R.id.tvVolume24);
//itemView.setOnClickListener(this);
}
}
/*
public interface ItemClickListener{
void onItemClick(View view, int position);
}
*/
}
PS: apologies for the rubbish coding as this is self taught and modifying some codes found online.
Remove this in response.
cList.clear();
Add This line in response
rcvMainAdp.setdata(listSorter);
In rcvMainAdp Adapter, Create a Method setdata()
public void setdata(ArrayList<cData> data) {
this.idxCoin = data;
notifyDataSetChanged();
}
Problem most likely is that when you call initRCV_Main() You set the adapter to the list as in rcvMainAdp = new rcvMainAdapter(cList); And when list is changed and you set it to adapter it functions.
But when you call getCoinData() and rcvMainAdp.notifyDataSetChanged(); at the end you never set the changed list to the adapter until you click initRCV_Main() again.
So maybe the fix is calling rcvMainAdp = new rcvMainAdapter(cList) and then
rcvMainAdp.notifyDataSetChanged();
I am using retrofit to search the images and displaying. I am using SearchView to search and recyclerview and adapter to display the item.
It's working for the very first time. Second time when I search, it's displaying the same item. It's not clearing the view to display.
Here is my code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
ImagesViewModel newsViewModel;
List<Hits> newsArticles;
List<Hits> articleArrayList;
ImagesAdapter newsAdapter;
RecyclerView rvHeadline;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvHeadline = (RecyclerView) findViewById(R.id.rvNews);
searchView=(SearchView) findViewById(R.id.searchView);
searchView.setQueryHint("Search Images");
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
callPixabayImages(query);
Toast.makeText(getBaseContext(), query, Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
callPixabayImages(newText);
Toast.makeText(getBaseContext(), newText, Toast.LENGTH_LONG).show();
return false;
}
});
articleArrayList = new ArrayList<>();
}
private void callPixabayImages(String query){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ImageApiInterface.PIXABAY_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
ImageApiInterface api = retrofit.create(ImageApiInterface.class);
Call<MyImages> call = api.getMovieDetails(query);
call.enqueue(new Callback<MyImages>() {
#Override
public void onResponse(Call<MyImages> call, Response<MyImages> response) {
Log.e("response","response"+response.code());
if(response.isSuccessful()){
Log.e("response","response"+response.code());
if((response.body() != null ? response.body().getHits() : null) !=null){
List<Hits> imagesList = response.body().getHits();
articleArrayList.addAll(imagesList);
newsAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onFailure(Call<MyImages> call, Throwable t) {
System.out.println("Error: "+ t.getMessage());
Log.e("response","response"+t);
}
});
}
private void setupRecyclerView() {
if (newsAdapter == null) {
newsAdapter = new ImagesAdapter(MainActivity.this, articleArrayList);
rvHeadline.setLayoutManager(new LinearLayoutManager(this));
rvHeadline.setAdapter(newsAdapter);
rvHeadline.setItemAnimator(new DefaultItemAnimator());
rvHeadline.setNestedScrollingEnabled(true);
} else {
newsAdapter.notifyDataSetChanged();
}
}
Here where I need to reset to search the new item inorder to avoid to display the previous item. List is not clearing to display the new items.
Here is Adapter class:
public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.NewsViewHolder> {
private Context context;
private List<Hits> articles;
public ImagesAdapter(Context context, List<Hits> articles) {
this.context = context;
this.articles = articles;
}
#NonNull
#Override
public ImagesAdapter.NewsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.images_item, parent, false);
return new NewsViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ImagesAdapter.NewsViewHolder holder, final int position) {
String image = articles.get(position).getLargeImageURL();
Log.e("imagestest",image);
if (image != null) {
Glide.with(context) //passing context
.load(image) //passing your url to load image.
.dontAnimate()
.placeholder(context.getDrawable(R.drawable.image_not_available)) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
//.centerCrop()//this method help to fit image into center of your ImageView
.into(holder.ivNews); //pass imageView reference to appear the image.*/
}
}
#Override
public int getItemCount() {
return articles.size();
}
class NewsViewHolder extends RecyclerView.ViewHolder {
ImageView ivNews;
private NewsViewHolder(#NonNull View itemView) {
super(itemView);
ivNews = itemView.findViewById(R.id.ivNews);
}
}
}
I am clueless where exactly I need to clear the view to fetch the newly searched item.
Trial 1: I tried to clear the list object articleArrayList but it throws crash that adapter size is null.
Clean the
articleArrayList
before loading the searched items again.... by executing this line articleArrayList.clear();
public void onResponse(Call<MyImages> call, Response<MyImages> response) {
Log.e("response","response"+response.code());
if(response.isSuccessful()){
Log.e("response","response"+response.code());
if((response.body() != null ? response.body().getHits() : null) !=null){
List<Hits> imagesList = response.body().getHits();
articleArrayList.clear();
articleArrayList.addAll(imagesList);
newsAdapter.notifyDataSetChanged();
}
}
You're adding generated imagesList to the articleArrayList, you should be replacing the values.
articleArrayList = imagesList;
Hello everyone I am doing a news app for university and I have kind of problem with displaying saved data to another activity. Im using retrofit to get data from internet and Sugar ORM to save data localy. So, news are displaying great and I created a CardView where it has a button "Read Later". The method to save data into database s working (at least on log it shows that it is working) but I cant get saved data to read_later activity. Alos I have an Adapter which contains onBindViewHolder.
#Override
public void onBindViewHolder(#NonNull MainArticalAdapter.ViewHolder viewHolder, int position) {
final Article articleModel = articleArrayList.get(position);
if(!TextUtils.isEmpty(articleModel.getTitle())){
viewHolder.titleText.setText(articleModel.getTitle());
}
if(!TextUtils.isEmpty(articleModel.getDescription())) {
viewHolder.descriptionText.setText(articleModel.getDescription());
}
if(!TextUtils.isEmpty(articleModel.getUrlToImage())){
Picasso.get().load(articleModel.getUrlToImage())
.resize(700,500)
.centerInside()
.into(viewHolder.imgView);
}
viewHolder.artilceAdapterParentLinear.setTag(articleModel);
Button btn = viewHolder.btn_read_later;
btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
Article art = new Article();
art.setAuthor("Harun Shaban");
art.save();
}
}
);
}
and viewholder which extends RecyclerView.ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView titleText, descriptionText;
private LinearLayout artilceAdapterParentLinear;
private ImageView imgView;
private Button btn_read_later;
public ViewHolder(#NonNull View view) {
super(view);
btn_read_later = view.findViewById(R.id.button_read_later);
imgView = view.findViewById(R.id.article_adapter_image_view);
titleText = view.findViewById(R.id.article_adapter_tv_title);
descriptionText = view.findViewById(R.id.article_adapter_tv_description);
artilceAdapterParentLinear = view.findViewById(R.id.article_adapter_ll_parent);
artilceAdapterParentLinear.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
if(onRecyclerViewItemClickListener != null){
onRecyclerViewItemClickListener.onItemClick(getAdapterPosition(),view);
}
}
}
);
}
}
and this is in my MainActivity class which contains a method to display all data received from internet by retrofit
private void showData(){
// Second step to create the recycler view to show the data taken
final RecyclerView mainRecycler = findViewById(R.id.activity_main_tv);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mainRecycler.setLayoutManager(linearLayoutManager);
// First step to create the response
final APIInterface apiService = ApiClient.getClient().create(APIInterface.class);
Call<ResponeModel> call = apiService.getLatestNews("techcrunch", API_KEY);
//third step
call.enqueue(new Callback<ResponeModel>() {
#Override
public void onResponse(Call<ResponeModel> call, Response<ResponeModel> response) {
if(response.body().getStatus().equals("ok")){
List<Article> articleList = response.body().getArticles();
if(articleList.size()>0){
final MainArticalAdapter mainArticalAdapter = new MainArticalAdapter(articleList);
mainArticalAdapter.setOnRecyclerViewItemClickListener(MainActivity.this);
mainRecycler.setAdapter(mainArticalAdapter);
}
}
}
#Override
public void onFailure(Call<ResponeModel> call, Throwable t) {
Log.e("on Fail", t.toString());
}
});
}
I tried to work with same logic in Read_later_Activity but it crashes.
This is my Read_later_activity…
public void getDatafromDB() {
List <Article> savedArticles = Article.listAll(Article.class);
final RecyclerView read_later_recView = findViewById(R.id.activity_read_later_tv);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
read_later_recView.setLayoutManager(linearLayoutManager);
if(savedArticles.size()>0){
final MainArticalAdapter mainArticalAdapter = new MainArticalAdapter(savedArticles);
mainArticalAdapter.setOnRecyclerViewItemClickListener((OnRecyclerViewItemClickListener) ReadLaterActivity.this);
read_later_recView.setAdapter(mainArticalAdapter);
//to retrieve the data from DB, by id (crashes)
//Article art = Article.findById(Article.class, 0);
//titleText_readLater.setText(art.getTitle());
/*Don`t know what to do*/
}
}
I apologize upfront if the question is too long. Here we go:
There are two activities: Main and Detail Activity.
Main Activity is basically a GridView.
Detail Activity is basically shows the clicked item's detail information. I am passing selected item's id (pid) from the Main to the Detail Activity.
I am facing an issue as follows. Initially, I have 3G connection (cellular connection) and clicked on the first item and see the corresponding item detail in the Detail Activity, it works perfectly fine, and go back to the Main Activity, then clicked on the second item, then unfortunately it still shows me the first item in the DetailActivity that I clicked initially.
I switched from 3g to wifi while app is on the active and open. No matter what I click, it still shows me the first item that I clicked initially.
But when I delete the app and reinstall it and get either wifi access only, the app works perfectly fine.
In the following implementation, Connection URL (PRODUCT_DETAIL_URL) is http, not https. I am using Volley library for the network connection.
DetailActivity.java
private void productDetailInit() {
// it is http
StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
jsonObject = response;
loadJsonData();
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("id", productID);
return params;
}
};
RetryPolicy policy = new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);
CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
}
CustomVolleyRequest.java
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
private class BitmapCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
mCache = new LruCache<>(20);
}
#Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
// scaling bitmap for avoiding too much big images
Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
mCache.put(url, scaled);
}
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
Adapter.java
class ProductMainAdapter extends ArrayAdapter<ImageRecord> {
private ImageLoader mImageLoader;
private String jsonObject;
ProductMainAdapter(Context context) {
super(context, R.layout.grid_item);
mImageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
}
#NonNull
#Override
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
convertView.setBackgroundResource(R.drawable.round_gridview);
holder.priceTagImage = (ImageView) convertView.findViewById(R.id.priceTag_IV);
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
holder.priceTagRL = (RelativeLayout) convertView.findViewById(R.id.priceTag_RL);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
ImageRecord imageRecord = getItem(position);
holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openProductDetail(position);
}
});
holder.textView.setText(imageRecord != null ? imageRecord.getTitle() : null);
holder.priceTagRL.setRotation(0);
return convertView;
}
private class ViewHolder{
TextView textView;
ImageView priceTagImage;
NetworkImageView imageView;
RelativeLayout priceTagRL;
}
private void openProductDetail(int position) {
try {
ImageRecord imr = getItem(position);
String productID = imr != null ? imr.getId() : "0";
Intent intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("pid", productID);
getContext().startActivity(intent);
} catch (Exception e) {
Log.e("openProductDetail", "exception", e);
}
}
I wonder what I am missing/ doing wrong in the provided implementation. It has been taking almost 2-3 months, I could not able to handle that issue. Has anyone ever faced a similar situation? Any suggestion or comment is highly appreciated.
You could just kill the activities with finish(); when the other one loads.
I was also having the same problem. In my case in onClick method the position was not correct.
Then I used to set the position as a tag to the specific view which has OnClickListener set and my problem solved.
It may be due to the position is declared as final. So remove the final keyword and try like this.
holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
holder.imageView.setTag(position);
holder.priceTagRL.setRotation(0);
And in onClick method
openProductDetail((int)view.getTag());
Check if the below link can be helpful:
https://stackoverflow.com/a/33682366/2798289 (3G connection might actually be slow that you to have increase the threshold of timeout)
Also, there is a chance the position reference in the adapter can come wrong in some edge case (irrespective of your actual issue). You can try the below alternative method..
Use gridView.setOnItemClickListener
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//do your logic based on the position
}
});
Note: In case if GridView is not able to observe the item click event, you have to set android:descendantFocusability="blocksDescendants" to the child root view under gridview. This might happen basically because the GridView items should not be clickable by itself as it will observe the click event.
Reference: https://developer.android.com/guide/topics/ui/layout/gridview.html
I have a list view containing few fields coming from back end. One feed is 'number of likes'.
When I click on any list row it opens one activity for that row, there like button in that activity. When user presses like it get appended on server.
Now the problem is it should show incremented value in the list view when user go back to list view activity. How to do that?
NOTE: Like counter is incremented if I close the app and start it again.
I tried to call on Create method again from on Resume method but it produces duplicate copy of rows every time list view activity is remusmed.
Here is my list activity code.
public class MainActivity extends Activity {
// Session Manager Class
SessionManager session;
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "MY_URL";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
{
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setThumbnailUrl(obj.getString("image"));
movie.setTitle(obj.getString("title"));
movie.setDate(obj.getString("date"));
movie.setVideo(obj.getString("video"));
movie.setLikes(obj.getInt("likes"));
movie.setId(obj.getInt("id"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating unique ID
final String deviceId = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
/**
* Call this function whenever you want to check user login
* This will redirect user to LoginActivity is he is not
* logged in
* */
session.checkLogin();
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getting values from selected ListItem
// ImageView thumbNail = (ImageView)view.findViewById(R.id.thumbnail);
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String likes = ((TextView)view.findViewById(R.id.likes)).getText().toString();
String date = ((TextView)view.findViewById(R.id.date)).getText().toString();
String video = ((TextView) view.findViewById(R.id.video)).getText().toString();
String idd = ((TextView) view.findViewById(R.id.idd)).getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(), MovieDetailActivity.class);
// in.putExtra("THUMB", thumbNail.toString());
in.putExtra("TITLE", title);
in.putExtra("LIKES", likes);
in.putExtra("DATE", date);
in.putExtra("VIDEO", video);
in.putExtra("IDD", idd);
in.putExtra("UNIQUEID",deviceId);
//in.putExtra(TAG_URL,"url");
// in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
}
);
// Creating volley request obj
enter code here
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setThumbnailUrl(obj.getString("image"));
movie.setTitle(obj.getString("title"));
movie.setDate(obj.getString("date"));
movie.setVideo(obj.getString("video"));
movie.setLikes(obj.getInt("likes"));
movie.setId(obj.getInt("id"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView likes = (TextView) convertView.findViewById(R.id.likes);
TextView date = (TextView) convertView.findViewById(R.id.date);
TextView video = (TextView) convertView.findViewById(R.id.video);
TextView id = (TextView) convertView.findViewById(R.id.idd);
//TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
date.setText(m.getDate());
video.setText(m.getVideo());
likes.setText(String.valueOf(m.getLikes()));
id.setText(String.valueOf(m.getId()));
return convertView;
// Listview on item click listener
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Requested implementation using a SortedList and RecyclerView:
Here is the example I used to build mine.
This is my slightly more complex code that includes sorting via a SearchView in the toolbar. You can use the example in the above link if you want the example without the sorting. The control logic is in my Presenter class that manages this adapter and the Fragment:
public class AdapterInstitutionList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// lists to control all items and items visible after sorting
private SortedList<MInstitutionInfo> visibleList;
private ArrayList<MInstitutionInfo> allItems;
// my fragment and the presenter
private FInstitutionSelection fInstitutionSelection;
private PInstitutionList presenter;
public AdapterInstitutionList(PInstitutionList pInstitutionSelection, FInstitutionSelection fInstitutionSelection) {
// get ref to fragment, presenter, and create new callback for sortedlist
this.fInstitutionSelection = fInstitutionSelection;
presenter = pInstitutionSelection;
visibleList = new SortedList<>(MInstitutionInfo.class, new InstitutionListCallback());
allItems = new ArrayList<>();
}
// inflate your list item view here
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem_institution, parent, false);
return new InstitutionViewHolder(view);
}
// on binding, you populate your list item with the values, onclickhandle, etc
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
InstitutionViewHolder institutionViewHolder = (InstitutionViewHolder) viewHolder;
final MInstitutionInfo institutionInfo = visibleList.get(position);
institutionViewHolder.setInstitutionInfo(institutionInfo);
institutionViewHolder.populateTextView();
institutionViewHolder.parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
presenter.onInstitutionSelected(institutionInfo);
}
});
}
#Override
public int getItemCount() {
return visibleList.size();
}
// my utility function for the presenter/fragment
public MInstitutionInfo get(int position) {
return visibleList.get(position);
}
public int add(MInstitutionInfo item) {
return visibleList.add(item);
}
public int indexOf(MInstitutionInfo item) {
return visibleList.indexOf(item);
}
public void updateItemAt(int index, MInstitutionInfo item) {
visibleList.updateItemAt(index, item);
}
public void addAll(List<MInstitutionInfo> items) {
visibleList.beginBatchedUpdates();
try {
for (MInstitutionInfo item : items) {
visibleList.add(item);
allItems.add(item);
}
} finally {
visibleList.endBatchedUpdates();
}
}
public void addAll(MInstitutionInfo[] items) {
addAll(Arrays.asList(items));
}
public boolean remove(MInstitutionInfo item) {
return visibleList.remove(item);
}
public MInstitutionInfo removeItemAt(int index) {
return visibleList.removeItemAt(index);
}
public void clearVisibleList() {
visibleList.beginBatchedUpdates();
try {
// remove items at end to remove unnecessary array shifting
while (visibleList.size() > 0) {
visibleList.removeItemAt(visibleList.size() - 1);
}
} finally {
visibleList.endBatchedUpdates();
}
}
public void clearAllItemsList() {
allItems.clear();
}
public void filterList(String queryText) {
clearVisibleList();
visibleList.beginBatchedUpdates();
try {
String constraint = queryText.toLowerCase();
for (MInstitutionInfo institutionInfo : allItems) {
if (institutionInfo.getName() != null && institutionInfo.getName().toLowerCase().contains(constraint)) {
visibleList.add(institutionInfo);
}
}
} finally {
visibleList.endBatchedUpdates();
}
}
public void clearFilter() {
if (visibleList.size() == allItems.size()) {
return;
}
clearVisibleList();
visibleList.beginBatchedUpdates();
try {
for (MInstitutionInfo institutionInfo : allItems) {
visibleList.add(institutionInfo);
}
} finally {
visibleList.endBatchedUpdates();
}
}
// the callback for the SortedList
// this manages the way in which items are added/removed/changed/etc
// mine is pretty simple
private class InstitutionListCallback extends SortedList.Callback<MInstitutionInfo> {
#Override
public int compare(MInstitutionInfo o1, MInstitutionInfo o2) {
return o1.getName().compareTo(o2.getName());
}
#Override
public void onInserted(int position, int count) {
notifyItemRangeInserted(position, count);
}
#Override
public void onRemoved(int position, int count) {
notifyItemRangeRemoved(position, count);
}
#Override
public void onMoved(int fromPosition, int toPosition) {
notifyItemMoved(fromPosition, toPosition);
}
#Override
public void onChanged(int position, int count) {
notifyItemRangeChanged(position, count);
}
#Override
public boolean areContentsTheSame(MInstitutionInfo oldItem, MInstitutionInfo newItem) {
return oldItem.getName().equals(newItem.getName());
}
#Override
public boolean areItemsTheSame(MInstitutionInfo item1, MInstitutionInfo item2) {
return item1.getName().equals(item2.getName());
}
}
// this is the view holder that is used for the list items
private class InstitutionViewHolder extends RecyclerView.ViewHolder {
public View parent;
public TextView tvName;
public MInstitutionInfo institutionInfo;
public InstitutionViewHolder(View itemView) {
super(itemView);
parent = itemView;
tvName = (TextView) itemView.findViewById(R.id.tv_institution_listitem_name);
}
public MInstitutionInfo getInstitutionInfo() {
return institutionInfo;
}
public void setInstitutionInfo(MInstitutionInfo institutionInfo) {
this.institutionInfo = institutionInfo;
}
public void populateTextView() {
if (tvName != null && institutionInfo != null && institutionInfo.getName() != null) {
tvName.setText(institutionInfo.getName());
}
}
}
You simply instantiate this adapter and assign it to your RecyclerView
myRecyclerView.setAdapter(myAdapter);
When you call any of the batched updates, the list will automatically update itself in the UI. So when you get your initial data, just call addAll(yourData) and the RecyclerView will auto populate the list. When you get updated data, you just call addAll(yourNewData) and the RecyclerView will automatically add new items and remove the new non-existent items leaving you with a fully updated list. You will need to be sure to implement the SorteList.Callback methods compare(...), areContentsTheSame(...), areItemsTheSame(...) properly to ensure that this behaves as you want it to when adding/removing items.
Please let me know if you need any more help. Frankly, this implementation type for updated and sorted data is extremely smooth.