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

时间:2015-03-14 23:40:40

标签: java android fragment

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

这是主要活动(起点):

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;

}

}

这是ResultsFragment:

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

现在我的问题是如何将在 onClick 方法(QueryFragment)中处理的结果(一个String)传递给ResultsFragment,其中String将在TextView中显示? / p>

提前致谢!

1 个答案:

答案 0 :(得分:0)

简单方法是:在父public static String中设置activity字段。 onClick方法会将info设置为此值,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;
}

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