开发者

getContext().getSystemService error

So I'm just trying to currently inflate a view in my getView function and getContext() for some reason is saying it's undefined..

package com.MTSUAndroid;

import com.MTSUAndroid.Alarm_Settings.EfficientAdapter1.ViewHolder;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundl开发者_JS百科e;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;

public class Alarm_Settings extends ListActivity {
    public static class EfficientAdapter1 extends BaseAdapter{
        private LayoutInflater mInflater;

        public EfficientAdapter1(Context context){
            mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            int viewType = this.getItemViewType(position);

            switch (viewType)
            {
            case 1:
                holder = new ViewHolder();

                View v = convertView;
                if (v == null)
                {
                    LayoutInflater vi = null;
                    //LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.alerts,parent,false);

                    holder.text1 = (TextView)v.findViewById(R.id.menu_Cancel);
                    v.setTag(holder);
                }
                else {
                    holder = (ViewHolder)v.getTag();
                }
                return v;
            case 2:
                ViewHolder holder1 = new ViewHolder();

                View v1 = convertView;
                if (v1 == null)
                {
                    LayoutInflater vi = null;
                    //LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v1 = vi.inflate(R.layout.about, parent, false);

                    holder1.text1 = (TextView)v1.findViewById(R.id.menu_Cancel);
                    v1.setTag(holder1);
                }
                else {
                    holder1 = (ViewHolder)v1.getTag();
                }

                return v1;
            }
            return null;
        }

        static class ViewHolder{
            TextView text1;
        }

    }
    public void onCreate(Bundle SavedInstanceState)
    {
        super.onCreate(SavedInstanceState);
        setListAdapter(new EfficientAdapter1(this));
        ListView listview = getListView();
        listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent(Alarm_Settings.this, Alerts.class);
                startActivity(intent);
            }
        });
    }
}

This is the piece of code I'm having problems with. LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); getContext() isn't defined for the class and I don't understand why :(


You call it in the class EfficientAdapter1 which does not extend activity and has no such method.

Add a Context field to the internal class and call getSystemService on it:

/* snip */
public static class EfficientAdapter1 extends BaseAdapter{
    private LayoutInflater mInflater;
    private Context ctx;
    public EfficientAdapter1(Context context){
        mInflater = LayoutInflater.from(context);
        ctx = context;
    }
/* snip */
     LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/* snip */


getContext() is not a method on BaseAdapter. Store the context given to you in the constructor in a member variable and just use that.


If you don't need to access your BaseAdapter from other Activities, you can replace getContext() with Alarm_Settings.this


You are using BaseAdapter so you cannot use

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

In the case of ArrayAdapter you can use it as follows:

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Pass Context as a parameter in your inner class and then use it where you need it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜