NOVINKA: Začni v IT jako webmaster s komplexním akreditovaným online kurzem Tvůrce WWW stránek. Zjisti více:
NOVINKA: Staň se datovým analytikem a získej jistotu práce, lepší plat a nové kariérní možnosti. Více informací:

Main

package qwasyx0.notepad;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import qwasyx0.notepad.R;
import android.app.AlertDialog.Builder;
import android.widget.Toast;

import java.util.ArrayList;

public class Main extends Activity {

    ArrayList<FileName> filenames;
    ListViewAdapter adapter;
    ListView lv_filenames;

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

        final NoteManager nm = new NoteManager(getApplicationContext());
        nm.CreateNewDirectory();

        filenames = nm.GetNotesList();

        adapter = new ListViewAdapter(this, R.layout.listview_item, filenames);

        lv_filenames = (ListView) findViewById(R.id.lv_filename);
        lv_filenames.setAdapter(adapter);


        lv_filenames.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Intent intent = new Intent(Main.this, Note.class);
                intent.putExtra("filename", filenames.get(position).getName());
                startActivity(intent);
            }
        }
        );
        lv_filenames.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);
                alert.setTitle("Delete note");
                alert.setMessage("Are you sure you want to delete this note?");

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        adapter.remove(filenames.get(position));
                        adapter.notifyDataSetChanged();
                        Toast.makeText(Main.this, "deleted", Toast.LENGTH_SHORT).show();
                    }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });

                alert.show();

                return false;
            }

        });

                /*filenames.remove(filenames.get(position));
                filenames.remove(position);
                FileName selectedItem = adapter.getItem(position);
                nm.DeleteNote(selectedItem);
                nm.DeleteNote(filenames.get(position));*/





    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                Intent intent = new Intent(Main.this, Note.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}



package qwasyx0.notepad;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class NoteManager {

    private Context context;
    private String path = Environment.getExternalStorageDirectory().toString() + "/Notes/";

    public NoteManager(Context _context) {
        this.context = _context;
    }

    public NoteManager() {

    }

    public void CreateNewDirectory() {
        File mydir = new File(this.path);
        if (!mydir.exists())
            mydir.mkdirs();
        else
            Log.d("error", "dir. already exists");
    }
    public void DeleteNote(FileName object) {
        GetNotesList().remove(object);


        }


    public void SaveNote(String sFileName, String sBody) {
        try {
            File root = new File(this.path);
            if (!root.exists()) {
                root.mkdirs();
            }
            File file = new File(root, sFileName);
            FileWriter writer = new FileWriter(file);
            writer.append(sBody);
            writer.flush();
            writer.close();
            Toast.makeText(this.context, "Saved", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            //importError = e.getMessage();
            //iError();
        }
    }

    public String readNote(String filename) {
        StringBuilder text = new StringBuilder();
        try {
            //File sdcard = Environment.getExternalStorageDirectory() + "/Notes/";
            File file = new File(this.path, filename);
            //System.out.println("exception");

            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                //System.out.println("text : "+text+" : end");
                text.append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("hello");

        }

        return text.toString();
    }

    public ArrayList<FileName> GetNotesList() {

        File f = new File(this.path);
        File file[] = f.listFiles();

        ArrayList<FileName> filenames = new ArrayList<FileName>();
        for (int i = 0; i < file.length; i++) {
            String desc = new NoteManager().readNote(file[i].getName());
            FileName filename = new FileName(file[i].getName(), desc);
            filenames.add(filename);
        }

        return filenames;
    }


}

Neformátovaný

Přidáno: 14.5.2017
Expirace: Neuvedeno

Avatar
Autor: qwasyx0
Aktivity