如何在 ViewPager 中将值从一个 Fragment 传递到另一个?

How to pass values from one Fragment to another in a ViewPager?

我对 Android 编程完全陌生。我正在写一个有两个片段的地方:QueryResults。查询片段中有一个 Button 和一个 EditText。当用户单击 Button 时,EditText 的结果应显示在 Results Fragment 中的 TextView 中。

这是主要的Activity(起点):

public class StartingPoint extends FragmentActivity
{
ViewPager viewPager;

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

    if (android.os.Build.VERSION.SDK_INT > 9) 
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    viewPager = (ViewPager)findViewById(R.id.pager);
    viewPager.setAdapter(new FragmentsPagerAdapter(getSupportFragmentManager()));
}
}

这里是 QueryFragment.java:

public class QueryFragment extends Fragment
{
EditText queryET, endpointET;
Button execute;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    // Get the view from fragmenttab1.xml
    View view = inflater.inflate(R.layout.query_fragment, container, false);

    queryET = (EditText) view.findViewById(R.id.sparqlQueryET);
    endpointET = (EditText) view.findViewById(R.id.sparqlEndpointET);

    execute = (Button) view.findViewById(R.id.executeButton);
    execute.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            String query = queryET.toString();
            String endpoint = endpointET.toString();

            Query q = QueryFactory.create(query, Syntax.syntaxARQ);
            q.setOffset(1);
            QueryExecution qe = QueryExecutionFactory.sparqlService(endpoint, query);
            ResultSet rs = qe.execSelect();
            StringBuffer results = new StringBuffer();
            List<String> columnNames = rs.getResultVars();

            while(rs.hasNext())
            {
                QuerySolution qs = rs.next();
                for(String var: columnNames)
                {
                    results.append(var +":");
                    if(qs.get(var) == null)
                        results.append("{null}");
                    else if (qs.get(var).isLiteral())
                        results.append(qs.getLiteral(var).toString());
                    else
                        results.append(qs.getResource(var).getURI());
                    results.append('\n');
                }
                results.append("----------------\n");
            }
            qe.close();
        }
    });

    return view;

}

}

这是结果片段:

public class ResultsFragment extends Fragment
{
TextView r;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    // Get the view from fragmenttab1.xml
    View view = inflater.inflate(R.layout.results_fragment, container, false);

    r = (TextView) view.findViewById(R.id.resultsTV);

    return view;
}
}

现在我的问题是,如何将在(QueryFragment 的)onClick 方法中处理的结果(一个字符串)传递到 ResultsFragment,其中字符串将显示在 TextView 中?

简单的方法是:在父 activity 中创建 public static String 字段。 onClick 方法会将信息设置到这个值中,onCreateView 结果片段会尝试从那里获取它。

onClick(View v){
    MainActivity.myResultString = "result code";
}

并在需要时获取;

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

    View view = inflater.inflate(R.layout.results_fragment, container, false);

    r = (TextView) view.findViewById(R.id.resultsTV);
    r.setText(MainActivity.myResultString);
    return view;
}

像这样。它没有完成解决方案,但可以帮助你)祝你好运。