我有一个活动,它根据http请求返回的系列值列表创建一个列表。如果列表包含一个位置列表,我调用下一个活动并通过意图传递一些值。但是,如果响应不包含地点列表,我需要使用新的系列值列表重新填充我的活动,但我不确定如何在我已经进行的活动中调用相同的活动。我的尝试在下面。
编辑:刷新活动的目的是在网址中使用新参数发出https请求。我之前尝试过回忆一下http请求,但没有运气
protected void onPostExecute(JSONObject result){
ArrayList<String> modelNames = new ArrayList<>();
Log.d("JSON RESPONSE", result.toString());
final ArrayList<JSONObject> modelsObjectList = new ArrayList<>();
try {
JSONObject info = result.getJSONObject("info");
JSONArray models = info.getJSONArray("models");
for(int i = 0; i < models.length(); i++){
modelsObjectList.add(models.getJSONObject(i));
}
for(int i = 0; i < modelsObjectList.size(); i++){
modelNames.add(modelsObjectList.get(i).getString("name"));
}
} catch(JSONException e ){
Log.d("JSONEXCEPTION", e.getMessage().toString());
}
//TODO found problem, model names for whatever reason is passing in the names from previous activity
Log.d("MODEL NAMES", modelNames.toString());
Toast.makeText(AfterDestinationActivity.this, modelNames.toString(), Toast.LENGTH_LONG).show();
mList = (ListView) findViewById(android.R.id.list);
CustomMainAdapter mainAdapter = new CustomMainAdapter(AfterDestinationActivity.this, modelNames);
mList.setAdapter(mainAdapter);
setProgressBarIndeterminateVisibility(false);
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
String name;
Long id;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JSONObject passingObject = modelsObjectList.get(position);
ArrayList<String> nextPlacesList = new ArrayList<>();
Log.d("AD OBJECT PRESSED ON", passingObject.toString());
//TODO found issues with debugging
try {
JSONArray places;
JSONArray series = passingObject.getJSONArray("series");
name = passingObject.getString("name");
id = passingObject.getLong("id");
Log.d("NEXT ACTIVITY NAME", name);
System.out.println("NEXT ACTIVITY ID " + id);
Log.d("NEXT SERIES LIST", nextPlacesList.toString());
//if there are no places, launch http request again for series
if(passingObject.optJSONArray("places") == null){
ArrayList<String> sameSeries = new ArrayList();
System.out.println("WENT INTO FOR LOOP");
for(int i = 0; i < series.length(); i++){
sameSeries.add(series.getString(i));
Log.d("AD FOR LOOP SERIES", series.getString(i));
}
Intent refreshActivity = new Intent(AfterDestinationActivity.this, AfterDestinationActivity.class);
refreshActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
refreshActivity.putExtra("id", id);
refreshActivity.putExtra("name", name);
refreshActivity.putExtra("placesList", nextPlacesList);
Log.v("RESTARTING ACTIVIY", "activity restating");
startActivity(refreshActivity);
}
} catch(JSONException e){
Log.d("JSONEXCEPTION", "IN THERE " + e.getMessage());
}
Intent intent = new Intent(AfterDestinationActivity.this, PlacesListActivity.class);
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("placesList", nextPlacesList);
startActivity(intent);
}
});
}
答案 0 :(得分:0)
只需使用此, 将此添加到您的清单中,
<activity android:name=".StartActivity" android:label="@string/app_name"
android:clearTaskOnLaunch="true">
</activity>
从JAVA代码中使用此,
Intent refreshActivity = new Intent(AfterDestinationActivity.this, AfterDestinationActivity.class);
refreshActivity.putExtra("id", id);
refreshActivity.putExtra("name", name);
refreshActivity.putExtra("placesList", nextPlacesList);
Log.v("RESTARTING ACTIVIY", "activity restarting");
startActivity(refreshActivity);
答案 1 :(得分:0)
您不需要拨打相同的活动。只需使用活动中的新值更新相应的视图即可。
答案 2 :(得分:0)
我会通过&#34;这个指针&#34;来访问它。或界面:
的修改
MyActivity extends Activity implements IUpdateListener
{
public void onCreate(Bundle onSaveInstanceState)
{
//code
}
@Override
void onUpdate(Result result)
{
//update data or do other stuff - like start another activity or populate urs.
}
}
下一篇AsynTask:
MyAsyncTask extends AsyncTask<Void, Void, Result>
{
private IUpdateListener listener;
public MyAsyncTask(IUpdateListener listener)
{
this.listener = listener;
}
Result doInBackground(Void.. voids)
{
//http request and parse - an object Result
return result data;
}
void onPostExecute(Result result)
{
if(result!=null) - or other operation u want
{
listener.OnUpdate(result) - or other stuff you want.
}
}
接下来的界面:
interface IOnUpdateListener
{
onUpdate(Result result);
}
&#34;这个指针&#34;:
How can I access my Activity's instance variables from within an AlertDialog's onClickListener?
我希望我很清楚。