Reputation:
https://stackoverflow.com/a/14164318/3575963
Here, he used
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
From parent class. Also I don't have inflater
in my parent, it is a map.
And from asynctask doitbackground
, I returned 5 arraylists to make textview
in postexecute
.
But I can't do because I can't use findviewbyid because I can't get activity. I got context but it does not do anything.
Here is my postexecute
protected void onPostExecute(Wrapper wrap){
TextView name = new TextView (mContext);
TextView type = new TextView (mContext);
TextView location = new TextView (mContext);
TextView distance = new TextView (mContext);
List<Double> dist = new ArrayList();
List<String> loc = new ArrayList();
List<String> nme = new ArrayList();
List<String> typ = new ArrayList();
List<Calendar> start = new ArrayList();
List<Calendar> endd = new ArrayList();
dist = wrap.getDist();
loc = wrap.getLocation();
nme = wrap.getName();
typ = wrap.getType();
start = wrap.getsDate();
endd = wrap.geteDate();
int idx = -1;
LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);
for(Double dis:dist){
idx++;
name.setText(nme.get(idx));
type.setText(typ.get(idx));
location.setText(loc.get(idx));
distance.setText(dist.get(idx).toString()+" meters");
This part is faulty
LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);
I tried other things but it did not work. I will use another layout that was not used before.
show_events.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
android:id="@+id/shwevnt"
</LinearLayout>
Here caller mapactivity class
public class MapActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private int strokeColor = 0xffff0000; //red outline
private int shadeColor = 0x44ff0000; //opaque red fill
private int count=0;
private GoogleMap googleMap;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;
private Context mContext;
private String TAG = "Chic";
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private int radius;//in meters
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "inside map oncreaste");
mContext = getApplicationContext();
View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
super.onCreate(savedInstanceState);
setUpMapIfNeeded();
Log.d(TAG, "buildgoogleapi called");
buildGoogleApiClient();
Log.d(TAG, "after buildgoogleapi called");
}//end of oncreate
here error on that
View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
because i dont have inflater
I dont want to return from postexecute to main class or another wrapper class. I think i can do inside postexecute, cant i?
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
if i do this, i will take current view, not the view i want to create?
I tried
private Inflater inflater;
View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
Upvotes: 1
Views: 3880
Reputation: 1
this is the solution..
facturas_edittext=(EditText)((Activity)context).findViewById(R.id.mye);
Upvotes: 0
Reputation: 126445
Yep, just pass the Activity
to the Asynctask
:
AsyncTask myAsyncTask = new MyTask(this);
And then you will find the element inside the layout
of the Activity
:
public class MyTask extends AsyncTask<String, String, String>{
public MyActivity activity;
public MyTask(MyActivity a){
this.activity = a;
}
protected void onPostExecute(String result){
...
...
LinearLayout shw_evnt = (LinearLayout) activity.findViewById(R.id.shwevnt);
...
...
}
}
Like Daniel describes it would be a bad practice.
for example:
a) Create interface class.
public interface AsyncResponse {
void processFinish(String output);
}
b) Go to your AsyncTask class, and declare interface AsyncResponse as a field :
public class MyAsyncTask extends AsyncTask{
public AsyncResponse delegate = null;
@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
}
c) In your main Activity you need to implements interface AsyncResponse.
public class MainActivity implements AsyncResponse{
MyAsyncTask asyncTask =new MyAsyncTask();
@Override
public void onCreate(Bundle savedInstanceState) {
//this to set delegate/listener back to this class
asyncTask.delegate = this;
//execute the async task
asyncTask.execute();
}
//this override the implemented method from asyncTask
void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
LinearLayout shw_evnt = (LinearLayout) findViewById(R.id.shwevnt);
}
}
Upvotes: 5
Reputation: 9825
Just pass the Activity as a parameter to the AsyncTask class. See here. Note that its bad practice to store context as a member variable since the context may change.
Upvotes: -1