如何将带有recyclelerview的一个活动中的json资产文件中的arraylist收集的数据传递给另一个具有回收者视图的活动?

我有一个名为Cardview Activity ,它有一个与之关联的回收站视图,我从中获取JSON资产文件中的数据。 但是现在当我点击Cardview活动的项目时,我想将与该项目相关的数据仅发送到另一个活动,即People_List_Activity ,它再次具有recyclerView。

CardViewActivity.java

 package com.example.android.directory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; public class CardViewActivity extends AppCompatActivity { Toolbar mActionBarToolbar; private RecyclerView mainRecyclerView; private RecyclerView.Adapter mainAdapter; private RecyclerView.LayoutManager mainLayoutManager; private static String LOG_TAG = "CardViewActivity"; EditText inputSearchMain; private ArrayList categoryLists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_card_view); mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar); mActionBarToolbar.setTitle("Telephone Directory"); mActionBarToolbar.setLogo(R.drawable.toolbar_logo); mActionBarToolbar.setTitleMargin(5,2,2,2); setSupportActionBar(mActionBarToolbar); categoryLists=new ArrayList(); categoryLists.addAll(getmcategoryset()); mainRecyclerView=(RecyclerView)findViewById(R.id.recyclerView_Main); mainRecyclerView.setHasFixedSize(true); mainLayoutManager=new LinearLayoutManager(this); mainRecyclerView.setLayoutManager(mainLayoutManager); mainAdapter=new RecyclerViewAdapterMain(getmcategoryset()); mainRecyclerView.setAdapter(mainAdapter); inputSearchMain = (EditText) findViewById(R.id.inputSearchMain); addTextListener(); } public void addTextListener(){ inputSearchMain.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence query, int start, int before, int count) { query = query.toString().toLowerCase(); final ArrayList filteredList = new ArrayList(); for (int i = 0; i < categoryLists.size(); i++) { final String text = categoryLists.get(i).getCategory_name().toLowerCase(); if (text.contains(query)) { filteredList.add(categoryLists.get(i)); } } mainRecyclerView.setLayoutManager(new LinearLayoutManager(CardViewActivity.this)); mainAdapter = new RecyclerViewAdapterMain(filteredList); mainRecyclerView.setAdapter(mainAdapter); mainAdapter.notifyDataSetChanged(); // data set changed } }); } private ArrayList getmcategoryset() { try { ArrayListcategoryList = new ArrayList(); JSONObject jsonObject = new JSONObject(readJSONFromAsset()); JSONArray categoryArray = jsonObject.getJSONArray("Category"); Log.d("getmcategoryset", "category count: "+categoryArray.length()); for (int i = 0; i < categoryArray.length(); i++) { JSONObject job = categoryArray.getJSONObject(i); int categoryId = job.getInt("Category_id"); String categoryName = job.getString("Category_name"); //this is for email array ArrayList emails = new ArrayList(); JSONArray emailArray = job.getJSONArray("Emails"); for (int j = 0; j< emailArray.length(); j++){ emails.add(emailArray.getString(j)); } //This i for Epabx array ArrayList epabx = new ArrayList(); JSONArray epabxArray = job.getJSONArray("Epabx"); for (int j = 0; j < epabxArray.length(); j++){ epabx.add(epabxArray.getString(j)); } //This i for Category_Fax array ArrayList category_Fax = new ArrayList(); JSONArray category_FaxJson = job.getJSONArray("Category_Fax"); for (int j = 0; j < category_FaxJson.length(); j++){ category_Fax.add(category_FaxJson.getString(j)); } //This i for Persons array ArrayList personsList = new ArrayList(); JSONArray personsArray = job.getJSONArray("Persons"); for (int j = 0; j < personsArray.length(); j++){ JSONObject jobIn = personsArray.getJSONObject(j); int Person_ID = jobIn.getInt("Person_ID"); String Name = jobIn.getString("Name"); String Designation = jobIn.getString("Designation"); String Office_Phone = jobIn.getString("Office_Phone"); String Residence_Phone = jobIn.getString("Residence_Phone"); String VOIP = jobIn.getString("VOIP"); String Address = jobIn.getString("Address"); //this is for Fax array ArrayListFax = new ArrayList(); JSONArray fax = jobIn.getJSONArray("Fax"); for (int k=0; k < fax.length(); k++) { // JSONObject jobI = fax.getString(k); Fax.add(fax.getString(k)); } String Ext = jobIn.getString("Ext"); personsList.add(new CategoryModel.Persons(Person_ID, Name, Designation, Office_Phone, Residence_Phone, VOIP, Address, Fax, Ext)); } //here your Category[] value store in categoryArrayList categoryList.add(new CategoryModel.CategoryList(categoryId, categoryName, emails, epabx, category_Fax, personsList)); Log.i("categoryList size = ", ""+categoryArray.length()); } if (categoryList != null) { Log.i("categoryList size = ", ""+categoryArray.length()); } return categoryList; } catch (JSONException e) { e.printStackTrace(); return null; } } @Override protected void onResume() { super.onResume(); } public String readJSONFromAsset() { String json = null; try { InputStream is = getAssets().open("DirectoryData.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } } 

RecyclerViewAdapterMain

 package com.example.android.directory; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.Arrays; /** * Created by Android on 3/17/2017. */ public class RecyclerViewAdapterMain extends RecyclerView.Adapter { private static String LOG_TAG = "categoryRecyclrVwAdptr"; private ArrayList mcategoryset; private static CategoryClickListener categoryClickListener; public static class CategoryObjectHolder extends RecyclerView.ViewHolder { TextView category_name; public CategoryObjectHolder(View itemView){ super(itemView); category_name=(TextView)itemView.findViewById(R.id.category_name); /*category_emails=(TextView)itemView.findViewById(R.id.category_emails); category_epabx=(TextView)itemView.findViewById(R.id.category_epabx); category_fax=(TextView)itemView.findViewById(R.id.category_fax);*/ Log.i(LOG_TAG, "Adding Listener"); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(CardViewActivity.this,PeopleListActivity.class); } }); } public RecyclerViewAdapterMain(ArrayList myDataset) { mcategoryset = myDataset; } public CategoryObjectHolder onCreateViewHolder(ViewGroup parent,int viewType){ View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row_main_activity,parent,false); CategoryObjectHolder categoryObjectHolder=new CategoryObjectHolder(view); return categoryObjectHolder; } @Override public void onBindViewHolder(CategoryObjectHolder holder, int position) { holder.category_name.setText(mcategoryset.get(position).getCategory_name()); } public void addItem(CategoryModel.CategoryList dataObj, int index) { mcategoryset.add(index, dataObj); notifyItemInserted(index); } public void deleteItem(int index) { mcategoryset.remove(index); notifyItemRemoved(index); } @Override public int getItemCount() { return mcategoryset.size(); } public interface CategoryClickListener { public void onItemClick(int position, View v); } } 

People_List_Activity.java

 package com.example.android.directory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; public class PeopleListActivity extends AppCompatActivity { Toolbar mActionBarToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_people_list); mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar); mActionBarToolbar.setTitle("Staff List"); mActionBarToolbar.setLogo(R.drawable.toolbar_logo); mActionBarToolbar.setTitleMargin(5,2,2,2); setSupportActionBar(mActionBarToolbar); } } 

RecyclerViewAdapter_People.java

 package com.example.android.directory; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by Android on 3/20/2017. */ public class RecyclerViewAdapter_People extends RecyclerView.Adapter { private static String TAG = "peopleRecyclrVwAdptr"; public static class PeopleObjectHolder extends RecyclerView.ViewHolder{ TextView peopleName,peopleDesignation; public PeopleObjectHolder(View itemView) { super(itemView); peopleName=(TextView)itemView.findViewById(R.id.people_name); peopleDesignation=(TextView)itemView.findViewById(R.id.people_designation); Log.d(TAG, "PeopleObjectHolder: "); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } } @Override public PeopleObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_people_list_row,parent,false); PeopleObjectHolder peopleObjectHolder=new PeopleObjectHolder(view); return peopleObjectHolder; } @Override public void onBindViewHolder(RecyclerViewAdapter_People.PeopleObjectHolder holder, int position) { } @Override public int getItemCount() { return 0; } } 

CategoryModel.java

 package com.example.android.directory; import java.util.ArrayList; /** * Created by Android on 3/17/2017. */ public class CategoryModel { private ArrayListcategoryList; public ArrayList getCategoryList() { return categoryList; } public void setCategoryList(ArrayList categoryList) { this.categoryList = categoryList; } public static class CategoryList{ private int Category_id; private String Category_name; private ArrayListEmails; private ArrayListEpabx; private ArrayListCategory_Fax; private ArrayList persons; public CategoryList(int category_id, String category_name, ArrayList emails, ArrayList epabx, ArrayList category_Fax, ArrayList persons) { Category_id = category_id; Category_name = category_name; Emails = emails; Epabx = epabx; Category_Fax = category_Fax; this.persons = persons; } public int getCategory_id() { return Category_id; } public void setCategory_id(int category_id) { Category_id = category_id; } public String getCategory_name() { return Category_name; } public void setCategory_name(String category_name) { Category_name = category_name; } public ArrayList getEmails() { return Emails; } public void setEmails(ArrayList emails) { Emails = emails; } public ArrayList getEpabx() { return Epabx; } public void setEpabx(ArrayList epabx) { Epabx = epabx; } public ArrayList getCategory_Fax() { return Category_Fax; } public void setCategory_Fax(ArrayList category_Fax) { Category_Fax = category_Fax; } public ArrayList getPersons() { return persons; } public void setPersons(ArrayList persons) { this.persons = persons; } } public static class Persons{ private int Person_ID; private String Name; private String Designation; private String Office_Phone; private String Residence_Phone; private String VOIP; private String Address; private ArrayListFax; private String Ext; public Persons(int person_ID, String name, String designation, String office_Phone, String residence_Phone, String VOIP, String address, ArrayList fax, String ext) { Person_ID = person_ID; Name = name; Designation = designation; Office_Phone = office_Phone; Residence_Phone = residence_Phone; this.VOIP = VOIP; Address = address; Fax = fax; Ext = ext; } public int getPerson_ID() { return Person_ID; } public void setPerson_ID(int person_ID) { Person_ID = person_ID; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getDesignation() { return Designation; } public void setDesignation(String designation) { Designation = designation; } public String getOffice_Phone() { return Office_Phone; } public void setOffice_Phone(String office_Phone) { Office_Phone = office_Phone; } public String getResidence_Phone() { return Residence_Phone; } public void setResidence_Phone(String residence_Phone) { Residence_Phone = residence_Phone; } public String getVOIP() { return VOIP; } public void setVOIP(String VOIP) { this.VOIP = VOIP; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public ArrayList getFax() { return Fax; } public void setFax(ArrayList fax) { Fax = fax; } public String getExt() { return Ext; } public void setExt(String ext) { Ext = ext; } } } 

如何将Person_IdNameDesignation发送到People_List_Activity? 请帮忙,因为我卡在代码中。

您可以使用Intent发送,通过在Bundle中传递数据,然后将Bundle传递给Intent Extras 。当您将一个Activity为另一个Activity时。

 Intent intent=new Intent(CardViewActivity.this,PeopleListActivity.class); Bundle bundle = new Bundle(); bundle.putString("key1",value1); bundle.putString("key2",value2); bundle.putString("key3",value3); intent.putExtras(bundle) startActvity(intent); 

并且您可以在下一个Activity OnCreate()检索数据:

  Bundle bundle = getIntent().getExtras(); String value1 = bundle.getString("key1"); String value2 = bundle.getString("key2"); String value3 = bundle.getString("key3");