如何在不同的活动中将Edit Text值设置为null?

时间:2019-07-07 03:52:28

标签: java android

首先,我有两个活动。一个是MainActivity,另一个是NoteEditorActivity。我在NoteEditorActivity中创建了一个表,其中包含约40个编辑文本。但是,当我从菜单中在应用程序中添加便笺时,它会创建另一个便笺,但不会刷新编辑文本中的值。我想知道如何将所有文本存储在数组列表中,并在添加注释时将所有值刷新为null。

我已经尝试了很多其他方法,但是这种方法可能很有希望。

这是主要活动

public class MainActivity extends AppCompatActivity {

    static ArrayList<String> notes = new ArrayList<>();
    static ArrayAdapter arrayAdapter;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.add_note_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.add_note) {

            Intent intent = new Intent(getApplicationContext(), TableNoteEditorActivity.class);

            startActivity(intent);

            return true;
        }

        return false;
    }

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

        ListView listView = (ListView) findViewById(R.id.listView);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.NoteLab", Context.MODE_PRIVATE);

        HashSet<String> set = (HashSet<String>) sharedPreferences.getStringSet("notes", null);

        HashSet<String> tableInputSet = (HashSet<String>) sharedPreferences.getStringSet("tableInput", null);

        if (set == null) {

            notes.add("Example");

        } else {

            notes = new ArrayList(set);
        }

        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);

        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(getApplicationContext(), TableNoteEditorActivity.class);
                intent.putExtra("noteId", i);
                startActivity(intent);

            }
        });

这是TableNoteEditorActivity

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

        EditText editText = (EditText) findViewById(R.id.titleViewInput);

        et1 = (EditText) findViewById(R.id.textView1);
        et2 = (EditText) findViewById(R.id.textView2);
        et3 = (EditText) findViewById(R.id.textView3);
        et4 = (EditText) findViewById(R.id.textView4);
        et5 = (EditText) findViewById(R.id.textView5);
        et6 = (EditText) findViewById(R.id.textView6);
        et7 = (EditText) findViewById(R.id.textView7);
        et8 = (EditText) findViewById(R.id.textView8);
        et9 = (EditText) findViewById(R.id.textView9);
        et10 = (EditText) findViewById(R.id.textView10);
        et11 = (EditText) findViewById(R.id.textView11);
        et12 = (EditText) findViewById(R.id.textView12);

        loadSavedPreferences();

        Intent intent = getIntent();
        noteId = intent.getIntExtra("noteId", -1);

        if(noteId != -1){

            editText.setText(MainActivity.notes.get(noteId));

        } else {

            MainActivity.notes.add("");
            noteId = MainActivity.notes.size() - 1;
            MainActivity.arrayAdapter.notifyDataSetChanged();
        }

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                MainActivity.notes.set(noteId, String.valueOf(charSequence));
                MainActivity.arrayAdapter.notifyDataSetChanged();

                SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.NoteLab", Context.MODE_PRIVATE);

                HashSet<String> set = new HashSet(MainActivity.notes);

                sharedPreferences.edit().putStringSet("notes", set).apply();

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

https://www.youtube.com/watch?v=48EB4HeP1kI&list=LLhpPM8FLd5uST7RfQa_riQg&index=3&t=1842s

我看着它创建了笔记应用的基础。

0 个答案:

没有答案