java正在将ArrayList从活动发送到片段?
我是Android新手,我正在尝试使用bundle和parcelable to fragment从数据库发送一些数据,以显示RecycleView和layouttab。使用虚拟数据,我可以在两个选项卡中显示recyclerview。但当我试图使用从数据库中获取的数据时,我遇到了一些麻烦,无法将获取的数据发送到片段
我在帖子中尝试了许多相似的解决方案。但是直到现在我还不能找到正确的解决方案
这是我的主要活动。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;
}
}
这是fragment类,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"));
}
}
当我运行应用程序时,没有任何错误,只是没有显示recyclerview,但是当我更改FragmentActive上的activeArrayList时。一旦创建视图到虚拟ArrayList,将显示虚拟数据
然后,我尝试打印从数据库中获取的数据,如图所示。但当我尝试从MainActivity打印arrayList时,这是我从run debug text获得的结果
D/Debug List: [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行。我应该如何获取、发送和显示数据
# 1 楼答案
我通常使用这种应用程序的方法是在活动中填充一个
ArrayList
(从数据库),然后让Fragment
使用接口从活动中提取数据(或者将Fragment
的getActivity()
强制转换到主活动的类),当片段变得可见时(请参见setUserVisibleHint()
)关于您的代码:
1)我看到您使用
FragmentActive
和FragmentHistory
初始化了viewpager,但此时它们没有参数2)在
getDataOder()
中,您创建了FragmentActive
的参数,并创建了片段的新实例,但它似乎丢失了,因为没有对它做任何其他操作3)另外,请记住
ViewPager
管理您添加到其中的Fragments
的生命周期,因此我认为,如果您为ViewPager
中使用的片段设置参数,则可能会发生有线情况,因为最有可能的情况是,您创建并设置参数的实例在某个时候将被删除,并替换为缺少参数的新实例。(我应该对此进行测试,但我相信会发生这种情况)# 2 楼答案
调试列表:[com.xxx.xxx.app。Active@79e903c,com。xxx。xxx。应用程序。Active@8dde1c5,com。xxx。xxx。应用程序。Active@4a1571a]这是你的activeArrayList。 其中的值是列表中的对象。 您可能应该制作一个自定义的回收器视图适配器。 您使用的是字符串数组列表。 因此,制作一个RecycleServiceAdapter,并以这样一种方式显示所需的数据
# 3 楼答案
创建片段但不将其添加到任何位置,只有在获得数据后才能填充ViewPagerAdapter:
添加到ViewPager的另一个实例为空,如果不使用相同的实例,则将为空
# 4 楼答案
您需要在fragment中有一个方法来更新
ArrayList<Active>
数据,这将 更新回收器视图的适配器数据这可能像
片段:
在您的回收器适配器中,有更新的数据方法,如:
在onResponse中获取数据后,调用
updateListData
on FragmentActive将FragmentActive作为类级别变量