2016-07-20 6 views
0

이 코드를 실행할 때 getLayoutInflater 메소드를 해석하지 않을 때 다음과 같은 오류가 발생합니다. 제발 sugggest error ::는 기호 메서드 getLayoutInflater를 찾을 수 없습니다. 내가 기호 메소드 getLayoutInflater를 찾을 수 없습니다.

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; 


    } 
} 

mainActivity.java

여기

코드를 붙여하고 이러한 관련 XML 파일이 mainactivity.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

당신이 하나 개의 파일에 모든 어댑터 클래스의 활동과 모델 클래스를 사용하는 ??? –

+0

가능한 [오류 해결 getLayoutInflater] (http://stackoverflow.com/questions/5065451/error-resolving-getlayoutinflater) – Ironman

답변

0

onclicklayout.xml getLayoutInflater는 방법이 아니라 변수입니다. 당신은 getLayoutInflater이 방법처럼 사용할 수 있어야 getLayoutInflater() 대신

+0

구문 상 올바르지 만 문제는 해결되지 않습니다 – devnull69

1

getLayoutInflater의 필요 : 또한이 방법을 사용할 수 있습니다

getLayoutInflater() 

:

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

다른 방법뿐만 아니라 :

이 있습니다
+1

두 번째 코드 샘플 만 작동합니다. OP의 코드는 어댑터 내부에있다. – devnull69

+0

음, 나는 그가 MainActivity.this 또는 이것을 호출하기 전에 뭔가를 할 것이라고 생각했다. :) – Kushan

0

어댑터 내부에 있으므로 getLayoutInflater를 사용할 수 없습니다 (괄호를 추가 한 경우에도 마찬가지 임).

대신이 시도) :

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

view = inflater.inflate(R.layout.onclicklayout, viewGroup, false); 
관련 문제