如何将带有recyclelerview的一个活动中的json资产文件中的arraylist收集的数据传递给另一个具有回收者视图的活动? [英] How to pass data collected in arraylist from json asset file in one activity having recyclerview to another activity again having recycler view?

查看:122
本文介绍了如何将带有recyclelerview的一个活动中的json资产文件中的arraylist收集的数据传递给另一个具有回收者视图的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an activity named Cardview Activity which has a recycler view associated with it and i am fetching data from a JSON asset file in that. But now when i click on an item of Cardview Activity, I want to send data related to that item only to another activity i.e. People_List_Activity which is again having a 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<CategoryModel.CategoryList> 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<CategoryModel.CategoryList>();
        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<CategoryModel.CategoryList> filteredList = new ArrayList<CategoryModel.CategoryList>();

                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<CategoryModel.CategoryList> getmcategoryset() {

        try {

            ArrayList<CategoryModel.CategoryList>categoryList = new ArrayList<CategoryModel.CategoryList>();
            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<String> 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<String> 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<String> 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<CategoryModel.Persons> 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
                    ArrayList<String>Fax = 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<RecyclerViewAdapterMain.CategoryObjectHolder> {
    private static String LOG_TAG = "categoryRecyclrVwAdptr";
    private ArrayList<CategoryModel.CategoryList> 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<CategoryModel.CategoryList> 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<RecyclerViewAdapter_People.PeopleObjectHolder> {
    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 ArrayList<CategoryList>categoryList;

    public ArrayList<CategoryList> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(ArrayList<CategoryList> categoryList) {
        this.categoryList = categoryList;
    }

    public  static class CategoryList{

        private int Category_id;
        private String Category_name;
        private ArrayList<String>Emails;
        private ArrayList<String>Epabx;
        private ArrayList<String>Category_Fax;
        private ArrayList<Persons> persons;

        public CategoryList(int category_id, String category_name,
                            ArrayList<String> emails, ArrayList<String> epabx, ArrayList<String> category_Fax,
                            ArrayList<Persons> 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<String> getEmails() {
            return Emails;
        }

        public void setEmails(ArrayList<String> emails) {
            Emails = emails;
        }

        public ArrayList<String> getEpabx() {
            return Epabx;
        }

        public void setEpabx(ArrayList<String> epabx) {
            Epabx = epabx;
        }

        public ArrayList<String> getCategory_Fax() {
            return Category_Fax;
        }

        public void setCategory_Fax(ArrayList<String> category_Fax) {
            Category_Fax = category_Fax;
        }

        public ArrayList<Persons> getPersons() {
            return persons;
        }

        public void setPersons(ArrayList<Persons> 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 ArrayList<String>Fax;
        private String Ext;

        public Persons(int person_ID, String name, String designation, String office_Phone,
                       String residence_Phone, String VOIP, String address, ArrayList<String> 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<String> getFax() {
            return Fax;
        }

        public void setFax(ArrayList<String> fax) {
            Fax = fax;
        }

        public String getExt() {
            return Ext;
        }

        public void setExt(String ext) {
            Ext = ext;
        }
    }
}

我该如何发送 Person_Id 名称指定到People_List_Activity?请帮忙,因为我被困在代码中。

how can i send the Person_Id, Name and Designation to the People_List_Activity ? Please help as i am stuck in the code.

推荐答案

您可以使用发送意图,通过在Bundle中传递数据然后将Bundle传递到 Intent Extras 。当你进入一个活动到另一个。

You can send using Intent , by passing your data in Bundle and then pass Bundle into Intent Extras.When you go form one Activity to another .

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);

您可以在下一个活动中检索数据 OnCreate()

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

这篇关于如何将带有recyclelerview的一个活动中的json资产文件中的arraylist收集的数据传递给另一个具有回收者视图的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆