we need to ask for permission to use internet assets . (we do so by writing permission code inside manifest app )
TO create a loading icon . Use <ProgressBar "CONTENT**"/>
Article Class me apan ne API me articles array ke saare content store karaye hai .(url,urtToImage,author,title,description,publishedAt)
Methods used in Articles class .->
constructor of class variables .
getter and setter .
NewsModal Class ke andar apan ne status and totalResults var. store kara rhe hai .
Certainly! Here's a brief summary of the topics we discussed:
1. ViewHolder in RecyclerView
Purpose:
View Caching: To cache the references to the views of an individual item's layout in a
RecyclerView
.Optimized Access to Views: To efficiently access and manipulate the views of each item in the
RecyclerView
.
Usage:
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView categoryName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryName);
}
}
2. Usage of holder
in RecyclerView Adapter
Steps:
ViewHolder Initialization:
- In
onCreateViewHolder
, theholder
(an instance ofViewHolder
) is created and associated with the inflated item layout.
- In
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
return new ViewHolder(view);
}
Bind Data to ViewHolder:
- In
onBindViewHolder
, you useholder
to access and populate the views of the item with the appropriate data for the item at the given position.
- In
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String category = categories.get(position);
holder.categoryName.setText(category);
}
Example:
public class CategoryRVAdapter extends RecyclerView.Adapter<CategoryRVAdapter.ViewHolder> {
private List<String> categories;
public CategoryRVAdapter(List<String> categories) {
this.categories = categories;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String category = categories.get(position);
holder.categoryName.setText(category);
}
@Override
public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView categoryName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryName);
}
}
}
3. notifyDataSetChanged()
in Android
Purpose:
- To notify the attached observers (e.g.,
RecyclerView
orListView
) that the underlying data set has changed and any view reflecting the data set should refresh itself.
Usage:
List<String> newCategories = new ArrayList<>();
newCategories.add("New Category 1");
newCategories.add("New Category 2");
newCategories.add("New Category 3");
categories.addAll(newCategories); // Modify the data set
adapter.notifyDataSetChanged(); // Notify the adapter that the data set has changed
Example with RecyclerView:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CategoryRVAdapter adapter;
private List<String> categories = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize RecyclerView
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Initialize adapter
categories.add("Category 1");
categories.add("Category 2");
categories.add("Category 3");
adapter = new CategoryRVAdapter(categories);
recyclerView.setAdapter(adapter);
// Button to add new categories
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewCategories();
}
});
}
private void addNewCategories() {
List<String> newCategories = new ArrayList<>();
newCategories.add("New Category 1");
newCategories.add("New Category 2");
newCategories.add("New Category 3");
categories.addAll(newCategories);
adapter.notifyDataSetChanged(); // Notify the adapter that the data set has changed
}
}
You can use this summary as a quick reference for the topics discussed.