-3
您好同事!如何將對象中的數據從一個活動傳遞到另一個活動android
我是新來的android和我正在開發一個應用程序。我被困在一個點上,我需要將活動A中的對象中包含的數據傳遞給活動B.我在下面粘貼我的代碼,請檢查它並讓我知道我是否正確或錯誤地提供了最佳做法它......我應該將數據傳遞給對象還是應該做其他事情? ...請建議。
代碼活動A
public MainListActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_list, container, false);
listView = (ListView) rootView.findViewById(R.id.listView_recipes);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.recipe_listview_progressBar);
if(isNetworkAvailable()) {
mProgressBar.setVisibility(View.VISIBLE);
GetRecipeData getRecipeData = new GetRecipeData();
getRecipeData.execute();
} else {
Toast.makeText(getContext(),getString(R.string.no_network), Toast.LENGTH_LONG).show();
}
//mRecipeTitles = new ArrayList<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object recipeData = mRecipeAdapter.getItem(position);
Intent intent = new Intent(getActivity(), RecipeDetailActivity.class).putExtra("data", (Serializable) recipeData);
startActivity(intent);
}
});
return rootView;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void handleRecipeData() {
mProgressBar.setVisibility(View.INVISIBLE);
if(mRecipeData == null){
handleErrors();
}else {
try {
JSONArray jsonPosts = mRecipeData.getJSONArray("posts");
//mRecipePostData = new String[jsonPosts.length()];
mRecipePostData = new ArrayList<>();
for (int i = 0; i < jsonPosts.length(); i++){
JSONObject post = jsonPosts.getJSONObject(i);
String title = post.getString(KEY_TITLE);
title = Html.fromHtml(title).toString();
String author = post.getJSONObject(KEY_AUTHOR).getString("name");
author = Html.fromHtml(author).toString();
HashMap<String, String> singleRecipePost = new HashMap<>();
singleRecipePost.put(KEY_TITLE, title);
singleRecipePost.put(KEY_AUTHOR, author);
mRecipePostData.add(singleRecipePost);
//mRecipePostData[i] = title;
}
String[] keys = {KEY_TITLE, KEY_AUTHOR};
int[] ids = {android.R.id.text1, android.R.id.text2};
mRecipeAdapter = new SimpleAdapter(getContext(), mRecipePostData, android.R.layout.simple_list_item_2, keys, ids);
listView.setAdapter(mRecipeAdapter);
mRecipeAdapter.notifyDataSetChanged();
} catch (JSONException e) {
Log.e(LOG_TAG,"Exception Caught: ",e);
}
}
}
代碼活動B
public class RecipeDetailActivityFragment extends Fragment {
public static final String LOG_TAG = RecipeDetailActivity.class.getSimpleName();
public static final String RECIPE_SHARE_HASHTAG = "#AmericanCuisines";
private String mRecipeDataStr;
public RecipeDetailActivityFragment() {
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = getActivity().getIntent();
View rootView = inflater.inflate(R.layout.fragment_recipe_detail, container, false);
if (intent != null && intent.hasExtra("data")){
mRecipeDataStr = intent.getSerializableExtra("data");
TextView details = (TextView) rootView.findViewById(R.id.recipe_detail_textView);
details.setText(mRecipeDataStr);
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.recipedetailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareRecipeIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
public Intent createShareRecipeIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mRecipeDataStr + " " + RECIPE_SHARE_HASHTAG);
return shareIntent;
}
}
我沒有讓我的問題是引用的問題的重複,因爲我試圖通過串行數據認爲一個數組通過活動和一個我在這樣做有問題...我的問題是,我是否做對了......我應該通過活動傳遞對象中包含的數據?因爲我會傳遞更多..像整個內容...圖像...等
的[我如何通過Android上的活動之間的數據?(可能的複製http://stackoverflow.com/questions/2091465/how-do-i-pass- data-between-activities-on-android) – Jas
我不認爲我的問題是參考問題的重複...請查看我的代碼... – FaISalBLiNK
您希望如何傳遞您的數據?通過點擊listView? –