package com.example.rvj;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity implements CategoryRVAdapter.CategorClickInterface{
private RecyclerView newsRV,categoryRV;
private ProgressBar loadingPB;
private ArrayList<Articles> articlesArrayList;
private ArrayList<CategoryRVModal> categoryRVModalArrayList;
private CategoryRVAdapter categoryRVAdapter;
private NewsRVAdapter newsRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newsRV =findViewById(R.id.idRVNews);
categoryRV =findViewById(R.id.idRVCategories);
loadingPB =findViewById(R.id.idPBLoading);
articlesArrayList =new ArrayList<>();
categoryRVModalArrayList =new ArrayList<>();
newsRVAdapter =new NewsRVAdapter(articlesArrayList,this);
categoryRVAdapter =new CategoryRVAdapter(categoryRVModalArrayList,this,this::onCategoryClick);
newsRV.setLayoutManager(new LinearLayoutManager(this));
newsRV.setAdapter(newsRVAdapter);
categoryRV.setAdapter(categoryRVAdapter);
getCategories();
getNews("All");
newsRVAdapter.notifyDataSetChanged();
}
private void getCategories(){
categoryRVModalArrayList.add(new CategoryRVModal("All","https://images.unsplash.com/photo-1477959858617-67f85cf4f1df?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1244&q=80"));
categoryRVModalArrayList.add(new CategoryRVModal("Technology","https://images.unsplash.com/photo-1518770660439-4636190af475?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHRlY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60"));
categoryRVModalArrayList.add(new CategoryRVModal("Science","https://images.unsplash.com/photo-1611784601826-d17011218c7b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8cGh5c2ljc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=500&q=60"));
categoryRVModalArrayList.add(new CategoryRVModal("Sports","https://images.unsplash.com/photo-1541534741688-6078c6bfb5c5?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8c3BvcnR8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60"));
categoryRVModalArrayList.add(new CategoryRVModal("Entertainment","https://images.unsplash.com/photo-1522869635100-9f4c5e86aa37?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80"));
categoryRVModalArrayList.add(new CategoryRVModal("Business","https://images.unsplash.com/photo-1590283603385-17ffb3a7f29f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxjb2xsZWN0aW9uLXBhZ2V8MTB8OVlzOE1oYkNnOWt8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"));
categoryRVModalArrayList.add(new CategoryRVModal("Health","https://images.unsplash.com/photo-1524863479829-916d8e77f114?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OHx8eW9nYXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=500&q=60"));
categoryRVModalArrayList.add(new CategoryRVModal("General","https://images.unsplash.com/photo-1509854120-eeab44807cd0?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1219&q=80"));
categoryRVAdapter.notifyDataSetChanged();
}
private void getNews(String category){
loadingPB.setVisibility(View.VISIBLE);
String categoryURL = "https://newsapi.org/v2/top-headlines?country=in&category=" + category + "&apiKey=515c8455797e4eb5b942b094725804eb";
String url = "https://newsapi.org/v2/top-headlines?country=in&excludeDomains=stackoverflow.com&sortBy=publishedAt&language=en&apiKey=515c8455797e4eb5b942b094725804eb";
String BASE_URL ="https://newsapi.org/";
Retrofit retrofit =new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
RetrofitAPI retrofitAPI =retrofit.create(RetrofitAPI.class);
Call<NewsModal> call;
if(category.equals("All")){
call = retrofitAPI.getAllNews(url);
}
else{
call = retrofitAPI.getNewsByCategory(categoryURL);
}
call.enqueue(new Callback<NewsModal>() {
@Override
public void onResponse(Call<NewsModal> call, Response<NewsModal> response) {
NewsModal newsModal = response.body();
loadingPB.setVisibility(View.GONE);
ArrayList<Articles> articles= newsModal.getArticles();
for(int i=0;i<articles.size();i++){
articlesArrayList.add(new Articles(articles.get(i).getTitle(), articles.get(i).getDescription()
, articles.get(i).getUrlToImage(), articles.get(i).getUrl(), articles.get(i).getContent()));
}
newsRVAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<NewsModal> call, Throwable t) {
Toast.makeText(MainActivity.this,"Fail to get news Try Agian",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCategoryClick(int position) {
String category = categoryRVModalArrayList.get(position).getCategory();
getNews(category);
}
}
What I have tried:
This is code of MainActivity.java and i think something is wrong here ,i wants that when i click on my category RecyclerView i should get news of that category. , that is if i click on science i should get science related news.