2011-07-13 32 views
4

所以我只是想目前我getView功能的getContext()由於某種原因,充氣視圖是說這是不確定的..的getContext()。getSystemService錯誤

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.Bundle; 
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); 
      } 
     }); 
    } 
} 

這是一段代碼我遇到問題了。 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); getContext()沒有爲類定義,我不明白爲什麼:(

回答

9

你調用它的類EfficientAdapter1不延伸活動,也沒有這樣的方法

就可以了Context字段添加到內部類和呼叫getSystemService

/* 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 */ 
+0

謝謝!很有幫助。 – cj1098

0

getContext()不是BaseAdapter上的方法。將給定的上下文存儲在構造函數中的成員變量中, 。這

0

您正在使用BaseAdapter所以你不能使用

getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

ArrayAdapter的情況下,可以按如下方式使用它:

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

Context在你的內部類作爲參數,然後在那裏使用它,你需要它。

0

如果您不需要從其他活動的機會你BaseAdapter,您可以用Alarm_Settings.this替代的getContext()

相關問題