![](/img/trans.png)
[英]Activity to Fragment sending data and access arraylist in Fragment
[英]Sending ArrayList from Activity to Fragment?
我是Android的新手,我正在尝试使用bundle和parcelable从数据库发送一些数据来显示recyclelerview和layouttab。 使用虚拟数据,我可以在2个选项卡中显示recyclerview。 但是当我尝试使用数据库中的数据时,我从活动中获取数据,我发送数据时遇到了一些麻烦。
我在帖子中尝试了许多具有相似性的解决方案。 但直到这个时候我才能得到正确的解决方案。
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private static final String URL = "http://myweb.com/api/getID.php?id=1";
private String[] ar_id, ar_stat;
private int arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout_id);
viewPager = (ViewPager) findViewById(R.id.viewpager_id);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
//Add Fragment
adapter.AddFragment(new FragmentActive(), "Active");
adapter.AddFragment(new FragmentHistory(), "History");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
getDataOrder();
}
private void getDataOrder(){
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray list = response.getJSONArray("result");
ArrayList<Active> arrayList = new ArrayList<Active>();
arr = list.length();
ar_id = new String[arr];
ar_stat = new String[arr];
for (int i = 0; i < arr; i++) {
JSONObject data = list.getJSONObject(i);
ar_id[i] = data.getString("id");
ar_stat[i] = data.getString("status");
arrayList.add(new Active(ar_id[i], ar_stat[i]));
}
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("ac_array", arrayList);
bundle.putString("test", "123");
FragmentActive fragmentActive = new FragmentActive();
fragmentActive.setArguments(bundle);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Fetching ERROR", "ERROR");
}
}
);
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
}
此外,这是对象类Active.java
public class Active implements Parcelable {
private String status;
public Active(){
}
public Active(Parcel in){
super();
readFromParcel(in);
}
public Active(String id, String status) {
this.id = id;
this.status = status;
}
public static final Parcelable.Creator<Active> CREATOR = new Parcelable.Creator<Active>() {
public Active createFromParcel(Parcel in) {
return new Active(in);
}
public Active[] newArray(int size) {
return new Active[size];
}
};
public void readFromParcel(Parcel in){
id = in.readString();
status = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(status);
}
public String getId() {
return id;
}
public String getStatus() {
return status;
}
public void setId(String id) {
this.id = id;
}
public void setStatus(String status) {
this.status = status;
}
}
这是片段类FragmentActive.java
public class FragmentActive extends Fragment {
View v;
private RecyclerView myrecyclerview;
private ArrayList<Active> lstActive;
//private ArrayList<Active> getArray;
public FragmentActive() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ArrayList<Active> activeArrayList = new ArrayList<Active>();
if (getArguments()!=null) {
activeArrayList = getArguments().getParcelableArrayList("ac_array");
String test = getArguments().getString("test");
Log.d("List", test);
}else{
Log.d("List", "Null?");
}
v = inflater.inflate(R.layout.active_fragment, container, false);
myrecyclerview = (RecyclerView) v.findViewById(R.id.active_recyclerview);
RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getContext(), ***activeArrayList***);
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Dummy Data
lstActive = new ArrayList<>();
lstActive.add(new Active("1", "1"));
lstActive.add(new Active("2", "0"));
lstActive.add(new Active("3", "1"));
}
}
当我运行应用程序时,根本没有错误,只是recyclelerview没有显示,但是当我将FragmentActive.onCreateView上的activeArrayList更改为虚拟ArrayList时,虚拟数据显示出来。
然后,我尝试打印从数据库获得的数据,它显示。 但是当我尝试从MainActivity打印arrayList时,这就是我从运行调试文本中得到的
D /调试清单:[com.xxx.xxx.app.Active@79e903c,com.xxx.xxx.app.Active @ 8dde1c5,com.xxx.xxx.app.Active@4a1571a]
I / System.out:[com.xxx.xxx.app.Active@79e903c,com.xxx.xxx.app.Active@8dde1c5,com.xxx.xxx.app.Active@4a1571a]
它们是数据库中的3行吗? 因为我选择的数据库显示3行。 我该怎么做才能获取,发送和显示数据?
D / Debug List:[com.xxx.xxx.app.Active@79e903c,com.xxx.xxx.app.Active @ 8dde1c5,com.xxx.xxx.app.Active@4a1571a]这是您的activeArrayList。 里面的值是列表中的对象。 你应该制作一个自定义的回收站视图适配器。 您使用的是一个字符串数组列表。 因此,制作一个recyclerviewadapter并以这种方式制作,以便显示所需的数据。
您创建片段但不将其添加到任何位置,只有在获得数据后才需要填充ViewPagerAdapter:
FragmentActive fragmentActive = new FragmentActive();
fragmentActive.setArguments(bundle);
adapter.addFragment(fragmentActive//
您添加到ViewPager的另一个是空的,如果您不使用相同的实例将是空的。
您需要在片段中使用一个方法来更新ArrayList<Active>
数据,这将更新Recycler视图的适配器数据。
这可能是
在片段中:
public void updateListData(ArrayList<Active> list){
recyclerAdapter.updateData(list);
}
在您的Recycler适配器中有updateData方法,如:
public void updateData(ArrayList<Active> list){
//assign list to your data in adapter
this.lists = list;
notifyDataSetChanged();
}
在onResponse中获取数据后,在FragmentActive上调用updateListData
。
将FragmentActive作为类级变量。
我通常使用这种应用程序的方法是在活动中填充ArrayList
(来自数据库),然后让Fragment
使用接口从活动中提取数据(或者将Fragment
的getActivity()
转换为类片段变得可见时(主要活动)(请参阅setUserVisibleHint()
)。
关于你的代码:
1)我看到你使用FragmentActive
和FragmentHistory
初始化viewpager,但此时它们没有参数。
2)在getDataOder()
您为FragmentActive
创建了一个参数,并创建了一个新的片段实例,但它似乎已丢失,因为没有其他任何操作。
3)另外,请记住, ViewPager
管理您添加到其中的Fragments
的生命周期,因此我认为如果您为ViewPager
使用的片段设置参数,可能会发生有线事情,因为最可能的是您创建的实例和设置参数,在某些时候,将被删除,并替换为缺少参数的新实例。 (我应该测试一下,但这是我相信会发生的事情)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.