2014-04-21 62 views
2

在Android的日食 當我想用非staticHow「getSystemService」,它告訴我錯誤:如何在自定義適配器中使用非staticHow「getSystemService」?

Cannot make a static reference to the non-static method getContext() from the type ArrayAdapter

ArrayAddapter.java:

package ir.redreactor.app.Com.NovinEr; 

import java.util.ArrayList; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


public class AdapterNote extends ArrayAdapter<StructNote> { 

public AdapterNote(ArrayList<StructNote> array) { 
    super(G.context, R.layout.adapter_notes, array); 
} 


private static class ViewHolder { 

    public ViewGroup LayoutRt; 
    public TextView txtTitle; 
    public TextView txtSort; 
    public TextView txtTableName; 
    public TextView txtID; 
    public TextView txtpos; 
    public TextView txtDetail; 


    public ViewHolder(View view) { 
     txtTitle = (TextView) view.findViewById(R.id.txtTitleAN); 
     txtSort = (TextView) view.findViewById(R.id.txtSortAN); 
     txtTableName = (TextView) view.findViewById(R.id.txtTableNameAN); 
     txtID = (TextView) view.findViewById(R.id.txtIDAN); 
     txtpos = (TextView) view.findViewById(R.id.txtpos); 
     txtDetail = (TextView) view.findViewById(R.id.txtDetailAN); 
     LayoutRt = (ViewGroup) view.findViewById(R.id.layout_Rt); 
    } 


    private void copy(String strdata) { 
     android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); 
     clipboard.setText(strdata); 
    } 


    public void fill(ArrayAdapter<StructNote> Adapter, StructNote Item, final int position) { 
     txtTitle.setText(Item.title); 
     txtDetail.setText(Item.detail); 
     txtSort.setText(Item.Sort.toString()); 
     txtTableName.setText(Item.nameOfTable); 
     txtID.setText(Item.intID.toString()); 
     txtpos.setText("" + position); 
     LayoutRt.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       copy(txtTitle.getText().toString() + "\n" + txtDetail.getText().toString()); 
      } 
     }); 
    } 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    StructNote item = getItem(position); 
    final ViewHolder holder; 
    if (convertView == null) { 
     convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.fill(this, item, position); 
    return convertView; 
} 

}

StructNote.java:

package ir.redreactor.app.Com.NovinEr; 

import android.app.Activity; 
import android.content.Context; 


public class StructNote { 

public String nameOfTable; 
public Integer Sort; 
public Integer intID; 
public Boolean Favorite; 
public String title; 
public String detail; 

}

G.java是全球性的類 類G.java:

public class G extends Application { 

    // Creating Context 
    public static Context    context; 
    // Creating Log 
    public static String    LOG_TAG   = "RR1"; 
    public static String    LOG_ERR   = "NVR"; 
    //Creating Current Activity Finder 
    public static Activity    currentActivity; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     context = this.getApplicationContext(); 
    } 
} 

當我使用 「getSystemService」 帶有OUT 「的getContext()」 它顯示我以下錯誤: 方法getSystemService(串)未定義類型AdapterNote.ViewHolder

-------------------------------編輯

感謝您Raghunandan 所以AdapterNote.java將是:

package ir.redreactor.app.Com.NovinEr; 

import java.util.ArrayList; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


public class AdapterNote extends ArrayAdapter<StructNote> { 

public LayoutInflater mInflater; 
public static Context context; 


public AdapterNote(ArrayList<StructNote> array, Context context) { 
    super(context, R.layout.adapter_notes, array); 
    mInflater = LayoutInflater.from(context); 
    this.context = context; 
} 


private static class ViewHolder { 

    public ViewGroup LayoutRt; 
    public TextView txtTitle; 
    public TextView txtSort; 
    public TextView txtTableName; 
    public TextView txtID; 
    public TextView txtpos; 
    public TextView txtDetail; 


    public ViewHolder(View view) { 
     txtTitle = (TextView) view.findViewById(R.id.txtTitleAN); 
     txtSort = (TextView) view.findViewById(R.id.txtSortAN); 
     txtTableName = (TextView) view.findViewById(R.id.txtTableNameAN); 
     txtID = (TextView) view.findViewById(R.id.txtIDAN); 
     txtpos = (TextView) view.findViewById(R.id.txtpos); 
     txtDetail = (TextView) view.findViewById(R.id.txtDetailAN); 
     LayoutRt = (ViewGroup) view.findViewById(R.id.layout_Rt); 
    } 


    private void copy(String strdata) { 
     android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
     clipboard.setText(strdata); 
    } 


    public void fill(ArrayAdapter<StructNote> Adapter, StructNote Item, final int position) { 
     txtTitle.setText(Item.title); 
     txtDetail.setText(Item.detail); 
     txtSort.setText(Item.Sort.toString()); 
     txtTableName.setText(Item.nameOfTable); 
     txtID.setText(Item.intID.toString()); 
     txtpos.setText("" + position); 
     LayoutRt.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       copy(txtTitle.getText().toString() + "\n" + txtDetail.getText().toString()); 
      } 
     }); 
    } 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    StructNote item = getItem(position); 
    final ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.adapter_notes, parent, false); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.fill(this, item, position); 
    return convertView; 
    } 
} 
+0

爲什麼'G.context'?將上下文傳遞給適配器的構造函數 – Raghunandan

+0

它在我的類中定義的名稱爲「G.java」 – HK1988

+0

什麼是G.java活動類? – Raghunandan

回答

1

傳遞上下文到適配器構造

new AdapterNote(ArrayList<StructNote> array,ActivityName.this) 

然後

LayoutInfalter mInflater; 
Context context; 
public AdapterNote(ArrayList<StructNote> array,Context context) { 
super(context, R.layout.adapter_notes, array); 
mInfalter = LayoutInfalter.from(context); 
this.context =context; 
} 

然後

convertView = mInflater.inflate(R.layout.adapter_notes, parent, false); 

然後

android.text.ClipboardManager clipboard = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); 
+0

謝謝你回答 – HK1988

+0

我可以有你的郵件嗎?我被阻止提問!我有一些問題,但沒有人可以幫助我 – HK1988