2017-10-19 31 views
-1

J'ai deux fragments. Après avoir cliqué sur le bouton dans le premier, il se déplace vers le second fragment avec quelques SharedPreferences. Chaque fois que je clique sur ce bouton, je veux ajouter une nouvelle textview sous une autre avec le texte de SharedPreferences dans le second fragment.Ajouter un nouveau textview dans un fragment après avoir cliqué sur le bouton dans un autre fragment

Code est ici de la deuxième fragment où je veux ajouter textviews

public class HomeFragment extends Fragment{ 


    public HomeFragment() { 
     // Required empty public constructor 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_home, container, false); 

     SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE); 
     String Income =sharedPreferences.getString("Income","N/A"); 

     LinearLayout m_ll = (LinearLayout) view.findViewById(R.id.home); 

     m_ll.addView(createNewTextView(Income)); 
     return m_ll; 
    } 

    private TextView createNewTextView(String text) { 
     final LinearLayoutCompat.LayoutParams lparams = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT); 
     final TextView textView = new TextView(getActivity()); 
     textView.setLayoutParams(lparams); 
     textView.setText("New text: " + text); 
     return textView; 
    } 
} 

Ajouter textview avec le texte correct, mais la prochaine fois quand j'appuie sur le bouton, il suffit de changer le texte de la textview (ou en créer un textview sur un autre?)

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:id="@+id/home" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.xxxx.HomeFragment"> 

<!-- TODO: Update blank fragment layout --> 
</LinearLayout> 
+1

il créer un seul textview que la mise en page ne contiendra pas gonflé votre plus textview d'origine. Vous devez enregistrer vos données sous la forme d'un tableau de chaînes et en fonction de la longueur du tableau, créez autant de textview – nomag

Répondre

0

J'ai réussi à le faire fonctionner avec TinyDB, qui peut stocker arraylist de chaîne comme SharedPreferences.

C'est ce que j'ai dans le fragment avec le bouton

TinyDB tinydb=new TinyDB(getActivity().getApplicationContext()); 

    incometemp = edttxt.getText().toString(); 
    mylist= tinydb.getListString("income"); 
    mylist.add(incometemp); 
    tinydb.putListString("income", mylist); 

fragment deuxième

TinyDB tinydb; 
    ArrayList<String> mylist = new ArrayList<>(); 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_home, container, false); 
     tinydb = new TinyDB(getActivity().getApplicationContext()); 
     mylist = tinydb.getListString("income"); 
     LinearLayout m_ll = (LinearLayout) view.findViewById(R.id.home); 

     for (int i=0;i<mylist.size();i++) { 
      m_ll.addView(createNewTextView(mylist.get(i))); 
     } 


     return m_ll; 
    } 

    private TextView createNewTextView(String text) { 
     final LinearLayoutCompat.LayoutParams lparams = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT); 
     final TextView textView = new TextView(getActivity()); 
     textView.setLayoutParams(lparams); 
     textView.setText("New text: " + text); 
     return textView; 
    } 
}