how to launch another activity from fragment with

2019-09-17 01:17发布

问题:

Here is the first page that contents fragment 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" />

next is the java file for fragment

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

}
}

but the main class where the shown layout launches is

public class First_page extends ActionBarActivity {

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

The question is how to make "Matt", "Ken", "July", "Trish" clickable, by clicking which user would be sent to the next activity with with clicked name put in extras? Thanks

回答1:

You have to Override the method

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

then, with the position, get the choosen name from your array, and then pass it to the intent. Something like:

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


回答2:

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