如何从片段中启动另一个活动,将所选成员信息添加到附加内容?

时间:2015-04-21 13:17:26

标签: android android-intent android-fragments

这是内容片段activity_fpage.xml

的第一页
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ImageView
    android:id="@+id/gymView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/arnold"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/appIcon"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/newUser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/arnold"
    android:layout_centerHorizontal="true"
    android:minWidth="288dip"
    android:text="@string/newUser" />

<fragment
    android:id="@+id/fragmet_user_list"
    android:name="com.example.gym1_1.ListOfUsers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/newUser" />

接下来是片段

的java文件
    import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;

public class ListOfUsers extends ListFragment implements OnClickListener {

  String data[] = new String[] { "Matt", "Ken", "July", "Trish" };

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, data);
    setListAdapter(adapter);
    }

@Override
public void onClick(View v) {
    Intent intent = new Intent(getActivity(),com.example.gym1_1.MainActivity.class);
    startActivity(intent);

}
}

但显示布局启动的主要类是

public class First_page extends ActionBarActivity {

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

问题是如何使“Matt”,“Ken”,“July”,“Trish”可点击,通过点击哪个用户将被发送到下一个活动,点击名称放入额外内容? 感谢

2 个答案:

答案 0 :(得分:0)

@Override
public void onClick(View v) {
    Intent intent = new Intent(getActivity(),com.example.gym1_1.MainActivity.class);
intent.putExtra("name_key",chosenName); // get the chosenName as a String from your widget with names
    startActivity(intent);
}

答案 1 :(得分:0)

您必须覆盖方法

public void onListItemClick (ListView l, View v, int position, long id)

然后,使用该位置,从数组中获取选择的名称,然后将其传递给intent。类似的东西:

 @Override
 public void onListItemClick (ListView l, View v, int position, long id){
     Intent intent = new Intent(getActivity(),com.example.gym1_1.MainActivity.class);
     intent.putExtra("name", data[position]);
     startActivity(intent);
 }