2014-10-20 159 views
0

我已經用盡了所有的搜索。本質上,我試圖完成的是將我的FragmentActivity中的字符串傳遞給ListFragment。下面是我的代碼:將字符串從FragmentActivity傳遞到ListFragment

MyActivity.java

package com.application.smst.smstapp; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.app.ProgressDialog; 
import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.text.util.Linkify; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.ScrollView; 
import android.widget.TextView; 
import android.widget.Toast; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.ParseException; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.support.v4.widget.SwipeRefreshLayout; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 

import com.application.smst.smstapp.adapter.TabsPagerAdapter; 




public class MyActivity extends FragmentActivity implements ActionBar.TabListener { 

private ViewPager viewPager; 
private ActionBar actionBar; 
// Tab titles 




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

    String[] tabs = { "LOA", "OT", "My "+getIntent().getExtras().getString("result_mover") }; 
    //////////////////////////////////////////////////// 
    String message = getIntent().getExtras().getString("result_mover"); 
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 

    //////////////////////////////////////////////////// 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getActionBar(); 
    TabsPagerAdapter mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

    viewPager.setAdapter(mAdapter); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Adding Tabs 
    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 
    /** 
    * on swiping the viewpager make respective tab selected 
    * */ 
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      // on changing the page 
      // make respected tab selected 
      actionBar.setSelectedNavigationItem(position); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
     } 
    }); 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
    // on tab selected 
    // show respected fragment view 
    viewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 
} 


} 

我試圖傳遞變量是消息字符串

String message = getIntent().getExtras().getString("result_mover"); 

的數據是從另一個活動傳遞成功。但是現在我必須將它提供給我的listfragment,它是上述片段活動所包含的三個選項卡(片段)之一。下面是listfragment代碼:

schfragment.java

package com.application.smst.smstapp; 


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

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

/** 
* Created by Admin on 10/5/2014. 
*/ 
public class schfragment extends ListFragment { 
String[] menuweekday; 
String[] menudate; 
String[] menustart; 
String[] menuend; 
CustomAdapter adapter; 
private List<RowItem> rowItems; 

@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ 


    //////////////////////////////////////// 
    return inflater.inflate(R.layout.fragment_sch, container, false); 



} 
@Override 
public void onActivityCreated(Bundle savedInstanceState){ 
super.onActivityCreated(savedInstanceState); 
menuweekday = getResources().getStringArray(R.array.weekdays); 
menudate = getResources().getStringArray(R.array.dates); 
menustart = getResources().getStringArray(R.array.starts); 
menuend = getResources().getStringArray(R.array.ends); 

rowItems = new ArrayList<RowItem>(); 

for (int i = 0; i< menuweekday.length;i++){ 
    RowItem items = new RowItem(menuweekday[i],menudate[i],menustart[i],menuend[i]); 
    rowItems.add(items); 
} 
adapter = new CustomAdapter(getActivity(),rowItems); 
    setListAdapter(adapter); 

} 

} 

任何幫助,將不勝感激。

我試過使用Bundles,這會導致NPE。我不知道在哪裏把代碼正確

謝謝你提前。並讓我知道如果我需要發佈我的tabspager適配器

回答

0

我不知道這是否會有任何幫助,因爲您已經嘗試過捆綁,但我會發布我的工作代碼。 這是我的片段中使用的一些代碼:

private static final String KEY_RESULT = "result"; 

public static MyFragment newInstance(String result) { 
    MyFragment myFragment = new MyFragment(); 

    Bundle bundle = new Bundle(); 
    bundle.putString(KEY_RESULT, result); 
    myFragment.setArguments(bundle); 

    return myFragment; 
} 

而當你需要你傳遞給片段中的字符串,您使用此代碼:

String result = this.getArguments().getString(KEY_RESULT, ""); 

最後,這是代碼,我用它來創建一個片段。我在我的活動中執行此操作,但您也可以在適配器中執行此操作。只要你有字符串'結果'那裏。

List<Fragment> fragments = new ArrayList<Fragment>(); 
fragments.add(MyFragment.newInstance(result));