如何在Notes应用程序中保存笔记?

时间:2015-09-05 18:32:04

标签: java android listview android-edittext

我构建了3个活动: 首先(" MainActivity") - 添加音符标题的主屏幕,以及从listView添加/删除。 第二个(" EditNote") - 一次,你按下listView上的一个项目进入第二个活动以添加" body"如果你想要注释和编辑标题。 第三个java类名为" Notes"它有两个字符串"主题" +" Body",setter和getters。

我尝试做的是,一旦用户完成了对音符(主题,正文)的编辑,我希望将音符保存在arrayList中并返回到MainView的listView。

主要活动:

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.Destroyable;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> list = new ArrayList<>();
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private EditText et;
    private Button buttonAdd;
    private final int AddCode = 10;
    private final int AddBody = 5;

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

        et = (EditText) findViewById(R.id.editNewItem);
        ImageButton add = (ImageButton) findViewById(R.id.AddButton);
        final ListView listView = (ListView) findViewById(R.id.listView);

        adapter = new ArrayAdapter<>(this, R.layout.list, list);
        listView.setAdapter(adapter);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                list.add(String.valueOf(et.getText()));
                    adapter.notifyDataSetChanged();


            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, EditNote.class);
                intent.putExtra("Title", list.get(position));
                startActivityForResult(intent, AddCode);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, list.get(position) + " was removed", Toast.LENGTH_SHORT).show();
                list.remove(position);
                adapter.notifyDataSetInvalidated();
                return false;
            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == AddBody && resultCode == RESULT_OK ){
                Intent intent = getIntent();
                list.add(data.getStringExtra("Title"));
                adapter.notifyDataSetChanged();




            }

    }
}

EditNote:

import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class EditNote extends AppCompatActivity {


    private ArrayList<Notes> NotesList = new ArrayList<>();
    private ArrayList<String> StringList = new ArrayList<>();
    private ArrayAdapter<Notes> NotesAdapter;

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

        Button Save = (Button)findViewById(R.id.SaveButton);
        final EditText Title = (EditText)findViewById(R.id.TitleEditView);
         final String title = Title.getText().toString();
        final EditText Body = (EditText)findViewById(R.id.BodyEditView);
         final String body = Body.getText().toString();
        final Intent intent = getIntent();


        Title.setText(intent.getStringExtra("Title"));



        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();


            }
        });

        Button cancel = (Button)findViewById(R.id.CancelButton);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(EditNote.this);
                builder.setTitle(" Warning ");
                builder.setMessage("by pressing ok data will get lost");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1 = new Intent(EditNote.this, MainActivity.class);
                        startActivityForResult(intent1, 20);
                        Toast.makeText(EditNote.this,"Bye Bye Data",Toast.LENGTH_LONG).show();
                    }
                });
                builder.setNeutralButton("cancel", null);
                builder.setNegativeButton("No", null);
                builder.show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        final EditText Body = (EditText)findViewById(R.id.BodyEditView);
        final TextView Title = (TextView)findViewById(R.id.TitleEditView);

        if (id == R.id.action_settings) {

            Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND);
            String[] recipients = new String[]{"" , "" ,};
            emailIntent.putExtra( android.content.Intent.EXTRA_EMAIL, recipients);
            emailIntent.putExtra( android.content.Intent.EXTRA_SUBJECT,  "New Note " + Title.getText() );
            emailIntent.putExtra( android.content.Intent.EXTRA_TEXT, Body.getText()  + "");
            emailIntent.setType("message/rfc822");
            startActivity( Intent.createChooser(emailIntent, "Send Email" ));
        }

        return super.onOptionsItemSelected(item);
    }
}

笔记课程:

import android.widget.EditText;

public class Notes {

    private String subject;
    private String body;

    public Notes() {
    }

    public Notes(String subject, String body) {
        this.subject= subject;
        this.body = body;

    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;

    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return subject+" "+body+ " ";
    }

}

非常感谢!!

0 个答案:

没有答案