Got irritating error with getLoaderManager.initLoader - java

I'm trying to use loader with asynctaskloader to load my listview on a fragment but got error with starting the loader:
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, context)
I know many have encounterd this error and i did researched but still not solved, can't understand why. If anyone got any ideas please answwer and thanks in advance!
here's my code:
import java.util.ArrayList;
import com.bblackbb.jotdownv2.R;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.content.AsyncTaskLoader;
import android.content.Loader;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.app.Activity;
public class WhiteNote extends Fragment implements **strong text**LoaderManager.LoaderCallbacks<ArrayList<NoteItems>> {
private NoteDatabase note_database;
private int i=0;
public Context context;
public NoteListAdapter noteListAdapter;
public ListView note_listview_container;
public SQLiteDatabase note_sqldb;
public Cursor c;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.white_note, container, false);
context=getActivity();
note_database = new NoteDatabase(context);
final EditText text_ed_1;
final EditText text_ed_2;
Button button_addNote;
Button button_listallNote;
Button button_delallNote;
text_ed_1 = (EditText)rootView.findViewById(R.id.textedit1);
text_ed_2 = (EditText)rootView.findViewById(R.id.textedit2);
button_addNote = (Button)rootView.findViewById(R.id.button1);
button_listallNote = (Button)rootView.findViewById(R.id.button2);
button_delallNote = (Button)rootView.findViewById(R.id.button3);
button_addNote.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
note_database.open();
note_database.createData(text_ed_1.getText().toString(),text_ed_2.getText().toString());
//i++;
note_database.close();
}
});
button_listallNote.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
note_database.open();
note_database.get_NoteListAdapter();
note_listview_container.setAdapter(note_database.noteListAdapter);
getLoaderManager().initLoader(0, null,context);
note_database.close();
}
});
button_delallNote.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
note_database.open();
note_database.deleteAllNote();
note_database.close();
}
});
return rootView;
}
#Override
public Loader<ArrayList<NoteItems>> onCreateLoader(int id, Bundle args) {
return new NoteItemsLoader(context, note_database);
}
#Override
public void onLoadFinished(Loader<ArrayList<NoteItems>> loader,
ArrayList<NoteItems> data) {
note_listview_container.setAdapter(new NoteListAdapter(context,data));
}
#Override
public void onLoaderReset(Loader<ArrayList<NoteItems>> loader) {
note_listview_container.setAdapter(null);
}
}
class NoteItemsLoader extends AsyncTaskLoader<ArrayList<NoteItems>> {
private ArrayList<NoteItems> loader_note_items= new ArrayList<NoteItems>();
private NoteDatabase loader_db;
public NoteItemsLoader(Context context, NoteDatabase db) {
super(context);
loader_db = db;
}
#Override
protected void onStartLoading() {
if (loader_note_items != null) {
deliverResult(loader_note_items); // Use the cache
}
else
forceLoad();
}
#Override
protected void onStopLoading() {
cancelLoad();
}
#Override
public ArrayList<NoteItems> loadInBackground() {
loader_db.open(); // Query the database
ArrayList<NoteItems> note_items = new ArrayList<NoteItems>();
loader_db.get_NoteListLoader(note_items,loader_db);
loader_db.close();
return note_items;
}
#Override
public void deliverResult(ArrayList<NoteItems> data) {
loader_note_items = data; // Caching
super.deliverResult(data);
}
#Override
protected void onReset() {
super.onReset();
onStopLoading();
loader_note_items = null;
}
#Override
public void onCanceled(ArrayList<NoteItems> data) {
super.onCanceled(data);
loader_note_items = null;
}
protected void onReleaseResources(ArrayList<NoteItems> data) {}
}

replace
getLoaderManager().initLoader(0, null,context);
with
getLoaderManager().initLoader(0, null,WhiteNote.this);
here your WhiteNode class implements LoaderManager.LoaderCallbacks but your Activity class in not implementing. so you have to pass reference of WhiteNote class.
and once check imports section as #Praveen Sharma said

Try to replace your old imports with these
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
hope this will fix your problem.

Related

How to pass Hashmap to another Activity and show it in Recycler View?

Cart Activity(Here I want to show this hashmap data in Recycler View...Any Help will be Highly Appreciated)
package com.example.silkenscoops;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Pair;
import java.util.HashMap;
public class CartActivity extends AppCompatActivity {
private HashMap<String, Pair<String,String>> cartMap=new HashMap<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_view);
cartMap= (HashMap<String, Pair<String, String>>) getIntent().getSerializableExtra("h");
}
}
My Adapter class
package com.example.silkenscoops;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import java.util.HashMap;
public class MainAdapter extends FirebaseRecyclerAdapter<Items, MainAdapter.myViewHolder> {
private HashMap<String, Pair<String,String>> cartMap=new HashMap<>();
public MainAdapter(#NonNull FirebaseRecyclerOptions<Items> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Items model) {
holder.Price.setText(model.getPrice());
holder.Product.setText(model.getProduct());
holder.Stock.setText(model.getStock());
holder.increment.setVisibility(View.GONE);
holder.decrement.setVisibility(View.GONE);
holder.add.setOnClickListener(new View.OnClickListener() {
int count=0;
#Override
public void onClick(View v) {
holder.increment.setVisibility(View.VISIBLE);
holder.decrement.setVisibility(View.VISIBLE);
if(count<Integer.parseInt(model.getStock())) {
count++;
holder.add.setText(String.valueOf(count));
cartMap.put(model.getProduct(),new Pair<String,String>(String.valueOf(count),model.getPrice()));
}
holder.decrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count>0) {
count--;
holder.add.setText(String.valueOf(count));
cartMap.put(model.getProduct(),new Pair<String,String>(String.valueOf(count),model.getPrice()));
}
if(count<=0){
holder.add.setText("ADD");
holder.increment.setVisibility(View.GONE);
holder.decrement.setVisibility(View.GONE);
cartMap.remove(model.getProduct());
}
}
});
}
});
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.display,parent,false);
return new MainAdapter.myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder{
TextView Product, Price, Stock,add;
ImageView decrement,increment;
public myViewHolder(#NonNull View itemView) {
super(itemView);
Price=itemView.findViewById(R.id.Price);
Product=itemView.findViewById(R.id.Product);
Stock=itemView.findViewById(R.id.Stock);
add=itemView.findViewById(R.id.add);
increment=itemView.findViewById(R.id.increment);
decrement=itemView.findViewById(R.id.decrement);
}
}
public HashMap<String, Pair<String, String>> getCartMap() {
return cartMap;
}
}
My MainActivity(Actually it is the menu list..)
package com.example.silkenscoops;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.util.HashMap;
public class MainItems extends AppCompatActivity {
private RecyclerView recyclerView;
private ImageView cart_btn;
MainAdapter adapter;
DatabaseReference databasereference;
private HashMap<String, Pair<String,String>> cartMap=new HashMap<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_display);
getSupportActionBar().hide();
cart_btn=findViewById(R.id.cart_btn);
databasereference = FirebaseDatabase.getInstance().getReference();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//This is the right Query that should be passed to the setQuery() method
//The fields should stay as in the Items class (I capital)
Query query = FirebaseDatabase.getInstance().getReference().child("Cups");
FirebaseRecyclerOptions<Items> options = new FirebaseRecyclerOptions.Builder<Items>()
.setQuery(query, Items.class)
.build();
adapter = new MainAdapter(options);
recyclerView.setAdapter(adapter);
cart_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cartMap=adapter.getCartMap();
Intent intent = new Intent(MainItems.this, CartActivity.class);
intent.putExtra("list", cartMap);
startActivity(intent);
}
});
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
There is no error , but how am I supposed to pass my hashmap from ActivityMain to Cart Activity and show it in recycler View...I have no idea now and after working and thinking for 10-15 days I'm asking here..Please help me..
Thanks

android Recycler View not showing Items but they are there

I am making a practice app for my self creating friends profile my self in my app and having their avatar as imageview and name,nickname as text view it accepts object but it never shows anything on recyclerview
Please Help!
My main activity.java
package rex.MyFriends;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
FloatingActionButton btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView view = findViewById(R.id.elementsview);
btn = (FloatingActionButton) findViewById(R.id.newfbtn);
view.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
view.setAdapter(new Friendsadapter(this));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(),FriendsAdder.class));
}
});
}
}
my FriendAdder activity
package rex.MyFriends;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class FriendsAdder extends AppCompatActivity {
TextView name,nickname;
Button add ,selectimage;
ImageView image;
String imgpath;
private static final int SELECT_IMAGE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friendsadder);
name = findViewById(R.id.name);
nickname = findViewById(R.id.nickname);
add = findViewById(R.id.add);
selectimage = findViewById(R.id.selimg);
image = findViewById(R.id.disimg);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(name.toString().isEmpty() || nickname.toString().isEmpty() || image.getDrawable()==null){
Toast.makeText(getBaseContext(),"please fill every thing!",Toast.LENGTH_SHORT).show();
}
else{
FriendList.addFriend(name.toString(),nickname.toString(),imgpath);
finish();
Toast.makeText(getBaseContext(),"Friend Added!",Toast.LENGTH_SHORT).show();
}
}
});
selectimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Your Friends Avatar!"), SELECT_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==SELECT_IMAGE && resultCode== Activity.RESULT_OK){
imgpath= data.getData().getPath();
Toast.makeText(getBaseContext(),imgpath,Toast.LENGTH_LONG).show();
image.setImageURI(data.getData());
}
}
}
my adapter class
package rex.MyFriends;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
public class Friendsadapter extends RecyclerView.Adapter<Friendsadapter.FriendsHolder> {
Context context;
public Friendsadapter(Context context) {
this.context = context;
}
#NonNull
#Override
public FriendsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.friendrow,null,false);
return new FriendsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull FriendsHolder holder, int position) {
FriendList.Friend friend = (FriendList.Friend) FriendList.friendlist.get(position);
holder.nickname.setText(friend.nickname);
holder.name.setText(friend.name);
File imgFile = new File(friend.imgpath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.image.setImageBitmap(myBitmap);
}
}
#Override
public int getItemCount() {
return FriendList.friendlist.size();
}
public class FriendsHolder extends RecyclerView.ViewHolder {
TextView name,nickname;
ImageView image;
public FriendsHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
nickname = itemView.findViewById(R.id.nickname);
image = itemView.findViewById(R.id.imageView2);
}
}
}
A helper Class I made with static Array list of Friend object acting as A runtime temporary database to add and remove Friend used in adapter class to display
package rex.MyFriends;
import java.util.ArrayList;
public class FriendList {
public static ArrayList<Friend> friendlist = new ArrayList<>();
public static class Friend {
String name;
String nickname;
String imgpath ;
public Friend(String name, String nickname, String imgpath) {
this.name = name;
this.nickname = nickname;
this.imgpath = imgpath;
}
}
public static void addFriend(String name, String nickname, String imgpath){
Friend friend = new Friend(name,nickname,imgpath);
friendlist.add(friend);
}
public int getSize(){
return friendlist.size();
}
public Friend getFriend(int position){
return friendlist.get(position);
}
}
Where the RecyclerView is handled adapterName.notifyDataSetChanged (); you need to run.
In your case, the data is collected statically. The place where the data is processed is a different Activity.
After switching to the new screen and adding data, you will need to update your adapter in onResume if you want the relevant data to appear when you come back. Recyclerview will appear blank since there is no data in its initial state.
#Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}

problem in open another activity from a custom adapter in java

I need to open another activity from a custom adapter, just like a list and then when I click on that, it will take to another activity and get a data from database and show it in the text view,
the problem is in SearchAdapter.java in the context.startActivity(intent);
here's my searchAdapter.java
package Adapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
//import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.manualdiagnosiskeperawatan.R;
import com.example.manualdiagnosiskeperawatan.isiDiagnosis;
import java.util.List;
import Model.Daftar;
class SearchViewHolder extends RecyclerView.ViewHolder{
public TextView judul, diagnosis;
public SearchViewHolder(View itemView) {
super(itemView);
judul = itemView.findViewById(R.id.judul);
diagnosis = itemView.findViewById(R.id.textViewIsi);
}
}
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder>
{
private Context context;
private List<Daftar> data;
public SearchAdapter(Context context, List<Daftar> data) {
this.context = context;
this.data = data;
}
#Override
public SearchViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.layout_item,parent,false);
return new SearchViewHolder(itemView);
}
#Override
public void onBindViewHolder(final SearchViewHolder holder, final int position)
{
holder.judul.setText(data.get(position).getJudul());
//open activity
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, data.get(position).getDiagnosis(),Toast.LENGTH_SHORT).show();
//this code, isn't working
Intent intent = new Intent(context, isiDiagnosis.class);
intent.putExtra("did",data.get(position).getDid());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
}
and here is my isiDiagnosis.java which will be opened when I clicked the list from the SearchAdapter.java
package com.example.manualdiagnosiskeperawatan;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
import com.mancj.materialsearchbar.MaterialSearchBar;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import Adapter.SearchAdapter;
import Database.Database;
import Model.Daftar;
public class isiDiagnosis extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
Database database;
List<Daftar> daftars = new ArrayList<>();
private TextView judul, diagnosis;
private String DID = "judul";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_isi_diagnosis);
DID = getIntent().getStringExtra("judul");
database = new Database(this);
diagnosis = (TextView) findViewById(R.id.textViewIsi);
//init DB
loadData(DID);
}
private void loadData(String did) {
//i don't really know how to code, just see from the youtube, so i made this myself
// database.getDiagnosis();
// String diagnosis = daftars.get(database.getDiagnosis());
// database.getDiagnosis(diagnosis.setText());
}
}
here's my searchDiagnosis.java
package com.example.manualdiagnosiskeperawatan;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.ListViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Adapter;
import android.widget.ListView;
import com.mancj.materialsearchbar.MaterialSearchBar;
import java.util.ArrayList;
import java.util.List;
import Adapter.SearchAdapter;
import Database.Database;
public class searchDiagnosis extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
Database database;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
#Override
protected void onStart() {
super.onStart();
adapter = new SearchAdapter(getBaseContext(),database.getDaftar());
recyclerView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_diagnosis);
//init view
recyclerView = (RecyclerView) findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar =(MaterialSearchBar) findViewById(R.id.search_bar);
//init DB
database = new Database(this);
//Setup search bar
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
loadSuggestList();
materialSearchBar.addTextChangeListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String> suggest = new ArrayList<>();
for(String search:suggestList){
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
#Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
#Override
public void onSearchStateChanged(boolean enabled) {
if(!enabled) {
// recyclerView.setAdapter(adapter);
//if close Search, just restore default
adapter = new SearchAdapter(getBaseContext(),database.getDaftar());
recyclerView.setAdapter(adapter);
}
}
#Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
#Override
public void onButtonClicked(int buttonCode) {
}
});
//Init adapter default set all result
adapter = new SearchAdapter(this,database.getDaftar());
}
private void startSearch(String text) {
adapter = new SearchAdapter(this,database.getDataByJudul(text));
recyclerView.setAdapter(adapter);
}
private void loadSuggestList() {
suggestList = database.getJudul();
materialSearchBar.setLastSuggestions(suggestList);
}
}
here's my Daftar.class
package Model;
public class Daftar {
public int id;
public String did, judul, diagnosis;
public Daftar(int id,String did, String judul, String diagnosis) {
this.id = id;
this.did = did;
this.judul = judul;
this.diagnosis = diagnosis;
}
public Daftar() {
}
public int getId() {
return id;
}
public String getDid() {
return did;
}
public void setDid(String did) {
this.did = did;
}
public void setId(int id) {
this.id = id;
}
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
public String getDiagnosis() {
return diagnosis;
}
public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
}
}
Look I don't know what kind of database is this, but I know that you are not receiving the intent extra correctly:
In your isiDiagnosis:
............
//get the did like this
DID = getIntent().getStringExtra("did");
.......
loadData(DID);
//loading the data is up to you according to the database
private void loadData(String did) {.........}

Class 'Anonymous class derived from FirebaseAdapter' must be declared abstract in Adapter

I've been getting an error (in my FirebaseRecyclerAdapter) saying that the class must be declared abstract or implement method 'onCreateViewHolder(ViewGroup, int) in 'Adapter', as well as Method does not override superclass on the "Override" just below that code.
However, I have no idea how to change my code to get rid of that error. Can someone please help me? I would really appreciate the help. Thank you!
DiaryActivity.java package com.shiminu1521462c.fyp_2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class DiaryActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference fNotesDatabase;
private RecyclerView mNotesList;
private GridLayoutManager gridLayoutManager;
private DatabaseReference noteRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary);
mNotesList = (RecyclerView) findViewById(R.id.notes_list);
gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
mNotesList.setHasFixedSize(true);
mNotesList.setLayoutManager(gridLayoutManager);
mNotesList.addItemDecoration(new GridSpacingDecoration(2, dpToPx(10), true));
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child(mAuth.getCurrentUser().getUid());
}
loadData();
}
#Override
public void onStart() {
super.onStart();
}
private void loadData() {
Query query = fNotesDatabase.orderByValue();
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(
NoteModel.class,
R.layout.single_note_layout,
NoteViewHolder.class,
query
) {
#Override
protected void populateViewHolder(final NoteViewHolder viewHolder, NoteModel model, int position) {
final String noteId = getRef(position).getKey();
fNotesDatabase.child(noteId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("title") && dataSnapshot.hasChild("timestamp")) {
String title = dataSnapshot.child("title").getValue().toString();
String timestamp = dataSnapshot.child("timestamp").getValue().toString();
viewHolder.setNoteTitle(title);
//viewHolder.setNoteTime(timestamp);
GetTimeAgo getTimeAgo = new GetTimeAgo();
viewHolder.setNoteTime(getTimeAgo.getTimeAgo(Long.parseLong(timestamp), getApplicationContext()));
viewHolder.noteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivity(intent);
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mNotesList.setAdapter(firebaseRecyclerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.diary_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.new_note_button:
Intent newIntent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
startActivity(newIntent);
break;
}
return true;
}
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
}
com.firebase.ui.database.FirebaseRecyclerAdapter is abstract.
If you want to extend it (as you do with an anonymous class in your method loadData()) you have to implement all its abstract methods. In that case apparently onCreateViewHolder(ViewGroup, int).
Add this method implementation to your anonymous class and you are done.

Show Edittext & Textview when checkbox is clicked

I make an application, with CRUD function in Android using Room Database.
When i checked the checkbox like this, Error Pictures 1 i want that application show textview & edittext. But, after i press next button, it won't show textview & edittext based on checked in the checkbox Error Pictures 2 or textview & edittext showed up but that's not based by checkbox isChecked
This is the code :
AdapterTargetIbadah.java
package skripsi.com.ggr.managementibadah.adapter;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import skripsi.com.ggr.managementibadah.R;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;
public class AdapterTargetIbadah extends RecyclerView.Adapter<AdapterTargetIbadah.ViewHolder>{
private ArrayList<Ibadah> targetIbadah;
private Context context;
private AppDatabase db;
public AdapterTargetIbadah(ArrayList<Ibadah> targets, Context ctx) {
targetIbadah = targets;
context = ctx;
db = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_target;
EditText et_Target;
ViewHolder(View itemView) {
super(itemView);
tv_target = itemView.findViewById(R.id.tv_target);
et_Target = itemView.findViewById(R.id.et_Target);
}
}
#Override
public AdapterTargetIbadah.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_target, parent, false);
AdapterTargetIbadah.ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull AdapterTargetIbadah.ViewHolder holder, int position) {
Ibadah iba = targetIbadah.get(position);
if (iba.getTargetIbadah()!=null) {
holder.tv_target.setText(iba.getTargetIbadah());
iba.setEtTarget(holder.et_Target.getText().toString());
} else {
targetIbadah.remove(position);
}
}
#Override
public int getItemCount() {
return targetIbadah.size();
}
}
AdapterCheckboxTarget.java
package skripsi.com.ggr.managementibadah.adapter;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import skripsi.com.ggr.managementibadah.R;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;
public class AdapterCheckboxTarget extends RecyclerView.Adapter<AdapterCheckboxTarget.ViewHolder> {
private ArrayList<Ibadah> targetIbadah;
private Context context;
private AppDatabase db;
public AdapterCheckboxTarget(ArrayList<Ibadah> targets, Context ctx) {
targetIbadah = targets;
context = ctx;
db = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
}
class ViewHolder extends RecyclerView.ViewHolder {
CheckBox cbTarget;
ViewHolder(View itemView) {
super(itemView);
cbTarget = itemView.findViewById(R.id.cbTarget);
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_targethome, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final String name = targetIbadah.get(position).getNamaIbadah();
holder.cbTarget.setText(name);
holder.cbTarget.setChecked(targetIbadah.get(position).getStatus());
holder.cbTarget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (holder.cbTarget.isChecked()) {
targetIbadah.get(position).setStatus(true);
targetIbadah.get(position).setTargetIbadah(targetIbadah.get(position).getNamaIbadah());
Toast.makeText(context, "checklist", Toast.LENGTH_SHORT).show();
} else {
targetIbadah.get(position).setStatus(false);
Toast.makeText(context, "unchecklist", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public int getItemCount() {
return targetIbadah.size();
}
}
TargetHome.java ==> Activity using AdapterTargetIbadah.java
package skripsi.com.ggr.managementibadah;
import android.app.Activity;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import skripsi.com.ggr.managementibadah.adapter.AdapterCheckboxTarget;
import skripsi.com.ggr.managementibadah.adapter.AdapterTargetIbadah;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;
public class TargetHome extends AppCompatActivity {
private AppDatabase db;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Ibadah> targetIbadah;
RecyclerView rvTarget;
Button btSimpan;
Context ctx;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_ibadah);
setTitle("Target Ibadah");
btSimpan = (Button)findViewById(R.id.btSimpanTarget);
targetIbadah = new ArrayList<>();
db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
rvTarget = findViewById(R.id.rvTar);
rvTarget.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
rvTarget.setLayoutManager(layoutManager);
targetIbadah.addAll(Arrays.asList(db.ibadahDAO().selectAllStatus()));
adapter = new AdapterTargetIbadah(targetIbadah, this);
rvTarget.setAdapter(adapter);
Ibadah ib = new Ibadah();
if (ib.getTargetIbadah()!=null) {
ib.setTvTarget(ib.getTargetIbadah());
ib.setEtTarget("");
db.ibadahDAO().insertCapai(ib);
}
btSimpan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(MainActivity.getActIntent(TargetHome.this));
}
});
}
public static Intent getActIntent(Activity activity) {
// kode untuk pengambilan Intent
return new Intent(activity, TargetHome.class);
}
}
CheckboxTarget.java ==> Activity using AdapterCheckboxTarget
package skripsi.com.ggr.managementibadah;
import android.app.Activity;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import skripsi.com.ggr.managementibadah.adapter.AdapterCatatIbadah;
import skripsi.com.ggr.managementibadah.adapter.AdapterCheckboxTarget;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;
public class CheckboxTarget extends AppCompatActivity {
private AppDatabase db;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Ibadah> targetIbadah;
RecyclerView rvCb;
Button btNext;
Context ctx;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_home);
setTitle("Target Ibadah");
btNext = (Button) findViewById(R.id.btNextTarget);
targetIbadah = new ArrayList<>();
db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
rvCb = findViewById(R.id.rvTarHome);
rvCb.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
rvCb.setLayoutManager(layoutManager);
targetIbadah.addAll(Arrays.asList(db.ibadahDAO().selectAllStatus()));
adapter = new AdapterCheckboxTarget(targetIbadah, this);
rvCb.setAdapter(adapter);
final Ibadah ibadah = (Ibadah) getIntent().getSerializableExtra("data");
if (ibadah != null) {
Ibadah ib = new Ibadah();
ib.setStatus(ib.status);
db.ibadahDAO().insertTarget(ib);
}
}
public static Intent getActIntent(Activity activity) {
// kode untuk pengambilan Intent
return new Intent(activity, CheckboxTarget.class);
}
}
Ibadah.java ==> Model
package skripsi.com.ggr.managementibadah.model;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import java.io.Serializable;
#Entity(tableName = "tibadah")
public class Ibadah implements Serializable {
#PrimaryKey(autoGenerate = true)
public int idIbadah;
#ColumnInfo(name = "nama_ibadah")
public String namaIbadah;
#ColumnInfo(name = "deskripsi_ibadah")
public String deskripsiIbadah;
#ColumnInfo(name = "target_ibadah")
public String targetIbadah;
#ColumnInfo(name = "tv_target")
public String tvTarget;
#ColumnInfo(name = "et_target")
public String etTarget;
#ColumnInfo(name = "et_capaian")
public String etCapaian;
#ColumnInfo(name = "status")
public boolean status;
public String getTvTarget() {
return tvTarget;
}
public void setTvTarget(String tvTarget) {
this.tvTarget = tvTarget;
}
public String getTargetIbadah() {
return targetIbadah;
}
public void setTargetIbadah(String targetIbadah) {
this.targetIbadah = targetIbadah;
}
public String getEtTarget() {
return etTarget;
}
public void setEtTarget(String etTarget) {
this.etTarget = etTarget;
}
public String getEtCapaian() {
return etCapaian;
}
public void setEtCapaian(String etCapaian) {
this.etCapaian = etCapaian;
}
public String getDeskripsiIbadah() {
return deskripsiIbadah;
}
public void setDeskripsiIbadah(String deskripsiIbadah) {
this.deskripsiIbadah = deskripsiIbadah;
}
public int getIdIbadah() {
return idIbadah;
}
public void setIdIbadah(int idIbadah) {
this.idIbadah = idIbadah;
}
public String getNamaIbadah() {
return namaIbadah;
}
public void setNamaIbadah(String namaIbadah) {
this.namaIbadah = namaIbadah;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
I hope, you can help me. Thank you very much & sorry for my bad english.
First you have to set your TextView and EditText invisible in your XML, by adding android:visibility="invisible" to the XML of your TextView and EditText.
Then you have to use setVisibility(View.VISIBLE) after you have set the checkbox, like this:
yourcheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
//if checkbox is checked (b == true)
yourTextView.setVisibility(View.VISIBLE);
yourEditText.setVisibility(View.VISIBLE);
}
if (!b) {
//if checkbox is not checked (b == false)
yourTextView.setVisibility(View.INVISIBLE);
yourEditText.setVisibility(View.INVISIBLE);
}
}
});
If you first want to click a button and then check weather checkbox is checked, add this to your OnClickListenter:
if (yourcheckbox.isChecked) {
yourTextView.setVisibility(View.VISIBLE);
yourEditText.setVisibility(View.VISIBLE);
}

Categories