【问题标题】:Pass arrayList from one fragment to another将 arrayList 从一个片段传递到另一个片段
【发布时间】:2014-07-09 06:07:33
【问题描述】:

我想将一个 Arraylist(SearchModl 是一个类)从一个片段传递到另一个片段。我正在使用 Bundle 来执行此操作。但是,当我在其他片段中获取捆绑包时,我得到的是空值

搜索模型

public class SearchModel implements Serializable {

    private String code,category, minExp, maxExp, postedOn, counts, applied, position, desc, type, hour, status, expiryDate, address, gender, religion, summary, requestId, requestorId;

    public SearchModel(String code,String category, String minExp, String maxExp, String postedOn, String counts, String applied, String position, String desc, String type, String hour, String status, String expiryDate, String address, String gender, String religion, String summary, String requestId, String requestorId) {
        this.code=code;
        this.category = category;
        this.minExp = minExp;
        this.maxExp = maxExp;
        this.postedOn = postedOn;
        this.counts = counts;
        this.applied = applied;
        this.position = position;
        this.desc = desc;
        this.type = type;
        this.hour = hour;
        this.status = status;
        this.expiryDate = expiryDate;
        this.address = address;
        this.gender = gender;
        this.religion = religion;
        this.summary=summary;
        this.requestId = requestId;
        this.requestorId = requestorId;
    } 

片段 A

for (int i = 0; i < tableArray.length(); i++) {
                                        JSONObject table = tableArray.getJSONObject(i);
                                        data = new SearchModel(table.getString("Job_Code"), table.getString("Job_Category"), table.getString("Min_Exp"), table.getString("Max_Exp"), table.getString("Posted_On"), table.getString("Candidate_Counts"), table.getString("Applications"), table.getString("No_Of_Pos"), table.getString("Job_Desc"), table.getString("Job_Type"), table.getString("Job_Hours"), table.getString("Job_Status"), table.getString("Job_Exp_Date"), table.getString("Address"), table.getString("Gender_Name"), table.getString("Religion_Name"), table.getString("Exp_Summary"), table.getString("IJob_Request_ID"), table.getString("Requestor_Name"));
                                        values.add(data);

                                    }

//                                    getArrayList.getArray(values);


                                    bundle.putSerializable("array", (java.io.Serializable) values);

                                }
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }
                        CommonFunctions.showProgress(getActivity(), "Please Wait...", false);
                        fragmentTransaction = getFragmentManager().beginTransaction();
                        SearchJobList searchJobList = new SearchJobList();
                        searchJobList.setArguments(bundle);
                        fragmentTransaction.replace(R.id.header_container, searchJobList, "searchJob").commit();
                    }
                }); 

第二个片段

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        data = new ArrayList<SearchModel>();
        Bundle bundle = getArguments();
        data = (List<SearchModel>) bundle.getSerializable("array");
        getArrayList = new GetArrayList() {
            @Override
            public void getArray(List<SearchModel> listValues) {
                data = listValues;
                jobAdapter = new SearchJobAdapter(getActivity(), data);
                setListAdapter(jobAdapter);
            }
        };

    } 

【问题讨论】:

  • 这取决于你的逻辑。您可以在您的活动类中创建模型类对象的一种方法。在 Activity 类中放置两个函数用于设置和获取数据。在片段 A 中填充数据。在片段 B 中,您可以调用 get 方法获取数据。另一种是通过communicator方式。

标签: android android-fragments


【解决方案1】:

您可以简单地将 ArrayList 保留在 Activity 中,供两个 Fragment 使用。

动态地,您在片段中创建一个公共方法,并在创建片段后,通过 setter 传递 List。

 YourActivity extends Activity {

   void createFragment(){
        YourFragment yourFragment = Fragment.getInstance();
        yourFragment.setList(models);
  }

}

YourFragment extends Fragment {
  private List<SearchModel> models; 

  public void setList(List<SearchModel> models){
        models = models;
  }
}

静态地,也不是最好的解决方案:

YourActivity extends Activity {
    static ArrayList<SearchModel> models;


}

YourFragment extends Fragment {

  public void doSomething(){
        List<SearchModel> model = YourActivity.models;
  }
}

实施 PArcelable 将是最好的解决方案:

http://developer.android.com/reference/android/os/Parcelable.html 这里有一个解决方案:Android ArrayList<MyObject> pass as parcelable

【讨论】:

  • 我已经编辑了我的答案,让你看看可能性。我希望很清楚:)
  • 和第一个一样。如果你想保持两个列表一致,那么你不需要在第一个片段中创建一个本地列表,只需在你的 Activity 中从两个片段中维护一个。因此,例如在添加时,您将执行 YourActivity.models.add(Object)。如果您只想将列表发送到片段而不保持它们的一致性,您只需将它们作为可打包对象的数组传递给 Bundle。
  • 您确定列表本身不为空吗?你之前没有实现 Serializable 吗?
  • @vandus 如何使用简单的activity从资产文件夹中检索列表
  • 通过什么方式将列表存储在 assets 文件夹中?
【解决方案2】:

1.实现一个单例类作为传递对象的例子。 (希望你知道什么是单例,我不知道,添加评论告诉我。

class ExampleClass {
    private ExampleClass () {}

    static ExampleClass obj = nil;
    public ExampleClass instance() {
         if (obj == nil) obj = new ExampleClass ();
         return obj;
    }

    public ArrayList<SearchModel> cache;
 }

2.在from活动中,

ExampleClass .instance().cache = Cus_Obje_arraylist;

3.然后在to活动中,可以从bridge类中获取。

ArrayList<SearchModel> Cus_Obje_arraylist = ExampleClass.instance().cache;

【讨论】:

    猜你喜欢
    • 2016-06-12
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多