prerna soni
prerna soni

Reputation: 1

cannot find symbol method getLayoutInflater.

I am getting the following error when I run this code , its not resolving getLayoutInflater method . Please sugggest error:: cannot find symbol method getLayoutInflater. I am pasting the code here

mainActivity.java

package com.example.prerna_soni.baseadapterlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    ArrayList<Employee> elist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        elist= new ArrayList<Employee>();
        for(int i= 0; i<10;i++)
        {
            Employee e= new Employee();
            e.setName ("Name"+i);
            e.setAddress("Address"+i);

            elist.add(e);

        }
        ListView listview = (ListView)findViewById(R.id.listView);
        MyAdapter myadapter= new MyAdapter(elist);
        listview.setAdapter(myadapter);
        listview.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        Employee e = elist.get(i);
        String name = e.getName();
        Toast.makeText(this,name,Toast.LENGTH_LONG).show();

    }
}

 class Employee
{
    String name ;
    String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

class MyAdapter extends BaseAdapter{

    List<Employee> list;

    public MyAdapter(List<Employee> list) {
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater.inflate(R.layout.onclicklayout,null,false);

        TextView t1= (TextView)view.findViewById(R.id.name);
        TextView t2= (TextView)view.findViewById(R.id.address);
        Employee e= list.get(i);
        String name = e.getName();
        String Address= e.getAddress();
        t1.setText(name);
        t2.setText(Address);
        return view;


    }
}

these are the related XML files mainactivity.xml and onclicklayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.prerna_soni.baseadapterlistview.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

//this is the code for onclicklayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/name" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/address" />
</LinearLayout>

Upvotes: 0

Views: 3516

Answers (3)

devnull69
devnull69

Reputation: 16544

You are inside of an Adapter, so you cannot use getLayoutInflater there (even if you added parentheses)

Try this instead:

LayoutInflater inflater = LayoutInflater.from(getContext());

view = inflater.inflate(R.layout.onclicklayout, viewGroup, false);

Upvotes: 0

Kushan
Kushan

Reputation: 5984

getLayoutInflater should be used like a method:

getLayoutInflater()

You can also use this approach:

LayoutInflater.from(MainActivity.this).inflate(R.layout.onclicklayout,viewGroup,false);

There are other ways as well :)

Upvotes: 1

parohy
parohy

Reputation: 2180

getLayoutInflater is a method, not a variable. You need to getLayoutInflater() instead of getLayoutInflater

Upvotes: 0

Related Questions