从片段开始活动
Posted
技术标签:
【中文标题】从片段开始活动【英文标题】:Starting Activity From Fragment 【发布时间】:2016-03-31 11:18:47 【问题描述】:我有两个 AsyncTask(FetchDataFromApi,FetchDataFromDb),它们都有一个“onPostExecute”方法的实现,每个都通过调用 passDataToDetailedActivity 方法来启动 DetailActivity。 每当我从 FetchFromApi onPostExecute 方法启动DetailedActivity 时,它都运行良好, 但整个问题是当我尝试从 FetchFromDb onPostExecute 方法启动DetailedActivity 时,因为DetailedActivity UI 的某些组件没有出现而其他组件出现。
请帮忙,,提前谢谢:)
public class MainFragment extends Fragment
public final static String API_KEY = "b932ba435fc93a5944938fe9d44cd198";
public final String BASE_URL = "http://api.themoviedb.org/3/discover/movie?sort_by=&api_key=";
ArrayList<Movie> movies = new ArrayList<>();
movieAdapter adapter;
GridView gridview;
DbOpenHelper dbOpenHelper;
ArrayList<Movie> FavList = new ArrayList<>();
public MainFragment()
public MainFragment(String SortBy)
if (SortBy == "fav")
new FetchDataFromDb().execute();
else
new FetchDataFromApi(SortBy).execute();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);
gridview = (GridView) getActivity().findViewById(R.id.gridView);
adapter = new movieAdapter(getActivity(), R.layout.movie_item, movies);
return view;
class FetchDataFromApi extends AsyncTask<String, Void, Boolean>
String SORT_CATEGORY;
FetchDataFromApi(String paramUrl)
SORT_CATEGORY = paramUrl;
@Override
protected Boolean doInBackground(String... params)
HttpURLConnection urlConnection = null;
try
Uri uriBuilder = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter("sort_by", SORT_CATEGORY)
.appendQueryParameter("api_key", API_KEY)
.build();
URL url = new URL(uriBuilder.toString());
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
sb.append(line);
String response = sb.toString();
ParseJsonData(response);
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
finally
urlConnection.disconnect();
return null;
@Override
protected void onPostExecute(Boolean result)
super.onPostExecute(result);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
PassDataToDetailedActivity(position);
);
private void ParseJsonData(String response) throws JSONException
String PosterBaseUrl = "http://image.tmdb.org/t/p/";
String LargePoster = "w185";
String SmallPoster = "";
final String RESULTS = "results";
final String ORIGINAL_TITLE = "original_title";
final String OVERVIEW = "overview";
final String RELEASE_DATE = "release_date";
final String POSTER_PATH = "poster_path";
final String VOTE_AVERAGE = "vote_average";
final String POPULARITY = "popularity";
final String ID = "id";
JSONObject jsono = new JSONObject(response);
Log.v("Json", response);
JSONArray jarray = jsono.getJSONArray(RESULTS);
for (int i = 0; i < jarray.length(); i++)
JSONObject object = jarray.getJSONObject(i);
String id = object.getString(ID);
String title = object.getString(ORIGINAL_TITLE);
String releaseDate = object.getString(RELEASE_DATE);
String overView = object.getString(OVERVIEW);
String voteAverage = object.getString(VOTE_AVERAGE);
String popularity = object.getString(POPULARITY);
String posterPath = object.getString(POSTER_PATH);
String BigPoster = PosterBaseUrl + LargePoster + posterPath;
String MinPoster = PosterBaseUrl + SmallPoster + posterPath;
Movie m = new Movie();
m.setId(id);
m.setTitle(title);
m.setReleaseDate(releaseDate);
m.setOverView(overView);
m.setVoteAverage(voteAverage);
m.setPopularity(popularity);
m.setLargePoster(BigPoster);
m.setMinPoster(MinPoster);
movies.add(m);
private void PassDataToDetailedActivity(int position)
String id = movies.get(position).getId();
String title = movies.get(position).getTitle();
String releaseDate = movies.get(position).getReleaseDate();
String overView = movies.get(position).getOverView();
String voteAverage = movies.get(position).getVoteAverage();
String popularity = movies.get(position).getPopularity();
String BigPoster = movies.get(position).getLargePoster();
String MinPoster = movies.get(position).getMinPoster();
Intent intent = new Intent(MainActivity.context, DetailedActivity.class);
intent.putExtra("id", id);
intent.putExtra("title", title);
intent.putExtra("releaseDate", releaseDate);
intent.putExtra("overView", overView);
intent.putExtra("voteAverage", voteAverage);
intent.putExtra("popularity", popularity);
intent.putExtra("MaxPoster", BigPoster);
intent.putExtra("MinPoster", MinPoster);
startActivity(intent);
public class FetchDataFromDb extends AsyncTask<String, Void, Boolean>
@Override
protected Boolean doInBackground(String... params)
dbOpenHelper = new DbOpenHelper(MainActivity.context);
movies.clear();
movies = dbOpenHelper.getAllFavoritePosters();
Log.v("database count", Integer.toString(FavList.size()));
return null;
@Override
protected void onPostExecute(Boolean aBoolean)
super.onPostExecute(aBoolean);
Log.v("database status", "Finished From Db");
adapter.notifyDataSetChanged();
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
PassDataToDetailedActivity(position);
);
【问题讨论】:
您的意思是说您的详细活动正在打开,但该活动的某些 UI 没有显示。? 是的,但只有当我从 FetchDataFromDb 启动它时才会发生这种情况,从 FetchDataFromApi 启动 Activity 效果很好。 您是否检查过您传递的数据实际上是用于该活动的? 是的,伙计,我在手动编码时调试了它们,但它们也没有出现 你能展示你的xml布局代码吗? 【参考方案1】:试试这个代码:
Intent in = new Intent(getActivity(), Opening_Activtiy_Name().class);
getActivity().startActivity(in);
注意:将您的 gridListener 带出该 post execute 方法
【讨论】:
它并没有真正帮助我怀疑这可能是因为片段附加的东西,但我调试并确保它不是问题。我真的被困在这个:(以上是关于从片段开始活动的主要内容,如果未能解决你的问题,请参考以下文章