从onListItemClick更改“TextView”的文本

时间:2016-03-03 15:47:19

标签: android xml

从类的onListItemClick ListFragment方法更改TextView的文本。 这是方法:

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

        String selectedValue = (String) getListAdapter().getItem(position);

        Descripcion_Noticias myDetailFragment = new Descripcion_Noticias();

            FragmentTransaction fragmentTransaction =
                    getActivity().getFragmentManager().beginTransaction();

        myDetailFragment.setText(v,selectedValue);

        fragmentTransaction.replace(R.id.fragment, myDetailFragment);
        fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();


    }

xml是:
    

XMl是新的片段,扩展片段。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/ss"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

ListFragment的方法:

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

        String selectedValue = (String) getListAdapter().getItem(position);
        CharSequence s =selectedValue;
        TextView t = (TextView) getActivity().findViewById(R.id.ss);
        t.setText(s);

        Descripcion_Noticias myDetailFragment = new Descripcion_Noticias();

            FragmentTransaction fragmentTransaction =
                    getActivity().getFragmentManager().beginTransaction();

        fragmentTransaction.replace(R.id.fragment, myDetailFragment);
        fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();


    }

The class primary of XMl: 

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.descripcion_noticias, null);
        return v;

    }

方法:

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

    String selectedValue = (String) getListAdapter().getItem(position);
    CharSequence s =selectedValue;
    mTextView.setText(s);

    Descripcion_Noticias myDetailFragment = new Descripcion_Noticias();

    FragmentTransaction fragmentTransaction =
            getActivity().getFragmentManager().beginTransaction();

    fragmentTransaction.replace(R.id.fragment, myDetailFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

}

类片段:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   View v = inflater.inflate(R.layout.descripcion_noticias, null);
   mTextView = (TextView) getActivity().findViewById (R.id.ss);
    return v;

}

两个班级有private TextView mTextView;

1 个答案:

答案 0 :(得分:0)

在onCreateView方法中,保存对TextView的全局引用:

mTextView = (TextView) v.findViewById(R.id.ss);

然后在你的onListItemClick中访问它:

mTextView.setText(s);

修改

在课程的顶部你输入:

private TextView mTextView;

在你的监听方法中:

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

    String selectedValue = (String) getListAdapter().getItem(position);
    CharSequence s =selectedValue;
    mTextView.setText(s);

    Descripcion_Noticias myDetailFragment = new Descripcion_Noticias();

        FragmentTransaction fragmentTransaction =
                getActivity().getFragmentManager().beginTransaction();

    fragmentTransaction.replace(R.id.fragment, myDetailFragment);
    fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();


}

XMl的主要类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.descripcion_noticias, null);
    mTextView = (TextView)v.findViewById(R.id.ss);
    return v;

}