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());
Related
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.
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
I'm trying to create nested listviews in multiple fragments using adapter. I'm able to create it in single activity but I couldn't do it in fragments using adapter. Refer second fragment as fragment2 and second list as listview2.
package com.example.admin.volleyrequestandresponse.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.admin.volleyrequestandresponse.R;
import com.example.admin.volleyrequestandresponse.activities.NavigationActivity;
import com.example.admin.volleyrequestandresponse.fragments.HomeFragment;
import java.util.ArrayList;
public class HomeFragmentAdapter extends BaseAdapter {
private ArrayList<String> stringArray;
private ArrayList<String> integerArray;
int[] logos;
Context context;
NavigationActivity navigationActivity;
public HomeFragmentAdapter(Context context, ArrayList<String> stringArray, ArrayList<String> integerArray, int[] logos) {
this.context = context;
this.stringArray = stringArray;
this.integerArray = integerArray;
this.logos = logos;
navigationActivity = new NavigationActivity();
}
#Override
public int getCount() {
return integerArray.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.home_adapter, null);
holder = new ViewHolder();
holder.imageview = (ImageView) convertView.findViewById(R.id.indeximg);
holder.txtTitle = (TextView) convertView.findViewById(R.id.indexname);
holder.text = (TextView) convertView.findViewById(R.id.indexvalue);
holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.home_adapter);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.imageview.setImageResource(logos[position]);
holder.txtTitle.setText(stringArray.get(position));
holder.text.setText(integerArray.get(position));
return convertView;
}
private class ViewHolder {
RelativeLayout relativeLayout;
TextView text;
TextView txtTitle;
ImageView imageview;
}
}
Fragment code to call the adapter
homeFragmentAdapter = new HomeFragmentAdapter(getActivity(),index_string,index_values,index_logo);
listview_HomeFragment.setAdapter(homeFragmentAdapter);
I'm creating a custom gridView and I need to have an image and a text for the elements of the gridView.
I have this adapter for the gridView but I can't solve the error I have:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
*
* #author manish.s
*
*/
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
The error I have is here:
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
It says: The method item.getTitle (and also item.getImage) is undefined for the type ClipData.Item
What am I missing?
The error is very specific: The method you used isn't defined for ClipData.Item.
For the text try to use:
holder.txtTitle.setText(item.getText());
For the image it really depend on the way you built your Item.