-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? –