null

я новичок в Android Studio.
Я делаю приложение заметочник, используя RecycleView и для сохранения заметок SQLite. Моя проблема в том, что при создании заметки, она не отображается на экране, хотя в базе данных записывается. Буду ОЧЕНЬ благодарен тому, кто сможет найти проблему

MainActivity.java:

    package denchic45.myapp.appmy;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;
import denchic45.myapp.appmy.model.Note;
public class MainActivity extends AppCompatActivity {

FloatingActionButton createNote;
TextView noneText;
ImageView noneImage;
List<Note> notes;
private RecyclerView recyclerView;
private Recycle_Adapter RecycleAdapter;

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

    DataBase myDb = new DataBase(this);
    notes=myDb.getAllNotes();
    createNote = findViewById(R.id.fab);
    recyclerView = (RecyclerView) findViewById(R.id.notes);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    RecycleAdapter = new Recycle_Adapter(this,notes);
    recyclerView.setAdapter(RecycleAdapter);


    emptyList();

    //Open page CreateNote
    createNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreateNote();
        }
    });
}

public void CreateNote() {
    Intent intent = new Intent(this, activity_create_note.class);
    startActivity(intent);
    finish();
}

public void emptyList() {
    //check for empty lis
    boolean emp = notes.isEmpty();
    noneImage =(ImageView) findViewById(R.id.noneImage);
    noneText = findViewById(R.id.noneText);
    if (emp == true) {
        noneText.setVisibility(View.VISIBLE);
        noneImage.setVisibility(View.VISIBLE);
    }
    else{
        noneText.setVisibility(View.GONE);
        noneImage.setVisibility(View.GONE);
    }
}
}

Note.java:

package denchic45.myapp.appmy.model;
public class Note {
    private String noteText;
    private long ID;

    public Note() {}
    public Note(String noteText) {
        this.noteText = noteText;
    }
     public Note(long id, String noteText) {
        this.ID = id;
        this.noteText = noteText;
    }


    public String getNoteText() {
        return noteText;
    }

    public void setNoteText(String noteText) {
        this.noteText = noteText;
    }

    public long getID() {
        return ID;
    }

    public void setID(long ID) {
        this.ID = ID;
    }
    }

Recycle_Adapter.java:

package denchic45.myapp.appmy;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

import denchic45.myapp.appmy.model.Note;

public class Recycle_Adapter extends RecyclerView.Adapter<Recycle_Adapter.viewHolder> {

    List<Note> notesList;
    LayoutInflater inflater;

    public Recycle_Adapter(Context context, List<Note> notesList) {
        this.inflater = LayoutInflater.from(context);
        this.notesList = notesList;
    }
    public class viewHolder extends RecyclerView.ViewHolder {

        TextView note;

        public viewHolder(View itemView) {
            super(itemView);
            note = itemView.findViewById(R.id.note);
        }
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.from(parent.getContext()).inflate(R.layout.list, parent,false);
        return new viewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull viewHolder holder, int position) {
        String noteTitle = notesList.get(position).getNoteText();
        holder.note.setText(noteTitle);
    }

    @Override
    public int getItemCount() {
        return notesList.size();
    }
}

DataBase.java:

    package denchic45.myapp.appmy;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

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

import denchic45.myapp.appmy.model.Note;

public class DataBase extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "note.db";
    public static final String DATABASE_TABLE = "notesTable";

    //columns name table

    public static final String KEY_ID = "id";
    public static final String KEY_TITLE = "title";

    public DataBase(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+ DATABASE_TABLE+ "("+ KEY_ID + " INTEGER PRIMARY KEY," +
                KEY_TITLE +" TEXT"+
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE "+ DATABASE_TABLE);
        onCreate(db);
    }
    public long insertNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_TITLE, note.getNoteText());
        long result = db.insert(DATABASE_TABLE, null, contentValues);
                return result;
    }

    public Note getNote(long id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(DATABASE_TABLE, new String[]{KEY_ID,KEY_TITLE},KEY_ID+"=?",
        new String[]{String.valueOf(id)},null,null,null);

        if(cursor != null) {
            cursor.moveToFirst();
        }
        return new Note(cursor.getLong(0),cursor.getString(1));
    }


    public List<Note> getAllNotes() {
        SQLiteDatabase db = this.getReadableDatabase();
        List<Note> allNotes = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+DATABASE_TABLE, null);

        if (cursor.moveToFirst()) {
            do {
              Note note = new Note();
              note.setID(cursor.getLong(0));
                note.setID(cursor.getLong(1));

                allNotes.add(note);
            }while (cursor.moveToNext());
        }

        return allNotes;
    }
}
Asked on January 19, 2020 in Programming.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.