import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import com.recipi.myapprishi.Model.RecipieModel;
import com.recipi.myapprishi.R;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Recipe_Adapter extends RecyclerView.Adapter<Recipe_Adapter.viewHolder> {
ArrayList<RecipieModel> list;
Context context;
public Recipe_Adapter(ArrayList<RecipieModel> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//this line have error indicated by logcat
View view = LayoutInflater.from(context).inflate(R.layout.sample_recyclerview , null);
//this line have error indicated by logcat
return new viewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
RecipieModel model = list.get(position);
holder.imageView.setImageResource(model.getPic());
holder.textView.setText(model.getText());
}
#Override
public int getItemCount() {
return list.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
public ImageSwitcher imageView;
ImageView imageview;
TextView textView;
public viewHolder(#NonNull View itemView) {
super(itemView);
imageview = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
what's wrong in this code it giving me run time error like unfortunately app has been stopped and logcat is indicating me on bolded line
Error :
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:32)
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:16)
You can inflate your view in this way:
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContet()).inflate(R.layout.sample_recyclerview , parent, false);
return new viewHolder(view);
}
Also, verify if R.layout.sample_recyclerview is the correct parameter.
Related
RecyclerView Code:OnClickListener
package com.example.cardview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class ArrayAdapter extends RecyclerView.Adapter<ArrayAdapter.ViewHolder> {
private String[] divisionName;
ArrayAdapter(String[] phoneNumebr) {
this.divisionName = phoneNumebr;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from( context );
View v = inflater.inflate( R.layout.element, parent, false );
return new ViewHolder( v );
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
holder.cNumbers.setText(divisionName[position] );
holder.itemView.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,divisionName[position],Toast.LENGTH_LONG).show();
}
} );
}
#Override
public int getItemCount() {
return divisionName.length;
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView cNumbers;
ViewHolder(#NonNull View itemView) {
super( itemView );
this.cNumbers = itemView.findViewById( R.id.cNumbers );
}
How to add OnClickListener in recyclerView according to the item position
How to add OnClickListener in recyclerView according to the item
position
To add OnClickListener to RecyclerView items, you achieve that within your custom RecyclerView.ViewHolder class, by setting .onClickListener() to the View passed into the ViewHolder constructor
snippet from your code:
static class ViewHolder extends RecyclerView.ViewHolder{
TextView cNumbers;
ViewHolder(#NonNull View itemView) {
super( itemView );
this.cNumbers = itemView.findViewById( R.id.cNumbers );
listItem.setOnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Click on position number: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(), divisionName[getAdapterPosition()],Toast.LENGTH_LONG).show();
}
}
}
}
Just Change Your Toast.
Toast.makeText(holder.itemView.getContext(),"You clicked "+divisionName[position],Toast.LENGTH_SHORT).show();
I can't put my textView in the method onBindViewHolder from the inner class Item, I made it public and final and is still cannot resolve it, I tried many examples and it's the same problem.
package com.example.recyclerview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
String [] items;
public Adapter(Context context, String[] items){
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.custom_row, viewGroup, false);
Item item = new Item(row);
return item;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
String mCurrent = items[i];
viewHolder.textView.setText(mCurrent); // error here textView cannot resolve
}
#Override
public int getItemCount() {
return items.length;
}
public class Item extends RecyclerView.ViewHolder {
public final TextView textView;
public Item(#NonNull View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item);
}
}
}
You should correct generic type of the adapter: use Adapter.Item instead of RecyclerView.ViewHolder
public class Adapter extends RecyclerView.Adapter<Adapter.Item>
and then update signature of onCreateViewHolder and onBindViewHolder methods
can not resolve symbol 'from' in
LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
this is my complete class:
package ir.sangram.sangram.chat;
import android.content.Context;
import android.provider.ContactsContract;
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.TextView;
import java.util.ArrayList;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
import ir.sangram.sangram.R;
/**
* Created by HOW on 07/04/2017.
*/
class ChatListViewHolder extends RecyclerView.ViewHolder{
public CircleImageView profile_picture;
public TextView user_name, last_chat, unread_messages;
public ChatListViewHolder(View itemView) {
super(itemView);
profile_picture = (CircleImageView) itemView.findViewById(R.id.profile_picture);
user_name = (TextView) itemView.findViewById(R.id.user_name);
last_chat = (TextView) itemView.findViewById(R.id.last_chat);
unread_messages = (TextView) itemView.findViewById(R.id.unread_messages);
}
}
public class ChatListAdapter extends RecyclerView.Adapter<ChatListViewHolder>{
private List<ChatListData> chat_list = new ArrayList<ChatListData>();
public ChatListAdapter(List<ChatListData> chat_list) {
this.chat_list = chat_list;
}
#Override
public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
//LayoutInflater inflater;// = new LayoutInflater.from(parent.getContext());
//inflater = ( LayoutInflater )parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);//!!!!!!!!!!
View itemView = inflater.inflate(R.layout.chat_list_row,parent,false);
return new ChatListViewHolder(itemView);
}
#Override
public void onBindViewHolder(ChatListViewHolder holder, int position) {
holder.profile_picture.setImageResource(chat_list.get(position).getProfile_picture_id());
holder.user_name.setText(chat_list.get(position).getUser_name());
holder.unread_messages.setText(chat_list.get(position).getUnread_messages());
holder.last_chat.setText(chat_list.get(position).getLast_chat());
}
#Override
public int getItemCount() {
return chat_list.size();
}
}
What can i do?
please help me
thnx
You're calling LayoutInflater.from as if you were calling a constructor:
LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
That's a mixture of constructor-call syntax (new) and method call syntax (.from). It would work if LayoutInflater.from were a static nested class, but it's not... it's just at static method. So you want:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
There is no need for "new" keyword. Try the code as follows :
#Override
public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.chat_list_row,parent,false);
return new ChatListViewHolder(itemView);
}
try this way my friend it will help you
View view = LayoutInflater.from(context).inflate(R.layout.cust_invitation, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
LayoutInflater.from() is an static method and to access static method you don't need to create an instance using new keyword.
Use:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Instead of:
LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
i am trying to make a listview in a nested fragment which will hold as elements images with captions beneath it, though i keep getting nullpointer errors inside my custom adapter i created to fill the listview. here is the code:
The custom adapter:
import android.app.Activity;
import android.view.View;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ImageAdapter extends ArrayAdapter<Image> {
ArrayList<Image> imageList;
private Context mContext;
int resource;
public ImageAdapter(Context context, int resource, ArrayList<Image> imageObjects) {
super(context, resource, imageObjects);
this.imageList = imageObjects;
this.mContext = context;
this.resource = resource;
}
public ArrayList<Image> getImageList() {
return imageList;
}
public void setImageList(ArrayList<Image> imageList) {
this.imageList = imageList;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public int getResource() {
return resource;
}
public void setResource(int resource) {
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);
}
Image image = getItem(position);
ImageView layoutImage = (ImageView)convertView.findViewById(R.id.membershipimage);
layoutImage.setImageResource(image.getImageResourceID());
TextView layoutText = (TextView)convertView.findViewById(R.id.membershipCaption);
layoutText.setText(image.getImageCaption());
return super.getView(position, convertView, parent);
}
}
The fragment i use, within another fragment:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import java.util.ArrayList;
public class MembershipFragment extends Fragment {
ArrayList<Image> imageList;
ListView membershipListView;
public static MembershipFragment newInstance() {
MembershipFragment fragment = new MembershipFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_membership, container, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
imageList = new ArrayList<>();
for(int i=0; i<3; i++){
Image image = new Image(R.drawable.cinnamon_apple_breakfast, "Test Caption");
imageList.add(image);
}
membershipListView = (ListView)view.findViewById(R.id.membershipListView);
try{
ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item, imageList);
membershipListView.setAdapter(adapter);
}
catch (Exception e){
e.printStackTrace();
}
}
}
The app keeps crashing at startup, throwing nullpointer exceptions in the custom adapter in the getView method, when i try to set the image resource id to the imageview. Where is the problem?
I think you're confusing your layouts.
And you should use getActivity here.
ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item
You passed that layout resource there. But inside the adapter, you're using a different layout.
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);
}
You made a getResource method, for a reason, I assume? You never call it.
I don't think you want your Fragment layout to hold a list of Fragment layouts (because recursion)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_membership, container, false);
return view;
}
Reading old questions I have and watching several tutorials I have written this code.
RecyclerAdapter.java
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String[] postText;
String[] groupName;
public RecyclerAdapter(String[] postText, String[] groupName){
this.postText = postText;
this.groupName = groupName;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.txPost.setText(postText[position]);
holder.txGroupName.setText(groupName[position]);
holder.cardView.setCardBackgroundColor(Color.GREEN);
}
#Override
public int getItemCount() {
return postText.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView txPost;
TextView txGroupName;
CardView cardView;
public RecyclerViewHolder(View view)
{
super(view);
txPost = (TextView) view.findViewById(R.id.post_text);
cardView = (CardView) itemView.findViewById(R.id.card_view);
txGroupName = (TextView) view.findViewById(R.id.group_name);
}
}
}
This works fine but now I need to change the colors randomly. I tried to pass an array string array of colors to the holder.cardView.setCardBackgroundColor(Color.GREEN); but it threw an error because it takes int. I am new to java and programming as a whole. What is the proper way of doing it?