如何启动从另一个片段与活动将选择会员信息到演员? [英] how to launch another activity from fragment with putting chosen member info into extras?

查看:155
本文介绍了如何启动从另一个片段与活动将选择会员信息到演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是第一页的内容片断activity_fpage.xml

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" />

接下来是片段Java文件

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

推荐答案

您必须覆盖的方法

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

这篇关于如何启动从另一个片段与活动将选择会员信息到演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆