为什么ListView没有保存到内存中?

huangapple 未分类评论45阅读模式
英文:

Why isn't the ListView saved to the memory?

问题

以下是翻译好的代码部分:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ... (其他代码)

        ArrayList<nume_list> colegi_list = DataHolder.getInstance().people;

        ColegListAdapter adapter1 = new ColegListAdapter(this, R.layout.list_item_layout, colegi_list);
        nume_ListView.setAdapter(adapter1);

        if (savedInstanceState == null) {
            // ... (其他代码)
        } else {
            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Schoolbox", MODE_PRIVATE);

            // ... (其他代码)
        }

        addColegButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ColegInfoActivity.class);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
            }
        });
    }
}

// 在详细信息活动中将详细信息添加到新对象的 Java 代码(稍后将其添加到 ListView 中的新项)

Intent intent1 = new Intent(ColegInfoActivity.this, MainActivity.class);
Bundle extras = new Bundle();
extras.putString("Prenume", prenumeString);
extras.putString("NumeFam", numeFamString);
extras.putString("Eticheta", etichetaString);
extras.putString("Email", emailString);
extras.putInt("Number", number2);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Schoolbox", MODE_PRIVATE);
sharedPreferences.edit().putString("Prenume", prenumeString).apply();
sharedPreferences.edit().putString("NumeFam", numeFamString).apply();
sharedPreferences.edit().putString("Eticheta", etichetaString).apply();
sharedPreferences.edit().putString("Email", emailString).apply();
sharedPreferences.edit().putInt("Number", number2).apply();

请注意,我已经将您提供的代码片段翻译成中文,并移除了您在代码中提到的"不要有别的内容",只保留了代码本身的翻译。如果您有任何问题或需要进一步的帮助,请随时问我。

英文:

I have a problem with my app. There is a ListView with custom objects and I want it to be saved in memory and to be able to add new things in it when the app is running. The thing is that I don't know how to save my ListView in memory and show it. Can you please help?

public class MainActivity extends AppCompatActivity {
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String nume;
        String prenume;
        String email;
        String eticheta;
        int number;

        TextView addColegButton = findViewById(R.id.textView3);
        ListView nume_ListView = findViewById(R.id.ListView);

        String nou = getString(R.string.nou);
        String addClassmate = getString(R.string.AddClassmate);

        TextView buna = findViewById(R.id.bunaTextView);
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);


        if (hour &gt;= 12 &amp;&amp; hour &lt;= 18) {
            buna.setText(R.string.bună);
            buna.setTextSize(43);
        }
        if (hour &gt;= 18 &amp;&amp; hour &lt;= 24) {
            buna.setText(R.string.bunăSeara);
            buna.setTextSize(39);
        }
        if (hour &gt;= 0 &amp;&amp; hour &lt;= 12) {
            buna.setText(R.string.bunăDimi);
            buna.setTextSize(36);
        }

        ArrayList&lt;nume_list&gt; colegi_list = DataHolder.getInstance().people;

        ColegListAdapter adapter1 = new ColegListAdapter(this, R.layout.list_item_layout, colegi_list);
        nume_ListView.setAdapter(adapter1);


        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                nume = null;
                prenume = null;
                eticheta=null;
                email=null;
                number= 0;
            } else {
                nume = extras.getString(&quot;NumeFam&quot;);
                prenume = extras.getString(&quot;Prenume&quot;);
                email = extras.getString(&quot;Email&quot;);
                number = extras.getInt(&quot;Number&quot;);
                eticheta = extras.getString(&quot;Eticheta&quot;);
                String nume_complet = prenume + &quot; &quot; + nume;

                nume_list colegNou = new nume_list(nume_complet, eticheta, email, number);
                nume_list colegNouv2 = new nume_list(prenume, eticheta, email, number);

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_custom,
                        (ViewGroup) findViewById(R.id.custom_toast_container));

                TextView text = (TextView) layout.findViewById(R.id.textToast);

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,50);
                toast.setView(layout);

                if (prenume != null &amp;&amp; nume != null &amp;&amp; eticheta != null) {
                    colegi_list.add(colegNou);
                    text.setText(R.string.toast1);
                    toast.show();
                }

                else if (nume == null &amp;&amp; eticheta != null &amp;&amp; prenume != null) {
                    colegi_list.add(colegNouv2);
                    text.setText(R.string.toast1);
                    toast.show();
                }

                else{
                    text.setText(R.string.toast2);
                    toast.show();
                }

            }
        } else {
            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(&quot;Schoolbox&quot;, MODE_PRIVATE);

            nume = sharedPreferences.getString(&quot;NumeFam&quot;, &quot;&quot;);
            prenume = sharedPreferences.getString(&quot;Prenume&quot;,&quot;&quot;);
            eticheta = sharedPreferences.getString(&quot;Eticheta&quot;,&quot;&quot;);
            email = sharedPreferences.getString(&quot;Email&quot;,&quot;&quot;);
            number =sharedPreferences.getInt(&quot;Number&quot;,0);

            String nume_complet = prenume + &quot; &quot; + nume;
            nume_list colegNou = new nume_list(nume_complet, eticheta, email, number);
            nume_list colegNouv2 = new nume_list(prenume, eticheta, email, number);

            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_custom,
                    (ViewGroup) findViewById(R.id.custom_toast_container));

            TextView text = (TextView) layout.findViewById(R.id.textToast);

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,50);
            toast.setView(layout);

            if (prenume != null &amp;&amp; nume != null &amp;&amp; eticheta != null) {
                colegi_list.add(colegNou);
                text.setText(R.string.toast1);
                toast.show();
            }

            else if (nume == null &amp;&amp; eticheta != null &amp;&amp; prenume != null) {
                colegi_list.add(colegNouv2);
                text.setText(R.string.toast1);
                toast.show();
            }

            else{
                text.setText(R.string.toast2);
                toast.show();
            }
        }

        addColegButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ColegInfoActivity.class);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
            }
        });



        //TODO: Create the new classmate activity and set OnClickListener for the button to it.
    }
}

THE DETAILS ACTIVITY JAVA CODE (Where the details are added to a new object, later a new item in the ListView)

Intent intent1 = new Intent(ColegInfoActivity.this, MainActivity.class);
                Bundle extras = new Bundle();
                extras.putString(&quot;Prenume&quot;, prenumeString);
                extras.putString(&quot;NumeFam&quot;, numeFamString);
                extras.putString(&quot;Eticheta&quot;,etichetaString);
                extras.putString(&quot;Email&quot;, emailString);
                extras.putInt(&quot;Number&quot;,number2);
                SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(&quot;Schoolbox&quot;, MODE_PRIVATE);
                sharedPreferences.edit().putString(&quot;Prenume&quot;, prenumeString).apply();
                sharedPreferences.edit().putString(&quot;NumeFam&quot;, numeFamString).apply();
                sharedPreferences.edit().putString(&quot;Eticheta&quot;,etichetaString).apply();
                sharedPreferences.edit().putString(&quot;Email&quot;, emailString).apply();
                sharedPreferences.edit().putInt(&quot;Number&quot;,number2).apply();

huangapple
  • 本文由 发表于 2020年4月8日 01:35:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61085982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定