简体   繁体   中英

Cannot resolve method getLayoutInflater

I want create a simple ListView with 2 textview. Original class : https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/LazyAdapter.java

But i have a big problem : I work on a Fragment, not on an Activity. So i have adapt the code.

Now i have a error in ListViewAdapter class (it's not my principal class):

LayoutInflater inflater =  context.getLayoutInflater();

"Cannot resolve method 'getLayoutInflater()'

import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pack.pack.R;

public class ListViewAdapter extends BaseAdapter
{
    Fragment context;
    String title[];
    String description[];

    public ListViewAdapter(Fragment context, String[] title, String[] description) {
        super();
        this.context = context;
        this.title = title;
        this.description = description;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return title.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView txtViewTitle;
        TextView txtViewDescription;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.simplerow, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtViewTitle.setText(title[position]);
        holder.txtViewDescription.setText(description[position]);

        return convertView;
    }

}

I have read that's not working in BaseAdapter. How we can do it ? Thank you.

EDIT : My listView class

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class commu extends Fragment {

    public static final String TAG = "commu";

    ListView lview;
    ListViewAdapter lviewAdapter;

    private final static String month[] = {"January","February","March","April","May",
            "June","July","August","September","October","November","December"};

    private final static String number[] = {"Month - 1", "Month - 2","Month - 3",
            "Month - 4","Month - 5","Month - 6",
            "Month - 7","Month - 8","Month - 9",
            "Month - 10","Month - 11","Month - 12"};


    @Nullable
    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.commu, container, false);

        lview = (ListView) rootView.findViewById(R.id.listView);
        lviewAdapter = new ListViewAdapter(this, month, number);

        //System.out.println("adapter => "+lviewAdapter.getCount());

        lview.setAdapter(lviewAdapter);

        //lview.setOnItemClickListener(this);


        return rootView;
    }

    /*public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
        // TODO Auto-generated method stub
        //Toast.makeText(this,"Title => "+month[position]+"=> n Description"+number[position], Toast.LENGTH_SHORT).show();
    }*/

    @Override
    public void onResume()
    {
        super.onResume();
        //wv6.onResume();
    }
    @Override
    public void onPause()
    {
        super.onPause();
        //wv6.onPause();
    }


}

您可以使用

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Use LayoutInflater.from(Context) . Make sure to pass an instance of Activity to the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM