3

我对 Java 和 Android 编程非常陌生。我试图弄清楚如何将一组变量从一个活动通过另一个使用 setContentView 的活动传递给附加到扩展 ListFragment 的类的 xml 文件。基本上,我希望第一个活动的数据能够被第二个活动中访问的片段访问。到目前为止,我一直在尝试研究这个问题,所以如果答案已经存在,请原谅我。

我有一个名为 Login.java 的活动,它执行以下操作:

Intent intent = new Intent(this, ListActivity.class);
Bundle varBundle = new Bundle();
varBundle.putString("companyKey",companyKey);
varBundle.putString("techID",techID);
varBundle.putString("userID",userID);       
varBundle.putString("startDate",startDate);
intent.putExtras(varBundle);
startActivity(intent);

在 ListActivity.java 中,到目前为止我有这个:

public class ListActivity extends Activity 
{
public String companyKey;
public String techID;
public String userID;
public String startDate;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Bundle vars = getIntent().getExtras();

    companyKey = vars.getString("companyKey");
    techID = vars.getString("techID");
    userID = vars.getString("userID");
    startDate = vars.getString("startDate");

    setContentView(R.layout.fragmentlist);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list, menu);
    return true;
}

}

我将内容视图设置为 fragmentlist.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titles"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.technicianappointmentlist.OrderList" />

Fragmentlist.xml 绑定到 OrderList.java。这是我希望传递到 ListActivity.java 的包中的数据能够被访问的地方。

public class OrderList extends ListFragment
{
//THIS IS WHERE I WANT TO ACCESS THE VARIABLES PASSED INTO ListActivity.java    
public OrderList() 
{
    super();
    // TODO Auto-generated constructor stub
}


@SuppressWarnings({ })
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}
}

这可能吗?如果这是一个菜鸟问题,我再次道歉,但我还没有找到答案。

先感谢您。

4

2 回答 2

2

您可以调用(从您的片段中)

String companyKey = ((ListActivity)getActivity()).companyKey;
String techId = ((ListActivity)getActivity()).techId;
String userId = ((ListActivity)getActivity()).userId;
String startDate = ((ListActivity)getActivity()).startDate;
于 2013-06-20T16:08:33.693 回答
2

If your fragment is always only going to be attached to that Activity, you can make a public method on your Activity. Then just enforce that relationship between the fragment and the activity:

public class OrderList extends ListFragment {
    private MyListActivity activity;

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof MyListActivity)) {
            throw new IllegalStateException("must be attached to an instance of MyListActivity!");
        }
        this.activity = activity;
    }

    /* elsewhere you can call public methods defined in MyListActivity */
}

If this coupling is too tight for your taste, you can make an interface instead, and have the activity implement that interface.

You can do things the other way as well. Find the fragment from the activity using the FragmentManager, check if it's an instance of OrderList, and call public methods on it:

OrderList fragment = (OrderList) getFragmentManager().findFragmentById(R.id.titles);
fragment.whatever();
于 2013-06-20T16:10:21.997 回答