how to launch another activity from fragment with putting chosen member info into extras?

我的未来我决定 提交于 2019-12-25 10:17:40

问题


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


来源:https://stackoverflow.com/questions/29773339/how-to-launch-another-activity-from-fragment-with-putting-chosen-member-info-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!