I'm trying to create a small android app but i can't get this activity work.
This is the Dashboard activity where i expect to see all products but it shows only the first one, the section where they should be shown gets bigger but the other object are not present, anywhere i press on this area the activity related to the first product is started.
public class DashBoardActivity extends AppCompatActivity {
ActivityDashBoardBinding binding;
private ProductsAdapter productsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityDashBoardBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.myToolbar);
productsAdapter = new ProductsAdapter(this);
binding.productRecycler.setAdapter(productsAdapter);
binding.productRecycler.setLayoutManager(new LinearLayoutManager(this));
getProducts();
binding.cart.setOnClickListener(view -> {
startActivity(new Intent(DashBoardActivity.this, CartActivity.class));
});
}
private void getProducts(){
FirebaseFirestore.getInstance()
.collection("products")
.whereEqualTo("show", true)
.get()
.addOnSuccessListener(queryDocumentSnapshots -> {
List<DocumentSnapshot> dsList = queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot ds:dsList){
ProductModel productModel = ds.toObject(ProductModel.class);
productsAdapter.addProduct(productModel);
}
});
}
}
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyViewHolder>{
private Context context;
private List<ProductModel> productModelList;
public ProductsAdapter(Context context){
this.context = context;
productModelList = new ArrayList<ProductModel>();
}
public void addProduct(ProductModel productModel){
productModelList.add(productModel);
notifyDataSetChanged();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_row, parent, false);
return new MyViewHolder(view);
}
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
ProductModel productModel = productModelList.get(position);
holder.title.setText(productModel.getTitle());
holder.description.setText(productModel.getDescription());
holder.price.setText(productModel.getPrice());
Glide.with(context).load(productModel.getImage())
.into(holder.img);
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("model", productModel);
context.startActivity(intent);
});
}
#Override
public int getItemCount() {
return productModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView title, description, price;
private ImageView img;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
price = itemView.findViewById(R.id.price);
img = itemView.findViewById(R.id.image);
}
}
}
Related
I have created a simple notes app in which I'm storing data in firebase. Showing the list of notes(time and few words of a note) in recyclerview. Now I want when I click in a particular note, it take to different activity where the details of the note will be shown. I am not able to get data from firebase column wise(note and time).
CustomAdapter
Here I'm sending the position of the item clicked to NotesDetails class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
Context context;
ArrayList<Model> models;
public CustomAdapter(Context context, ArrayList<Model> models){
this.context = context;
this.models = models;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_content, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.time_text.setText(models.get(holder.getBindingAdapterPosition()).getText());
holder.string_text.setText(models.get(holder.getBindingAdapterPosition()).getNotes());
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context.getApplicationContext(), NotesDetails.class);
intent.putExtra("position", String.valueOf(holder.getBindingAdapterPosition()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() { return models.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView string_text, time_text;
ConstraintLayout constraintLayout;
public MyViewHolder(View itemView) {
super(itemView);
string_text = itemView.findViewById(R.id.textView);
time_text = itemView.findViewById(R.id.timeshow);
constraintLayout = itemView.findViewById(R.id.constraintLayout);
}
}
}
NotesDetails
I want to show time and details of particular item here in testShow and timeShow
public class NotesDetails extends AppCompatActivity {
ArrayList<Model> models;
TextView textShow, timeShow;
DBHelper myDB;
DatabaseReference dbReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_details);
textShow = findViewById(R.id.notesShow);
timeShow = findViewById(R.id.timeshow);
myDB = new DBHelper(this);
Intent intent = getIntent();
int position = Integer.parseInt(intent.getStringExtra("position"));
dbReference = FirebaseDatabase.getInstance().getReference("Model").child("notes");
dbReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot: snapshot.getChildren()){
Model m1 = dataSnapshot.getValue(Model.class);
textShow.setText((CharSequence) m1);
}
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
}
});
}
}
Can someone help me test this recycler view adapter? I wan't to make a local unit test, but doesn't really know how to begin?
Following is my recyclerViewAdapter class:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<TasksTask> task;
protected Context context;
public RecyclerViewAdapter(Context context, List<TasksTask> task) {
this.task = task;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerViewHolders viewHolder = null;
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, parent, false);
viewHolder = new RecyclerViewHolders(layoutView, task);
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.categoryTitle.setText(task.get(position).getTask());
}
#Override
public int getItemCount() {
return this.task.size();
}}
And here is my recyclerViewHolder class:
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
private static final String TAG = RecyclerViewHolders.class.getSimpleName();
public TextView categoryTitle;
public ImageView deleteIcon;
private List<TasksTask> taskObject;
private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
private FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
public RecyclerViewHolders(final View itemView, final List<TasksTask> taskObject) {
super(itemView);
this.taskObject = taskObject;
categoryTitle = (TextView) itemView.findViewById(R.id.task_title);
deleteIcon = (ImageView) itemView.findViewById(R.id.task_delete);
deleteIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String taskTitle = taskObject.get(getAdapterPosition()).getTask();
mDatabase.child("Users").child(firebaseUser.getUid()).child("Tasks").child(taskTitle).removeValue();
}
});
}
}
I've been wondering that myself and have found this:
https://chelseatroy.com/2015/09/27/android-examples-a-test-driven-recyclerview/
there are example tests for each adapter method in there, hope that helps
I ran into situation where the RecyclerView with GridLayoutManager is not working on onClick(). Like ListView, when I tap/click on a picture in a GridView, the onClick is not returning the single item details. When I click on a poster of a movie, it should only display the details about the particular movie, but instead it is displaying multiple movies details in a scroll view.
Look at the code below:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
#Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
final MoviesGridAdapter.PosterViewHolder vh = new MoviesGridAdapter.PosterViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Context zContext = context;
Intent intent = new Intent(zContext, MoviesDetailActivity.class);
Movies movies = movieItems.get(vh.getAdapterPosition());
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection 1", movies.getTitle());
intent.putExtra("Items Selection 2", movies.getReleaseDate());
intent.putExtra("Items Selection 3", movies.getVoteAverage());
intent.putExtra("Items Selection 4", movies.getOverview());
zContext.startActivity(intent);
}
});
return vh;
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Context mcontext = context;
Movies movies = movieItems.get(position);
String posterUrlPath = movies.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
}
#Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(final View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
And following is the Fragment:
public class MoviesFragment extends Fragment {
private static final int COLUMN_COUNT = 2;
private List<Movies> movieItems = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesGridAdapter adapter;
private Subscription subscription;
public MoviesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movies_fragment_content, container, false);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), COLUMN_COUNT);
recyclerView = (RecyclerView) view.findViewById(R.id.movies_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
adapter = new MoviesGridAdapter(getActivity(), movieItems);
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onStart() {
super.onStart();
if (movieItems != null) {
adapter.setMoviesData(movieItems);
}
loadDate();
}
private void loadDate() {
MoviesAPI api = RetrofitManager.getMoviesClient().create(MoviesAPI.class);
subscription = api.getPopularMovies(APIKeys.MOVIES_DB_API_KEY)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
new Action1<MoviesResponse>() {
#Override
public void call(MoviesResponse response) {
movieItems = response.getMovies();
adapter.setMoviesData(response.getMovies());
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
}
}
);
}
}
Thank you guys for the attention. The solution I am looking is: when I click a picture from the grid view, I need the details about the particular movie like title, overview, releaseDate etc.
Make your model parcelable or send your model fields one by one but using a different keyword. You're adding same keyword with different values in here:
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection", movies.getTitle());
intent.putExtra("Items Selection", movies.getReleaseDate());
intent.putExtra("Items Selection", movies.getVoteAverage());
intent.putExtra("Items Selection", movies.getOverview());
Here's a tutorial link how you can make your model parcelable. And after that you can update your adapter as following:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
#Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
return new PosterViewHolder(view);
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Movies movie = movieItems.get(position);
String posterUrlPath = movie.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, MoviesDetailActivity.class);
intent.putExtra("movie", movie)
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
Don't set your click listener in onCreateViewHolder. Instead set your listener in onBindViewHolder method
So I have a CardView in a RecyclerView which has some data inside it. I would like to click on a specific card and launch a new fragment that displays specific data from the data in the CardView (more details about the data in the CardView). Here is the relevant code.
I don't have the most experience in programming and Android in general, any help would be appreciated!
DetailsFragment: `
public class DetailsFragment extends Fragment {
private CardView cardView;
private RecyclerView rv;
private List<Detail> detailList = new ArrayList<>();
public DetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
Context context = getActivity();
cardView = (CardView) rootView.findViewById(R.id.cv);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
final LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData() {
detailList = new ArrayList<>();
// not sure here
detailList.add(new Detail(" "));
}
private void initializeAdapter(){
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
}
}
RVNewsAdapater:
public class RVNewsAdapter extends RecyclerView.Adapter<RVNewsAdapter.NewsViewHolder> {
public static final String KEY_LINK ="link";
public static class NewsViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private static final String TAG = "Hello";
CardView cv;
TextView date;
TextView link;
TextView title;
TextView today;
public NewsViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
date = (TextView) itemView.findViewById(R.id.date);
link = (TextView) itemView.findViewById(R.id.link);
title = (TextView) itemView.findViewById(R.id.heading);
today = (TextView) itemView.findViewById(R.id.today);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Context context = view.getContext();
Intent intent = null;
for(int postion = 0; postion<42; postion++) {
intent = new Intent(context, Details.class);
}
context.startActivity(intent);
}
}
List<News> news;
RVNewsAdapter(List<News> news) {
this.news = news;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVNewsAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
NewsViewHolder nvh = new NewsViewHolder(v);
return nvh;
}
#Override
public void onBindViewHolder(NewsViewHolder newsViewHolder, int i) {
newsViewHolder.date.setText(news.get(i).date);
newsViewHolder.link.setText(news.get(i).link);
newsViewHolder.title.setText(news.get(i).title);
newsViewHolder.today.setText(news.get(i).today);
}
#Override
public int getItemCount() {
if (news != null) {
return news.size();
}
return 0;
}
}
RVDetailAdapter:
public class RVDetailAdapter extends RecyclerView.Adapter<RVDetailAdapter.DetailViewHolder> {
public static class DetailViewHolder extends RecyclerView.ViewHolder {
TextView link;
public DetailViewHolder(View itemView) {
super(itemView);
link = (TextView) itemView.findViewById(R.id.link);
}
}
List<Detail> detail;
RVDetailAdapter(List<Detail> detail) {
this.detail = detail;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVDetailAdapter.DetailViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
DetailViewHolder dvh = new DetailViewHolder(v);
return dvh;
}
#Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int i) {
detailViewHolder.link.setText(detail.get(i).link);
}
#Override
public int getItemCount() {
if (detail != null) {
return detail.size();
}
return 0;
}
}
Not sure if you're still looking for an answer, but I just saw the post and thought I'd give you one option.
When I'm using a RecyclerView (doesn't matter if it's using the CardView or not, it's the same for any list item), I create a a click listener for the view item in the Adapter. When the user clicks the item/cardview, I dispatch an item click event. It's your choice on how you respond - you can use a standard callback interface (and pass in your listener when you create the adapter) or use something like EventBus.
Here's an example of the code you would add to your adapter class:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemClick(view);
}
});
return new ViewHolder(view);
}
private void itemClick(View view) {
//Handle item event here
//EventBus.getDefault().post(new ItemSelectedEvent(/*send apprpopriate data*/));
}
I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);