在Android eclipse中
当我想使用non-staticHow“ getSystemService”时,显示错误:


  无法从类型ArrayAdapter静态引用非静态方法getContext()


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”与“ getContext()”一起使用时,它向我显示以下错误:
未为类型AdapterNote.ViewHolder定义方法getSystemService(String)

-------------------------------编辑

谢谢你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;
  }
}

最佳答案

将上下文传递给适配器构造函数

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);

关于android - 如何使用非静态自定义适配器中的“getSystemService”如何?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23196859/

10-10 16:02