我的代码有问题。这是我的MainActivity.java:
public class MainActivity extends FragmentActivity implements InterfaceTaskFinished {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
public boolean StatusConnection = false;
public int thread = 0;
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
}
public class FlightTask extends AsyncTask<String, Integer, Boolean> {
ProgressDialog prog;
boolean result;
private final InterfaceTaskFinished listener;
public FlightTask(InterfaceTaskFinished listener) {
this.listener = listener;
}
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Connessione in corso...");
prog.show();
prog.setCanceledOnTouchOutside(false);
prog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... params) {
try {
org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(10 * 1000).get();
// ARRIVI
org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();
while (iterator.hasNext()) {
thread++;
A_Compagnia[thread] = iterator.next().text();
...
}
}
}
}
}
我有Fragment
:
public class arrivi extends Fragment {
ListView list;
int thread = 0;
List<RowItem> rowItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View arrivi = inflater.inflate(R.layout.arrivi, container, false);
return arrivi;
}
public void CheckRow() {
MainActivity m = new MainActivity();
rowItems = new ArrayList<RowItem>();
for (int i = 1; i <= thread; i++) {
RowItem item = new RowItem(m.A_Compagnia[i], m.A_CodiceVolo[i], m.A_Citta[i], m.A_OraPrevista[i], m.A_OraStimata[i], m.A_StatoVolo[i]);
rowItems.add(item);
System.out.println("arrivi: " + m.A_Compagnia[i]);
}
list = (ListView) getView().findViewById(R.id.listView1);
CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
list.setAdapter(adapter);
}
}
但是有一个问题。当我运行我的应用并在AsyncTask
中执行MainActivity
时,一切正常。但我想在到达checkRow()
后onCreateView()
之后执行Fragment
。如果我在这里执行checkRow()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View arrivi = inflater.inflate(R.layout.arrivi, container, false);
CheckRow();
return arrivi;
}
我的应用崩溃是因为未创建View
,checkRow()
包含:
list=(ListView)getView().findViewById(R.id.listView1);
我该如何解决这个问题?
2。问题:
在我的MainActivity
我有12个阵列......我怎样才能将它们传递给到达Fragment
?
编辑:我在MainActivity.java中遇到了FragmentTransiction的问题:
public static arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo) {
arrivi fragment = new arrivi();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putStringArray("A_Compagnia", A_Compagnia);
bundle.putStringArray("A_CodiceVolo", A_CodiceVolo);
System.out.println("newistance eseguito");
fragment.setArguments(bundle);
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
return fragment;
}
@Override
public void onTaskFinished() {
newInstance(A_Compagnia,A_CodiceVolo);
}
无法从FragmentActivity类型中对非静态方法getSupportFragmentManager()进行静态引用
编辑2:
以下是我onCreate()
中MainActivity
方法的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container, Arrivi.newInstance(A_Compagnia,A_CodiceVolo));
transaction.commit();
}
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
}
};
actionBar.addTab(actionBar.newTab().setText("Info").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Arrivi").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Partenze").setTabListener(tabListener));
}
编辑3
有错误
我按照你的建议编辑了代码 MainActivity已编辑:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container, Arrivi.newInstance(A_Compagnia,A_CodiceVolo));
transaction.commit();
}
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
}
};
actionBar.addTab(actionBar.newTab().setText("Info").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Arrivi").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Partenze").setTabListener(tabListener));
new FlightTask(this).execute();
}
layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
arrivi.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000" />
</RelativeLayout>
TabPageAdapter.java:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new info();
case 1:
//Fragment for Ios Tab
return new Arrivi();
case 2:
//Fragment for Windows Tab
return new partenze();
}
return null;
}
@Override
public int getCount() {
return 3; //No of Tabs
}
}
NEW ARRIVI.JAVA
public class Arrivi extends Fragment{
ListView list;
int thread = 0;
List<RowItem> rowItems;
//public static String[] A_Compagnia;
//public static String[] A_CodiceVolo;
public static Arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo) {
Arrivi fragment = new Arrivi();
Bundle bundle = new Bundle();
bundle.putStringArray("A_Compagnia", A_Compagnia);
bundle.putStringArray("A_CodiceVolo", A_CodiceVolo);
Log.i("newistance eseguito", "TAG");
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
System.out.println(" LOL: ");
Bundle bundle = getArguments();
if(bundle != null) {
String[] dataA = bundle.getStringArray("A_Compagnia");
String[] dataB = bundle.getStringArray("A_CodiceVolo");
System.out.println(" ONVIEWCREATED: "+dataA);
}
if(bundle == null) System.out.println(" BUNDLE NULL");
// CheckRow();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View arrivi = inflater.inflate(R.layout.arrivi, container, false);
super.onViewCreated(arrivi, savedInstanceState);
return arrivi;
}
但是
if(bundle == null) System.out.println(" BUNDLE NULL");
是空的......
UPDATED2 ARRIVI.JAVA:
public class Arrivi extends Fragment{
ListView list;
int thread = 0;
List<RowItem> rowItems;
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
public String[] A_OraPrevista = new String[200];
public String[] A_OraStimata = new String[200];
public String[] A_StatoVolo = new String[200];
// PARTENZE
public String[] P_Compagnia = new String[200];
public String[] P_CodiceVolo = new String[200];
public String[] P_Citta = new String[200];
public String[] P_OraPrevista = new String[200];
public String[] P_OraStimata = new String[200];
public String[] P_StatoVolo = new String[200];
public static Arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo, String[] A_Citta, String[] A_OraPrevista, String[] A_OraStimata, String[] A_StatoVolo) {
this.A_Compagnia = A_Compagnia;
this.A_CodiceVolo = A_CodiceVolo;
this.A_Citta = A_Citta;
this.A_OraPrevista = A_OraPrevista;
this.A_OraStimata = A_OraStimata;
this.A_StatoVolo = A_StatoVolo;
this.P_Compagnia = P_Compagnia;
this.P_CodiceVolo = P_CodiceVolo;
this.P_Citta = P_Citta;
this.P_OraPrevista = P_OraPrevista;
this.P_OraStimata = P_OraStimata;
this.P_StatoVolo = P_StatoVolo;
Arrivi fragment = new Arrivi();
System.out.println("NEWINSTANCE : "+A_Compagnia[1]);
Log.i("newistance eseguito", "TAG");
return fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
System.out.println(" onviewCreated: ");
// String[] dataA = bundle.getStringArray("A_Compagnia");
// String[] dataB = bundle.getStringArray("A_CodiceVolo");
// System.out.println(" ONVIEWCREATED: "+dataA);
//}
//if(bundle == null) System.out.println(" BUNDLE NULL");
// CheckRow();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View arrivi = inflater.inflate(R.layout.arrivi, container, false);
super.onViewCreated(arrivi, savedInstanceState);
return arrivi;
}
主要活动中的开关/案例:
private class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new info();
case 1:
return Arrivi.newInstance(A_Compagnia, A_CodiceVolo, A_Citta, A_OraPrevista, A_OraStimata, A_StatoVolo);
case 2:
return new partenze();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
-------重新编辑--------- arrivi.java:
public class Arrivi extends Fragment{
ListView list;
int thread = 0;
List<RowItem> rowItems;
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
public String[] A_OraPrevista = new String[200];
public String[] A_OraStimata = new String[200];
public String[] A_StatoVolo = new String[200];
// PARTENZE
public String[] P_Compagnia = new String[200];
public String[] P_CodiceVolo = new String[200];
public String[] P_Citta = new String[200];
public String[] P_OraPrevista = new String[200];
public String[] P_OraStimata = new String[200];
public String[] P_StatoVolo = new String[200];
public static Arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo, String[] A_Citta, String[] A_OraPrevista, String[] A_OraStimata, String[] A_StatoVolo) {
System.out.println("NEWINSTANCE : "+A_Compagnia[1]);
Arrivi fragment = new Arrivi();
Bundle bundle = new Bundle();
bundle.putStringArray("A_Compagnia", A_Compagnia);
bundle.putStringArray("A_CodiceVolo", A_CodiceVolo);
bundle.putStringArray("A_Citta", A_Citta);
bundle.putStringArray("A_OraPrevista", A_OraPrevista);
bundle.putStringArray("A_OraStimata", A_OraStimata);
bundle.putStringArray("A_StatoVolo", A_StatoVolo);
fragment.setArguments(bundle);
// System.out.println("NEWINSTANCE : "+A_Compagnia[1]);
return fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
if(bundle != null) {
this.A_Compagnia = bundle.getStringArray("A_Compagnia");
this.A_CodiceVolo = bundle.getStringArray("A_CodiceVolo");
this.A_Citta = bundle.getStringArray("A_Citta");
this.A_OraPrevista = bundle.getStringArray("A_OraPrevista");
this.A_OraStimata = bundle.getStringArray("A_OraStimata");
this.A_StatoVolo = bundle.getStringArray("A_StatoVolo");
}
System.out.println(" onviewCreated: "+A_Compagnia[1]);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View arrivi = inflater.inflate(R.layout.arrivi, container, false);
super.onViewCreated(arrivi, savedInstanceState);
return arrivi;
}
MainActivity.java:
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new info();
case 1:
return Arrivi.newInstance(A_Compagnia, A_CodiceVolo, A_Citta, A_OraPrevista, A_OraStimata, A_StatoVolo);
case 2:
我从这里得到Null:
public static Arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo, String[] A_Citta, String[] A_OraPrevista, String[] A_OraStimata, String[] A_StatoVolo) {
System.out.println("NEWINSTANCE : "+A_Compagnia[1]);
MainActivity.java
public void GetValue(String[] A_Compagnia, String[] A_CodiceVolo, String[] A_Citta, String[] A_OraPrevista, String[] A_OraStimata, String[] A_StatoVolo,
String[] P_Compagnia, String[] P_CodiceVolo, String[] P_Citta, String[] P_OraPrevista, String[] P_OraStimata, String[] P_StatoVolo){
this.A_Compagnia = A_Compagnia;
this.A_CodiceVolo = A_CodiceVolo;
this.A_Citta = A_Citta;
this.A_OraPrevista = A_OraPrevista;
this.A_OraStimata = A_OraStimata;
this.A_StatoVolo = A_StatoVolo;
this.P_Compagnia = P_Compagnia;
this.P_CodiceVolo = P_CodiceVolo;
this.P_Citta = P_Citta;
this.P_OraPrevista = P_OraPrevista;
this.P_OraStimata = P_OraStimata;
this.P_StatoVolo = P_StatoVolo;
System.out.println("MAIN__ "+this.P_Compagnia[1]);
}
SplashScreen.java
@Override
protected void onPostExecute(Boolean result) {
if(!result) {
return;
}
if(listener != null) {
prog.dismiss();
listener.onTaskFinished();
}
}
}// FINE THREAD
@Override
public void onTaskFinished() {
MainActivity m = new MainActivity();
m.GetValue(A_Compagnia,A_CodiceVolo,A_Citta,A_OraPrevista,A_OraStimata,A_StatoVolo,
P_Compagnia,P_CodiceVolo,P_Citta,P_OraPrevista,P_OraStimata,P_StatoVolo);
}
谢谢
答案 0 :(得分:1)
关于你的问题:
有一个名为onViewCreated()
的生命周期方法,它在方法onCreateView()
完成后执行。您可以像这样覆盖它并使用它来执行checkRow()
:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
checkRow();
}
建议使用静态工厂方法(通常称为newInstance()
)来创建Fragment
个实例并将值传递给这些实例。您可以在Fragment
中定义这样的静态工厂方法,如下所示:
public static arrivi newInstance(String[] dataA, String[] dataB) {
arrivi fragment = new arrivi();
Bundle bundle = new Bundle();
bundle.putStringArray(arrivi.EXTRA_DATA_A, dataA);
bundle.putStringArray(arrivi.EXTRA_DATA_B, dataB);
fragment.setArguments(bundle);
return fragment;
}
您将数据添加到Bundle
并将其设置为新创建的Fragment
实例。在`Fragment中,你可以像这样检索这些数据:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
if(bundle != null) {
String[] dataA = bundle.getStringArray(EXTRA_DATA_A);
String[] dataB = bundle.getStringArray(EXTRA_DATA_B);
...
}
}
EXTRA_DATA_A
和EXTRA_DATA_B
是您在Fragment
中定义的常量,如下所示:
public static final String EXTRA_DATA_A = "dataA";
public static final String EXTRA_DATA_B = "dataB";
您通常只应使用这些常量作为键。
您的Arrivi
Fragment
应如下所示:
public class Arrivi extends Fragment {
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
public String[] A_OraPrevista = new String[200];
public String[] A_OraStimata = new String[200];
public String[] A_StatoVolo = new String[200];
public static Arrivi newInstance(String[] A_Compagnia, String[] A_CodiceVolo, String[] A_Citta, String[] A_OraPrevista, String[] A_OraStimata, String[] A_StatoVolo) {
Arrivi fragment = new Arrivi();
Bundle bundle = new Bundle();
bundle.putStringArray("A_Compagnia", A_Compagnia);
bundle.putStringArray("A_CodiceVolo", A_CodiceVolo);
bundle.putStringArray("A_Citta", A_Citta);
bundle.putStringArray("A_OraPrevista", A_OraPrevista);
bundle.putStringArray("A_OraStimata", A_OraStimata);
bundle.putStringArray("A_StatoVolo", A_StatoVolo);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
if(bundle != null) {
this.A_Compagnia = bundle.getStringArray("A_Compagnia");
this.A_CodiceVolo = bundle.getStringArray("A_CodiceVolo");
this.A_Citta = bundle.getStringArray("A_Citta");
this.A_OraPrevista = bundle.getStringArray("A_OraPrevista");
this.A_OraStimata = bundle.getStringArray("A_OraStimata");
this.A_StatoVolo = bundle.getStringArray("A_StatoVolo");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.arrivi, container, false);
return view;
}
}
您的SplashScreen
Activity
应如下所示:
public class SplashScreen extends Activity {
public int thread = 0;
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
public String[] A_OraPrevista = new String[200];
public String[] A_OraStimata = new String[200];
public String[] A_StatoVolo = new String[200];
// PARTENZE
public String[] P_Compagnia = new String[200];
public String[] P_CodiceVolo = new String[200];
public String[] P_Citta = new String[200];
public String[] P_OraPrevista = new String[200];
public String[] P_OraStimata = new String[200];
public String[] P_StatoVolo = new String[200];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashscreen);
new FlightTask(this).execute();
}
public class FlightTask extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog prog;
private boolean result;
private final WeakReference<Context> contextReference;
public FlightTask(Context context) {
this.contextReference = new WeakReference<Context>(context);
}
@Override
protected void onPreExecute() {
prog = new ProgressDialog(SplashScreen.this);
prog.setMessage("Connessione in corso...");
prog.show();
prog.setCanceledOnTouchOutside(false);
prog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... params) {
try {
org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(10*1000).get();
// ARRIVI
org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();
while(iterator.hasNext()){
thread++;
A_Compagnia[thread] = iterator.next().text();
A_CodiceVolo[thread] = iterator.next().text();
A_Citta[thread] = iterator.next().text();
A_OraPrevista[thread] = iterator.next().text();
A_OraStimata[thread] = iterator.next().text();
A_StatoVolo[thread] = iterator.next().text();
System.out.println("THREAD ARRIVI: "+A_StatoVolo[thread]);
}
// PARTENZE
org.jsoup.nodes.Element tabella2 = doc.getElementsByClass("tabella-voli").get(1);
Iterator<org.jsoup.nodes.Element> iterator2 = tabella2.select("td").iterator();
thread = 0;
while(iterator2.hasNext()){
thread++;
P_Compagnia[thread] = iterator2.next().text();
P_CodiceVolo[thread] = iterator2.next().text();
P_Citta[thread] = iterator2.next().text();
P_OraPrevista[thread] = iterator2.next().text();
P_OraStimata[thread] = iterator2.next().text();
P_StatoVolo[thread] = iterator2.next().text();
System.out.println("THREAD PARTENZE: "+P_Citta[thread]);
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(!result) {
return;
}
Context context = this.contextReference.get();
if(context != null) {
prog.dismiss();
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("A_Compagnia", A_Compagnia);
intent.putExtra("A_CodiceVolo", A_CodiceVolo);
intent.putExtra("A_Citta", A_Citta);
intent.putExtra("A_OraPrevista", A_OraPrevista);
intent.putExtra("A_OraStimata", A_OraStimata);
intent.putExtra("A_StatoVolo", A_StatoVolo);
intent.putExtra("P_Compagnia", P_Compagnia);
intent.putExtra("P_CodiceVolo", P_CodiceVolo);
intent.putExtra("P_Citta", P_Citta);
intent.putExtra("P_OraPrevista", P_OraPrevista);
intent.putExtra("P_OraStimata", P_OraStimata);
intent.putExtra("P_StatoVolo", P_StatoVolo);
context.startActivity(intent);
}
}
}
}
这就是你的MainActivity
应该是这样的:
public class MainActivity extends Activity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
//ARRIVI
public String[] A_Compagnia = new String[200];
public String[] A_CodiceVolo = new String[200];
public String[] A_Citta = new String[200];
public String[] A_OraPrevista = new String[200];
public String[] A_OraStimata = new String[200];
public String[] A_StatoVolo = new String[200];
// PARTENZE
public String[] P_Compagnia = new String[200];
public String[] P_CodiceVolo = new String[200];
public String[] P_Citta = new String[200];
public String[] P_OraPrevista = new String[200];
public String[] P_OraStimata = new String[200];
public String[] P_StatoVolo = new String[200];
public String[] A = {"ciao","come"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
this.A_Compagnia = intent.getStringArrayExtra("A_Compagnia");
this.A_CodiceVolo = intent.getStringArrayExtra("A_CodiceVolo");
this.A_Citta = intent.getStringArrayExtra("A_Citta");
this.A_OraPrevista = intent.getStringArrayExtra("A_OraPrevista");
this.A_OraStimata = intent.getStringArrayExtra("A_OraStimata");
this.A_StatoVolo = intent.getStringArrayExtra("A_StatoVolo");
this.P_Compagnia = intent.getStringArrayExtra("P_Compagnia");
this.P_CodiceVolo = intent.getStringArrayExtra("P_CodiceVolo");
this.P_Citta = intent.getStringArrayExtra("P_Citta");
this.P_OraPrevista = intent.getStringArrayExtra("P_OraPrevista");
this.P_OraStimata = intent.getStringArrayExtra("P_OraStimata");
this.P_StatoVolo = intent.getStringArrayExtra("P_StatoVolo");
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab arg0,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(android.app.ActionBar.Tab arg0,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab arg0,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
};
actionBar.addTab(actionBar.newTab().setText("Info").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Arrivi").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Partenze").setTabListener(tabListener));
// new FlightTask(this).execute();
System.out.println(A_Compagnia[1]);
if (!isOnline()){
try {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Connessione Internet non disponibile.");
alertDialog.setButton("Esci", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
catch(Exception e) { }
}
}
private class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new info();
case 1:
return Arrivi.newInstance(A, A_Compagnia, A_CodiceVolo, A_Citta, A_OraPrevista, A_OraStimata, A_StatoVolo);
case 2:
return new partenze();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
}
答案 1 :(得分:0)
关于你的第一个问题,你可以试试吗?
list=(ListView)getActivity().findViewById(R.id.listView1);
关于第二个问题你能尝试这样吗?
MusicAlbumList fragment = new MusicAlbumList();
Bundle args = new Bundle();
String[] abc;
args.putStringArray("some string",abc);
fragment.setArguments(args);