https://stackoverflow.com/a/14164318/3575963
在这里,他使用了
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
来自父类。我的父母也没有inflater
,这是一张地图。
来自asynctask doitbackground
,我在textview
返回了5个arraylists以postexecute
。
但是我做不到因为我不能使用findviewbyid,因为我无法获得活动。我有上下文,但没有做任何事情。
这是我的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");
这部分是错误的
LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);
我尝试了其他的东西,但它没有用。我将使用之前未使用的其他布局。
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>
此处来电地图活动类
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
这里的错误
View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
因为我没有inflater
我不想从postexecute返回到主类或其他包装类。我想我可以在postexecute里面做,不能吗?
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
如果我这样做,我将采用当前视图,而不是我想创建的视图?
我试过
private Inflater inflater;
View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
答案 0 :(得分:5)
是的,只需将Activity
传递给Asynctask
:
AsyncTask myAsyncTask = new MyTask(this);
然后你会在layout
的{{1}}内找到该元素:
Activity
像丹尼尔描述这将是一种不好的做法。
例如:
a)创建接口类。
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);
...
...
}
}
b)转到AsyncTask类,并将接口AsyncResponse声明为字段:
public interface AsyncResponse {
void processFinish(String output);
}
c)在您的主Activity中,您需要实现AsyncResponse接口。
public class MyAsyncTask extends AsyncTask{
public AsyncResponse delegate = null;
@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
}
答案 1 :(得分:0)
这是解决方案..
some_dict = {'a':{}, b:{}}
答案 2 :(得分:-1)
只需将Activity作为参数传递给AsyncTask类。见here。请注意,将上下文存储为成员变量的不良做法是因为上下文可能会发生变化。