How Can I store RecylerView With Use SQliteOpenHelper - java

I have recyclerview on my project . and It can work successfuly . I want store my recyclerview with use SQliteOpenHelper but ıf you know another basic storing way then write please I can another storeing way. Can you be fast It's important .
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomSheetDialogx.BottomSheetListener {
Button listeo;
RecyclerView recyclerView;
List<String> Lists = new ArrayList<>();
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todolist);
recyclerView=findViewById(R.id.recyclerviewx);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter=new CustomAdapter(Lists);
recyclerView.setAdapter(adapter);
}
#Override
public void onButtonClick(String text) {
Lists.add(text);
adapter.notifyItemInserted(Lists.size()-1);
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.Holder> {
List<String> Lists;
public CustomAdapter(List<String>itemxx){
this.Lists = itemxx;
}
#NonNull
#Override
public CustomAdapter.Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_item,parent,false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull CustomAdapter.Holder holder, int position) {
holder.textView.setText(Lists.get(position));
}
#Override
public int getItemCount() {
return Lists.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView textView;
public Holder(View view) {
super(view);
textView=view.findViewById(R.id.text_view);
}
}
}

Use Room library instead of SQliteOpenHelper.

Related

Im getting error in startListening() stopListening()

This is the code in which I'm getting errors in startListening() and stopListening():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_app_cart,container,false);
recyclerView = view.findViewById(R.id.recylervieww);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
cartmodelList = new ArrayList<>();
cartadapt = new cartAdapter(getActivity(),cartmodelList);
recyclerView.setAdapter(cartadapt);
FirebaseRecyclerOptions<Cartmodel> options =
new FirebaseRecyclerOptions.Builder<Cartmodel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Cart List"), Cartmodel.class)
.build();
cartadapt = new cartAdapter(options);
recyclerView.setAdapter(cartadapt);
return view;
}
#Override
public void onStart() {
super.onStart();
cartadapt.startListening();
}
#Override
public void onStop() {
super.onStop();
cartadapt.stopListening();
}
}
This is the code for cartAdapter:
public class cartAdapter extends RecyclerView.Adapter<cartAdapter.ViewHolder> {
Context context;
List<Cartmodel> cartmodelList;
public cartAdapter(Context context, List<Cartmodel> cartmodelList) {
this.context = context;
this.cartmodelList = cartmodelList;
}
public cartAdapter(FirebaseRecyclerOptions<Cartmodel> options) {
}
#NonNull
#Override
public cartAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_singlero,parent,false));
}
#Override
public void onBindViewHolder(#NonNull cartAdapter.ViewHolder holder, int position) {
holder.productName.setText(cartmodelList.get(position).getProductName());
holder.quantity.setText(cartmodelList.get(position).getQuantity());
holder.totalPrice.setText(cartmodelList.get(position).getTotalPrice());
Glide.with(holder.itemView.getContext()).load(Cartmodel.productImage()).into(holder.productImage);
}
#Override
public int getItemCount() {
return cartmodelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView date,productName,productPrice,quantity,totalPrice;
ImageView productImage;
public ViewHolder(#NonNull View itemView) {
super(itemView);
productName = itemView.findViewById(R.id.pnametext);
quantity = itemView.findViewById(R.id.quantity);
totalPrice = itemView.findViewById(R.id.totalprice);
productImage = itemView.findViewById(R.id.img2);
}
}
}
I'm getting these errors:
Cannot resolve method 'startListening' in 'cartAdapter'
Cannot resolve method 'stopListening' in 'cartAdapter'
I'm using both classes in another activity but in those, I'm not getting any errors.
I'm getting error in startListening() and stopListening()
Because those methods are not present inside RecyclerView.Adapter class. Your adapter class should extend FirebaseRecyclerAdapter class instead in order to be able to call startListening() and stopListening().
For more info, please check the official documentation:
https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/

How to send data from RecyclerView to edit text in the same activity using adapter? (Android)

I have a problem in my code, I want to send the data I clicked from the RecyclerView to edit text in the same activity
My adapter:
public class RecyclerViewAdapter1 extends RecyclerView.Adapter<RecyclerViewAdapter1.ViewHolder> {
private Context context;
private List<Result> results;
public RecyclerViewAdapter1(Context context, List<Result> results) {
this.context = context;
this.results = results;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.arraylist1, parent,false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result result = results.get(position);
holder.textViewid.setText(result.getRef_desa_id());
holder.textViewNm_desa.setText(result.getNama_desa());
}
#Override
public int getItemCount() {
return results.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
#BindView(R.id.txtid)
TextView textViewid;
#BindView(R.id.txtnm_desa)
TextView textViewNm_desa;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
#Override
public void onClick(View v) {
String id = textViewid.getText().toString();
Intent i = new Intent(context, DaftarDukaActivity2.class);
((Activity)context).finish();
i.putExtra("id", id);
context.startActivity(i);
}
}
}
In the code above, my coding has succeeded in displaying data to another Edit text activity. I want to ask, how to send data from the RecyclerView I clicked to EditText in the same activity?
Do you want editText.setText() from reacyclerview adapter? You can do like this:
public class RecyclerViewAdapter1 extends RecyclerView.Adapter<RecyclerViewAdapter1.ViewHolder> {
private Context context;
private List<Result> results;
private EditText editText;
public RecyclerViewAdapter1(Context context, List<Result> results,EditText editText) {
this.context = context;
this.results = results;
this.editText=editText;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.arraylist1, parent,false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result result = results.get(position);
holder.textViewid.setText(result.getRef_desa_id());
holder.textViewNm_desa.setText(result.getNama_desa());
}
#Override
public int getItemCount() {
return results.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
#BindView(R.id.txtid)
TextView textViewid;
#BindView(R.id.txtnm_desa)
TextView textViewNm_desa;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
#Override
public void onClick(View v) {
String id = textViewid.getText().toString();
editText.setText(id);
}
}
}
Usage
public class MainActivity extends AppCompatActivity{
private Context context=this;
private List<Result> results;
private EditText editText;
private RecyclerView rec;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.my_edit_text_id_in_xml);
rec=findViewById(R.id.my_recycler_view_id);
// add items to your result List
rec.setAdapter(new RecyclerViewAdapter1(context,results,editText));
rec.setLayoutManager(new LinearLayoutManager(context));
}
}

Multiple recyclerviews using one adapter

I have an app where I want to use recyclerviews in different parts of the application and I was thinking of using the same adapter for the same two (or more) recyclerviews, I was thinking this would be a good practice, or am I mistaken and better off making a new adapter for every recyclerview in the app?
Here is the code for my adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder> {
private ArrayList<Task> list;
//This is the list of the second recyclerview which I want to implement
private ArrayList<String> categories;
private OnItemListener mOnItemListener;
RecyclerViewAdapter(ArrayList<Task> list, OnItemListener onItemListener)
{
this.list = list;
this.mOnItemListener = onItemListener;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent, false);
return new MyViewHolder(textView, mOnItemListener);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(list.get(position).getTaskName());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView textView;
OnItemListener onItemListener;
MyViewHolder(#NonNull View itemView, OnItemListener onItemListener) {
super(itemView);
textView = itemView.findViewById(R.id.singleView);
this.onItemListener = onItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
onItemListener.onItemClick(getAdapterPosition());
}
}
public interface OnItemListener
{
void onItemClick(int position);
}
}
So how would I go about doing this so that it can switch depending on which recyclerview in the app is setting/initializing the adapter, so that the getItemCount for example, would return the size of categories instead of list and so on.

My recycler view not shown for the first time in fragment

I am a beginner at android development. I tried to display data on Recycle View using MVVM. When I opened the fragment the first time it is empty, the second time it showed data. Adapter not called the first time. How to fix this.
Fragment class
public class fragmentEmployee extends Fragment {
private FragmentEmployeeBinding binding;
private ViewGroup viewGroup;
private View view;
private RecyclerView mRecycleView;
private UserAdapter mAdapter;
private EmployeeRegisterViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_employee, viewGroup, false);
view = binding.getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getContext(), "onViewCreated", Toast.LENGTH_SHORT).show();
mRecycleView = binding.recyleViewEmployee;
viewModel = new ViewModelProvider(this).get(EmployeeRegisterViewModel.class);
viewModel.init();
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
}
private void initRecycleView() {
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecycleView.setLayoutManager(layoutManager);
mRecycleView.setAdapter(mAdapter);
}
}
UserAdapter class
public class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
public List<employeeModel> employeeModelList= new ArrayList<>();
public Context context1;
public UserAdapter(Context context,List<employeeModel> employeeModels){
employeeModelList = employeeModels;
context1 = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
(ViewHolder)holder).username.setText(employeeModelList.get(position).getName());
}
#Override
public int getItemCount() {
return employeeModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView username;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.userText);
}
}
}
Try reordering like this.
Remove
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
from initRecycleView
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
initRecycleView();
mAdapter.notifyDataSetChanged();
Above method will work if the list declared inside adapter has any change. So, calling above method without changing adapter's list is useless.
Add a method to UserAdapter class to update list data. Like:
public void updateData(List<employeeModel> updatedList){
employeeModelList.clear();
employeeModelList.addAll(updatedList);
notifyDataSetChanged();
}
And call this method inside observer. Like:
viewModel.getEmployee().observe(getViewLifecycleOwner(), new
Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.updateData(employeeModels);
}
});
where are you get your network data?
If your get in here -> viewModel.init();
I think it just timing problem.
Just change code sequence
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
viewModel.init();

How to get http://jsonplaceholder.typicode.com/users with RecyclerView

I want to pull data http://jsonplaceholder.typicode.com/users but my code only get one person(name: Leanne Graham,username: Bret) I want to get all users. I create ServiceGenerator.java Call> placeHolderModel(); maybe this code may error.
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView ;
RecyclerView recyclerView ;
SimpleRecyclerAdapter simpleRecyclerAdapter ;
ArrayList<Person> arrayList ;
private LinearLayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
arrayList=new ArrayList<Person>();
Factory.getInstance().placeHolderModel().enqueue(new Callback<List<PlaceHolderModel>>() {
#Override
public void onResponse(Call<List<PlaceHolderModel>> call, Response<List<PlaceHolderModel>> response) {
textView.setText(response.body().get(0).name);
simpleRecyclerAdapter= new SimpleRecyclerAdapter(response.body());
recyclerView.setAdapter(simpleRecyclerAdapter);
}
#Override
public void onFailure(Call<List<PlaceHolderModel>> call, Throwable t) {
}
});
}
}
SimpleRecyclerAdapter.java
public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleRecyclerAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView isim ;
public TextView soyIsim ;
public CardView cardView ;
public ViewHolder(View itemView) {
super(itemView);
isim = itemView.findViewById(R.id.textIsım);
soyIsim=itemView.findViewById(R.id.textSoyIsim);
cardView = itemView.findViewById(R.id.cardView);
}
}
List<PlaceHolderModel> placeHolderModel;
public SimpleRecyclerAdapter(List<PlaceHolderModel> placeHolderModel) {
this.placeHolderModel = placeHolderModel;
}
#Override
public SimpleRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_custom,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder ;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.isim.setText(placeHolderModel.get(position).name);
holder.soyIsim.setText(placeHolderModel.get(position).username);
}
#Override
public int getItemCount() {
return placeHolderModel.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
ServiceGenerator.java
public interface ServiceGenerator {
#GET("/users")
Call<List<PlaceHolderModel>> placeHolderModel();
}
Thank you very much !
Your advice important for me!

Categories

Resources