android - Getting Context of a fragment -



android - Getting Context of a fragment -

i developing application read spectrum info txt file , using achartengine plotting points. using fragment , trying add together graphicalview returned getview(context context) method of scattergraphplot .java class linear layout of fragment. cannot figure out context passed getview (context context) method graphicalview. piece of code mainactivity.java file reading file , adding view

loadspec = (button) findviewbyid(r.id.loadspec); loadspec.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub filepath = filename.gettext().tostring(); filepath = "/sdcard/" + filepath + ".txt"; seek { bufferedreader br = new bufferedreader(new filereader(filepath)); string specdata = ""; int channelval = 1; while ((specdata = br.readline()) != null) { scatter.stringproc(specdata, channelval); specdata = ""; channelval++; } br.close(); scatter.addpointstochart(channelval); fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); chartfragment cf = new chartfragment(); ft.add(r.id.chart_fr, cf); ft.addtobackstack("cf"); ft.commit(); //code adding graphicalview in fragment //by calling getview(context context) method. }

as can see there loadspec button plot spectrum after reading .txt file scatter.addpoints(int) add together points series after adding points fragment transaction done , cannot figure out context passed getview(context context) getview(context context) method.

public graphicalview getview(context context) { chartview = chartfactory.getscatterchartview(context,dataset,mrenderer); homecoming chartview; }

this chartfragment class extending fragment class

package com.example.spectrumreaderfragments; import org.achartengine.graphicalview; import android.app.fragment; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; public class chartfragment extends fragment { @override public view oncreateview(layoutinflater inflator,viewgroup container, bundle savedinstancestate) { view v = inflator.inflate(r.id.chart_fr,container,false); homecoming v; } }

hope help me out this. in advance. allow me know if other part of code posted

you can utilize getactivity() method of fragment context.

so for,

public graphicalview getview(context context) { chartview = chartfactory.getscatterchartview(context,dataset,mrenderer); homecoming chartview; }

this work phone call view - getview(getactivity().getapplicationcontext()).

note:

be careful you're calling this, getactivity() homecoming null if fragment has been detached.

android achartengine

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -