我有在线数据库,我已存储"标题"对于每一页。 我正在使用Async Task从服务器下载数据,在日志语句中我能够看到所有标题字符串正在正确检索。 使用FragmentStatePagerAdapter在MainActivity中创建页面。 我能够成功创建页面,但我的所有页面都有相同的数据。
我的MainActivity.java:也有寻呼机适配器。
public class MainActivity extends AppCompatActivity {
ViewPager viewPager=null;
int numFrag1;
String title = "", getTitle= "";
public class TitleDownload extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... params) {
CloudQuery query = new CloudQuery("CollegeFeeds");
query.orderByAsc("updatedAt");
query.include("imageTitle");
try {
query.count(new CloudIntegerCallback() {
@Override
public void done(Integer integer, CloudException e) throws CloudException {
if(integer != null){
//int numFrag = integer;
noofFrag(integer);
//Log.i("no of fragments", String.valueOf(integer));
System.out.println("in background"+ integer);
}
}
});
} catch (CloudException e) {
e.printStackTrace();
}
try {
query.find(new CloudObjectArrayCallback() {
@Override
public void done(CloudObject[] cloudObjects, CloudException e) throws CloudException {
if(cloudObjects != null){
//Log.i("no of fragments", String.valueOf(cloudObjects.length));
for (int i=0; i < cloudObjects.length; i++){
// Log.i("no of fragments", String.valueOf(cloudObjects.length));
title = cloudObjects[i].get("imageTitle").toString();
titleName(title);
Log.i("title", title);
}
}
if( e != null){
e.printStackTrace();
}
}
});
} catch (CloudException e) {
e.printStackTrace();
}
return title ;
}
}
public void noofFrag(int data){
numFrag1 = data;
}
public void titleName(String titleData){
getTitle = titleData;
System.out.println("method title" + getTitle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloudApp.init("qrhjtmypohec", "6e22fd4d-eef9-49b5-87a9-fba0f35075f9");
new TitleDownload().execute();
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
//viewPager.setPageTransformer(true, );
//actionBar.setDisplayHomeAsUpEnabled(true);
}
class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
AttendanceResultFeed fragment = null;
for (int i=0; i<4; i++){
if(position == i){
fragment = new AttendanceResultFeed(position);
fragment.getTag();
//System.out.println("fragment tag" + fragment.getTag());
//fragment.getNumber(i);
Bundle bundle = new Bundle();
bundle.putInt("number", i);
bundle.putString("title", getTitle);
fragment.setArguments(bundle);
}
//return fragment;
new MyAdapter(getSupportFragmentManager()).notifyDataSetChanged();
}
return fragment;
}
@Override
public int getCount() {
// Log.d("info", "get count called");
//numFrag1--;
//Log.i("no of method(numFrag1) ", String.valueOf(numFrag1));
new MyAdapter(getSupportFragmentManager()).notifyDataSetChanged();
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0){
return "Attendance and Result";
}
if (position == 1){
return "College Feeds";
}
if (position == 2){
return "Branch Feeds";
}
return super.getPageTitle(position);
}
}
}
Fragment.java
public class AttendanceResultFeed extends Fragment {
TextView textView, titleText;
int number;
String text= "";
public AttendanceResultFeed(int position) {
Log.i("position", String.valueOf(position));
}
public void getNumber (int data){
number = data;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
// Log.d("info", "in attandec frag on create First time");
}else {
// Log.d("info", "in attandec frag on create subsequent time");
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.attendance_results_frag,container,false);
textView = (TextView) rootView.findViewById(R.id.textView);
titleText = (TextView )rootView.findViewById(R.id.titleText);
number = getArguments().getInt("number",0);
text = getArguments().getString("title");
Log.d("info", String.valueOf(number));
String num = String.valueOf(number);
titleText.setText(text);
textView.setText(num);
return rootView;
}
答案 0 :(得分:0)
实际上,您在以下函数中使用相同的标题设置片段的参数:
for (int i=0; i<4; i++){
if(position == i){
fragment = new AttendanceResultFeed(position);
fragment.getTag();
//System.out.println("fragment tag" + fragment.getTag());
//fragment.getNumber(i);
Bundle bundle = new Bundle();
bundle.putInt("number", i);
bundle.putString("title", getTitle);
fragment.setArguments(bundle);
}
正如您所看到的那样,您将相同的getTitle
放入片段参数中,因此实际上所有片段都会获得相同的标题。
我跟踪你的代码,看看getTitle
被分配到哪里,分配在titleName
函数中,在另一个for循环中调用,所以基本上getTitle
总是包含最后一个String分配给它。
我不确定您的代码是如何工作的,但这是一个提示:
使用ArrayList来保存你的标题:
//define this as a class member:
ArrayList<String> getTitle = new ArrayList<String>();
//change your titleName:
public void titleName(String titleData){
getTitle.add(titleData);
System.out.println("method title" + titleData);
}
//finally where you want to setArgument for your fragments use some logic to retrieve the titles, for example:
bundle.putString("title", getTitle.get(i));