简体   繁体   中英

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

I'm completely new to Android programming. I'm writing an where there are two Fragments: Query and Results . There is a Button and an EditText in the Query Fragment. When the user clicks on the Button, the results from the EditText should be displayed in the TextView that is present the Results Fragment.

Here is the Main Activity (Starting Point):

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

Here is the 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;

}

}

Here is the 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;
}
}

Now my question is how do I pass the result processed in the onClick method (of the QueryFragment), which is a String, to the ResultsFragment where the String would be displayed in a TextView?

Simple approach will be : Make public static String field in parent activity . onClick method will set info into this value, and onCreateView result fragment will try to take it from there.

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

and get it when you need it;

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

Something like this. Its not completed solution but can help you ) good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM