obsluga adaptera z recycleview

This commit is contained in:
Naiki00 2021-02-16 12:22:27 +01:00
parent bc48afcbdf
commit 6d81329448

View File

@ -7,18 +7,26 @@ import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.example.notatkon.R; import com.example.notatkon.R;
import com.example.notatkon.adapter.NoteAdapter;
import com.example.notatkon.entities.NoteEntity; import com.example.notatkon.entities.NoteEntity;
import com.example.notatkon.database.NoteRoomDatabase; import com.example.notatkon.database.NoteRoomDatabase;
import com.example.notatkon.note.CreateNote; import com.example.notatkon.note.CreateNote;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_NEW_NOTE = 1; private static final int REQUEST_CODE_NEW_NOTE = 1;
private List<NoteEntity> noteEntityList;
private RecyclerView noteRecycler;
private NoteAdapter noteAdapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -32,8 +40,6 @@ public class MainActivity extends AppCompatActivity {
); );
}); });
getAllNotes();
}
//Toolbar toolbar = findViewById(R.id.toolbar); //Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar); //setSupportActionBar(toolbar);
@ -56,25 +62,34 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
/* -- Pierwsza wersja apki -- */
//-- Pierwsza wersja apki --
//metody //metody
//https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps //https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notes); noteRecycler = (RecyclerView) findViewById(R.id.notes);
//ustaw LayoutManagera wertykalnie //ustaw LayoutManagera wertykalnie
recyclerView.setLayoutManager(new LinearLayoutManager(this)); //recyclerView.setLayoutManager(new LinearLayoutManager(this));
//ustaw LayoutManagera horyzontalnie //ustaw LayoutManagera horyzontalnie
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); noteRecycler.setLayoutManager(
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
);
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy //wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
ArrayList<Note> notes = new ArrayList<Note>();
for (int i = 0; i < 20; i++) { noteEntityList = new ArrayList<>();
notes.add(new Note());
}
//połącz Adapter z RecycleView //połącz Adapter z RecycleView
recyclerView.setAdapter(new NoteAdapter(notes, recyclerView)); noteAdapter = new NoteAdapter(noteEntityList);
noteRecycler.setAdapter(noteAdapter);
getAllNotes();
}
@ -120,7 +135,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onPostExecute(List<NoteEntity> noteEntities) { protected void onPostExecute(List<NoteEntity> noteEntities) {
super.onPostExecute(noteEntities); super.onPostExecute(noteEntities);
Log.d("NOTES", noteEntities.toString()); //Log.d("NOTES", noteEntities.toString());
} }
} }
new GetNotesTask().execute(); new GetNotesTask().execute();