I have two fragments. After I click on button in the first one, it moves to the second fragment with some SharedPreferences. Everytime I click on that button, I want to add new textview under another with text from SharedPreferences in the second fragment.
Here is code from second fragment where I want to add 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;
}
}
It add textview with correct text, but next time when I press the button it just change text of the textview (or create one textview on another?)
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>
I managed to make it work with TinyDB, which can store string arraylist as SharedPreferences.
This is what I have in fragment with button
TinyDB tinydb=new TinyDB(getActivity().getApplicationContext());
incometemp = edttxt.getText().toString();
mylist= tinydb.getListString("income");
mylist.add(incometemp );
tinydb.putListString("income", mylist);
Second fragment
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.