android - How To Use non-staticHow “getSystemService” in Custom adapter? -
in android eclipse when want use non-statichow “getsystemservice", show me error:
cannot make static reference non-static method getcontext() 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 global class class 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(); } }
when use "getsystemservice" out "getcontext()" show me below error: method getsystemservice(string) undefined type adapternote.viewholder
------------------------------- edit
thank raghunandan adapternote.java be:
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; } }
pass context adapter constructor
new adapternote(arraylist<structnote> array,activityname.this)
then
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; }
then
convertview = minflater.inflate(r.layout.adapter_notes, parent, false);
then
android.text.clipboardmanager clipboard = (android.text.clipboardmanager)context.getsystemservice(context.clipboard_service);
Comments
Post a Comment