在创建最后一个片段时收集RadioButton选择

时间:2013-11-18 11:18:38

标签: java android eclipse android-fragments android-fragmentactivity

我试图在第四个屏幕中使用第一个第二个和第三个屏幕的选定值。我将如何访问先前片段的无线电组?

我试图将所选值存储在每个片段的onPause方法中,但是当我从第一个片段滑动到第三个片段而不是从第一个片段滑动到第二个片段时,它会被调用。

我曾尝试使用一个用于radiogroup的监听器,但它永远不会被调用。

我试图从最终的Tsiatista片段中访问其他三个片段但没有任何成功。

package com.tsiatistonmyfriend;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import net.justanotherblog.swipeview.R;
import android.os.Bundle;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public static String funnySerious;

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

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);       
}

@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;
}

OnClickListener listener = new OnClickListener() 
{   
@Override
public void onClick(View v) 
{
RadioButton rb = (RadioButton) v;

funnySerious = (String) rb.getText();                   
}                
};  

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) 
{
super(fm);
}

@Override
public Fragment getItem(int position) 
{           
Fragment fragment = new FunnySerious();

switch (position) 
{
case 0:
return fragment = new FunnySerious();
case 1:
return fragment = new MaleFemale();
case 2:
return fragment = new DirtyClean();
case 3:
return fragment = new Tsiatisto();
default:
break;
}       

return fragment;
}

@Override
public int getCount() {
// Show 4 total pages.
return 4;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}


public static class FunnySerious extends Fragment 
{       
View v;

public FunnySerious() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.funny_serious, container, false);

v = rootView;

return rootView;
}           
}

public static class MaleFemale extends Fragment
{       
public MaleFemale() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
View rootView = inflater.inflate(R.layout.male_female, container, false);

return rootView;
}       
}

public static class DirtyClean extends Fragment
{       
public DirtyClean() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
View rootView = inflater.inflate(R.layout.dirty_clean, container, false);

return rootView;
}       
}

public static class Tsiatisto extends Fragment
{       
public Tsiatisto() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
//FunnySerious textFragment = (FunnySerious) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.pager);


View rootView = inflater.inflate(R.layout.tsiatisto, container, false);

DataBaseHelper myDbHelper = new DataBaseHelper(this.getActivity().getApplicationContext());         

try 
{
myDbHelper.createDataBase(); 
myDbHelper.openDataBase();
}catch(IOException ioe) 
{ 
throw new Error("Unable to create database"); 
}        

final SQLiteDatabase db = myDbHelper.getDB();       

Cursor cursor;
cursor =  db.query("Tsiatista ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
//cursor =  db.rawQuery("SELECT Verse FROM Tsiatista WHERE ID = 1", null);

if(cursor.moveToFirst())
{                   
String htr;
htr = (String) cursor.getString(cursor.getColumnIndex("Verse"));

EditText et = (EditText) rootView.findViewById(R.id.tsiatistoTxt);
et.setText(htr);

cursor.close();
}

return rootView;
}       
}
}

1 个答案:

答案 0 :(得分:0)

两种方式,

  1. 在片段之间切换时,使用setArgument方法将所选值作为键值形式的键值布尔对传递。然后,您可以访问此传递的包并使用先前片段中的选定值进一步驱动您的逻辑,然后您可以使用当前片段中的值来控制此值,并进一步导航。

  2. 了解共享首选项,您可以随时保存值并从整个应用程序访问它,因此使用此功能可以保存用户从前三个片段中选择的值,在第四个片段中检索它并完成逻辑。

  3. 希望有所帮助。