从以前的活动中获取文本到片段

时间:2017-05-29 00:49:59

标签: android android-layout android-fragments

我正在尝试将之前Activity的数据转换为Fragment。我可以在没有Fragment的情况下获取数据,但是当我将代码放在Fragment中时,它无法正常工作。我的片段在描述课内。这是我的代码:

这是我从以前的Activity发送数据的方式:

TextView textView = (TextView) view.findViewById(R.id.txt_name);
String text = textView.getText().toString();
    if(text.equals("ADA")) {
        intent = new Intent(view.getContext(), description.class);
        intent.putExtra("ADA", text);
        startActivity(intent);
     }

这就是我在Fragment内获取它的方式:

public static class DummyFragment extends Fragment {
    int color;
    TextView mTextview;

    public DummyFragment() {
    }

    @SuppressLint("ValidFragment")
    public DummyFragment(int color) {
        this.color = color;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);

        final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
        frameLayout.setBackgroundColor(color);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        mTextview = (TextView) view.findViewById(R.id.textView1);
        mTextview.setText(getActivity().getIntent().getStringExtra("ADA"));

        return view;
    }
}

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/dummyfrag_bg"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/bg_light">

<android.support.v7.widget.RecyclerView
    android:id="@+id/dummyfrag_scrollableview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin" />
</FrameLayout>

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

TextView textView = (TextView) view.findViewById(R.id.txt_name);
String text = textView.getText().toString();
if(text.equals("ADA")) {
   DummyFragment fragment = new DummyFragment();
   Bundle bundle = new Bundle();
   bundle.putString("ADA",text);
   fragment.setArguments(bundle);
   getSupportFragmentManager()
         .beginTransaction()
         .replace(fragment,R.id.layout)
         .addTobackStack(null)
         .commit();
}

然后在你的片段中

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);
        String text = getArguments().getString("ADA");

}

修改

1)fragmentTransaction.replace(int containerViewId,Fragment fragment,String tag)

替换添加到容器的现有片段。这与使用相同的containerViewId添加的所有当前添加的片段调用remove(Fragment),然后使用此处给出的相同参数添加(int,Fragment,String)基本相同。

2)fragmentTransaction.add(int containerViewId,Fragment fragment,String tag)

将片段添加到活动状态。此片段也可以选择将其视图(如果Fragment.onCreateView返回非null)放入活动的容器视图中。

答案 1 :(得分:0)

使用SharedPreferences存储文本。在加载片段

之前的活动中执行此操作
    SharedPreferences sharedPreferences = getSharedPreferences("TextHolder", 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("ADA", textView.getText().toString()).apply();

在Fragment中获取SharedPreferences中的文本,如

String textHolder = getActivity().getSharedPreferences("TextHolder", 0).getString("ADA", "");

现在将textHolder String设置为Fragment

中所选的TextView