当我运行此代码时,我收到以下错误,它没有解析getLayoutInflater方法。请建议 error ::找不到符号方法getLayoutInflater。 我在这里粘贴代码
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;
}
}
这些是相关的XML文件mainactivity.xml和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>
答案 0 :(得分:1)
getLayoutInflater应该像方法一样使用:
getLayoutInflater()
您也可以使用此方法:
LayoutInflater.from(MainActivity.this).inflate(R.layout.onclicklayout,viewGroup,false);
还有其他方法:)
答案 1 :(得分:0)
getLayoutInflater是一个方法,而不是变量。
您需要getLayoutInflater()
而不是getLayoutInflater
答案 2 :(得分:0)
你在一个适配器里面,所以你不能在那里使用getLayoutInflater(即使你添加了括号)
请改为尝试:
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.onclicklayout, viewGroup, false);