簡體   English   中英

單擊另一個片段中的按鈕后,在片段中添加新的文本視圖

[英]Add new textview in fragment after click on button in another fragment

我有兩個片段。 在我點擊第一個按鈕后,它會移動到帶有一些 SharedPreferences 的第二個片段。 每次單擊該按鈕時,我都想在第二個片段中使用來自 SharedPreferences 的文本在另一個下添加新的文本視圖。

這是我想添加文本視圖的第二個片段的代碼

    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;
    }
}

它添加帶有正確文本的文本視圖,但是下次當我按下按鈕時,它只會更改文本視圖的文本(或在另一個文本視圖上創建一個文本視圖?)

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>

我設法讓它與 TinyDB 一起工作,它可以將字符串數組列表存儲為 SharedPreferences。

這就是我在帶有按鈕的片段中所擁有的

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

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

第二個片段

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM