-6

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();
    }
}

Error in my Android Studio

What can i do? please help me thnx

  • 1
    Without any knowledge: Restart android studio. Check Documentation of android.view.LayoutInflater. Check your code for wrong place brackets and other syntax fails. – Luftbaum Jul 06 '17 at 06:45
  • Possible duplicate: https://stackoverflow.com/questions/19508649/android-studio-says-cannot-resolve-symbol-but-project-compiles – Spitzbueb Jul 06 '17 at 06:47
  • 1
    Remove the **new**. – Enzokie Jul 06 '17 at 06:54

4 Answers4

4

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());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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);
    }
Suraj Makhija
  • 1,376
  • 8
  • 16
1

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;
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

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());
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61