2016-03-03 110 views
0

从类的onListItemClick ListFragment方法更改TextView的文本。 这是该方法:从onListItemClick更改“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;

+0

没有功能:TextView t =(TextView)v.findViewB YID(R.id.ss); t.setText(s); – rovi

+0

如果你把错误的堆栈跟踪输出,这将是很好的。 –

+0

错误是:java.lang.NullPointerException:试图调用空对象引用的虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)' – rovi

回答

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; 

} 
+0

添加行:TextView mTextView =(TextView)getActivity()。findViewById(R.id.ss); And methdo“onListItemClick”: TextView t =(TextView)getActivity()。findViewById(R.id.ss); t。的setText(一个或多个); – rovi

+0

将其添加为全局参考。在顶部你需要有这样的: 私人TextView mTextView –

+0

@rovi看到我的编辑。 –