gpt4 book ai didi

android - 将值传递给 ViewPager fragment

转载 作者:可可西里 更新时间:2023-11-01 11:42:45 25 4
gpt4 key购买 nike

我想将作为整数数组的 dogruYanlis 值传递给这些 fragment 。如果我尝试用 bundle 处理它,它会发送任何东西。我在 fragment 类中收到的数组始终为空。我认为我无法处理 bundle,但我不知道如何解决这个问题...

我要发送数据的Activity

package oztiryaki.my;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class result extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

private Bundle bundle;
int[] dogruYanlis;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);

toolbar = (Toolbar) findViewById(R.id.toolbarResult);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

Bundle list = getIntent().getExtras();
if(list == null){
return;
}
dogruYanlis = list.getIntArray("dogruYanlis");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

bundle = new Bundle();
bundle.putIntArray("dogruYanlis", dogruYanlis);

result2015Fragment f15 = new result2015Fragment();
f15.setArguments(bundle);

result2014Fragment f14 = new result2014Fragment();
f14.setArguments(bundle);

result2013Fragment f13 = new result2013Fragment();
f13.setArguments(bundle);

adapter.addFragment(f15, "2015");
adapter.addFragment(f14, "2014");
adapter.addFragment(f13, "2013");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

fragment Activity 之一

package oztiryaki.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
* Created by oztir on 21.02.2016.
*/
public class result2015Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.result2015,container,false);
int[] dogruYanlis = getArguments().getIntArray("dogruYanlis");
if(dogruYanlis == null){
Toast.makeText(getActivity(),"null sory:(", Toast.LENGTH_LONG).show();
}
TextView ygs1hp = (TextView) v.findViewById(R.id.ygs1hp);

return v;
}
public void getArray(int[] dogruYanlis){
Toast.makeText(getActivity(),Integer.toString(dogruYanlis[0]), Toast.LENGTH_LONG).show();
}
}

最佳答案

我有过类似的挣扎,也许这会有所帮助。如果 Activity 中的变量在创建后没有改变,有一种简单的方法可以从它创建的 fragment 中访问 Activity 的变量:

  1. 获取 fragment 中的上下文
  2. 然后你可以从 fragment 中获取变量的值

    Context context;

    public void onViewCreated(View v, Bundle savedInstanceState) {
    context = getActivity();
    int i = ((result)context).dogruYanlis[0];
    }

关于android - 将值传递给 ViewPager fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35588720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com