Need Help in Room Database - android

MyDao Class isn't responding the right way it gives me each time an error , need help to identify problem. Even though it is copies from a youtube class, and i checked the comment if there is something but everybody seems got it right.
public class AddUserFragment extends Fragment {
private EditText UserId,UserName,UserEmail;
private Button Savebn;
public AddUserFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_user, container, false);
UserId=view.findViewById(R.id.txt_user_id);
UserName=view.findViewById(R.id.txt_user_name);
UserEmail=view.findViewById(R.id.txt_user_email);
Savebn=view.findViewById(R.id.bn_save);
Savebn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int userid = Integer.parseInt(UserId.getText().toString());
String username = UserName.getText().toString();
String useremail = UserEmail.getText().toString();
User user = new User();
user.setId(userid);
user.setName(username);
user.setEmail(useremail);
MainActivity.myAppDatabase.myDao().addUser(user);
Toast.makeText(getActivity(),"User added successfully",Toast.LENGTH_SHORT).show();
UserId.setText("");
UserName.setText("");
UserEmail.setText("");
}
});
return view;
}
}

Related

get Editext value in Fragment while inside the Parent Activity method

I have MainActivity in which I call a method to login in a child Fragment called ConnexionFragment. I need to recuperate the email and password from EditTexts located in the Fragment. Can someone help as to how simply explain/do this?
I tried with fragment manager but without success...
MainActivity (Login method)
public void LoginUser(View v){
ConnexionFragment fragment = (ConnexionFragment) manager.findFragmentById(R.id.ConnexionFragment);
courrielStr = fragment.getCourriel();
motDePasseStr = fragment.getMotDePasse();
RequestParams params = new RequestParams();
params.add("type","login");
params.add("courriel", courrielStr);
params.add("mot_de_passe", motDePasseStr);
WebReq.post(context, "index.php", params, new ConnexionResponseHandler());
}
ConnexionFragment
public class ConnexionFragment extends Fragment {
EditText courrielEt, motDePasseEt;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_connexion, container, false);
courrielEt = view.findViewById(R.id.editTextCourrielConnexion);
motDePasseEt = view.findViewById(R.id.editTextPasswordConnexion);
return view;
}
public void onViewCreated(View vue, Bundle savedInstance){
super.onViewCreated(vue,savedInstance);
}
public String getCourriel() {
return courrielEt.getText().toString();
}
public String getMotDePasse() {
return courrielEt.getText().toString();
}
}```

Fragement class inside a recycler view

i know that i can use a recycler view to show a list of item but what if I wanna show a list of existing Fragments?
I'll try to explain better with an example:
In my app I have a fragment called review that's look like this:
public class ReviewView extends Fragment implements ReviewViewContract.View {
private TextView tvContent, tvName,tvDate,tvReactionCounter, tvCommentCountCounter;
private ImageView userPhoto;
private ImageButton btnMore;
private Button btnComment, btnLike;
private PopupMenu popup;
private ReviewViewContract.Presenter presenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.component_review, container, false);
presenter = new ReviewViewPresenter(this,getContext(),getArguments());
initComponent(v);
presenter.getDetail();
return v;
}
//Some view methods...
#Override
public void initComponent(View view) {
userPhoto = view.findViewById(R.id.img_userphoto_profile);
tvName = view.findViewById(R.id.tv_name_profile);
tvDate = view.findViewById(R.id.tvdate_profile);
tvContent = view.findViewById(R.id.content_review_component);
tvReactionCounter = view.findViewById(R.id.tv_reactionCounter);
tvCommentCountCounter = view.findViewById(R.id.tv_commentsCounter);
btnComment = view.findViewById(R.id.btn_comment);
btnLike=view.findViewById(R.id.btn_like);
popup= new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore = view.findViewById(R.id.more_review);
popup = new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore.setOnClickListener(v->popup.show());
}
#Override
public void setDetail(String title, String date, String authorImageURL, String overView) {
tvName.setText("ciao");
}
private void likeBtnListener(){...};
private void commentsBtnListener(){...};
}
Now I need to show an indefinite number of reviewView in my activity, I thought to use a recycler view to do that but I do not want to rewrite all methods of ReviewView in the viewHolder class,(it doesn't seem a smart thing to do) so what should I do?
PS: I know that I can archive this with something like
Review fragment = new ReviewView();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
But this way doesn't seem the best way to do this.

Android - Getting Objects in Multiple RootView

public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
idk if my term is right in the title but here's what im doing...
when I click the 'Add more equipment' it adds layout to my app so
I want to get all the EditText inside the layout i added in the RootView,
I tried setting the text to test it but the first editText of the first rootView is the only one updating. All the codes in my fragment listed above. attached image is the result of this code.
image result
I've done this task by using *RecyclerView as #hjchin suggested. Thank You!
RecyclerView Tutorial # Developer.Android

Android Studio - Update RecyclerView from Dialog Fragment in Menu Item

Hey guys I really need your help. I've spent like 5 days trying to get my recyclerview to update only when the user presses OK on a dialogbox, that appears from the menu actionbar. I've tried every possible method I could think of, every method I've seen on stackoverflow, YouTube, etc. and none of them worked for me.
How do I get the recyclerview in a fragment to update after dialogbox closes? I know there are similar questions regarding updating the menu, and (recyclerviews with dialogfragments), but none of them have a combination.
Out of the countless attempts, the current code configuration posted below isn't causing any errors, however, the recyclerview remains blank. The closest attempt I had to finding a solution, was creating an adapter and setting up the recycler in onOptionsItemSelected. But obviously, it updates only when the user clicks the button, and the initial click would create a blank recyclerview.
Fragment:
(There's a lot of repeated commented code from different attempts)
public class ExerciseRoutine extends Fragment implements ExerciseRoutine_Dialog.RoutineDialogListener{
private String Routine_name, Routine_split;
private ArrayList<ExerciseRoutine_Information> Routine_information = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
public void sendInput(String name, String split, RecyclerView.Adapter DialogAdapter) {
Routine_name = name;
Routine_split = split;
adapter = DialogAdapter;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_fragment, container, false);
//Report that this fragment would like to participate in populating the options menu by
//receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.
//If true, the fragment has menu items to contribute.
setHasOptionsMenu(true);
recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
//BuildRecyclerView();
//recyclerView.setHasFixedSize(true); //If the Recyclerview is static
/*Routine_information.add(new ExerciseRoutine_Information(Routine_name, Routine_split));
recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
//recyclerView.setHasFixedSize(true); //If the Recyclerview is static
layoutManager = new LinearLayoutManager(getActivity());
adapter = new ExerciseRoutineAdapter(Routine_information);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);*/
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.exercise_routine_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_addRoutine:
ExerciseRoutine_Dialog routineDialog = new ExerciseRoutine_Dialog();
routineDialog.setTargetFragment(ExerciseRoutine.this, 1);
routineDialog.show(getFragmentManager(), "Routine Dialog");
//Routine_information.add(new ExerciseRoutine_Information(Routine_name, Routine_split));
BuildRecyclerView();
//adapter.notifyItemInserted(Routine_information.size());
//if(!Routine_name.equals("") && !Routine_split.equals("")) {
//}
}
return super.onOptionsItemSelected(item);
}
public void BuildRecyclerView(){
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
public void BuildAdapter(){
//adapter = new ExerciseRoutineAdapter(getContext(),Routine_information);
adapter.notifyItemInserted(Routine_information.size());
}
}
My Dialog Fragment:
public class ExerciseRoutine_Dialog extends DialogFragment{
private TextView ActionOK, ActionCANCEL;
private EditText Routine_name, Routine_split;
private RoutineDialogListener activityCommander;
private ArrayList<ExerciseRoutine_Information> Routine_information = new ArrayList<>();
private RecyclerView.Adapter adapter;
public interface RoutineDialogListener{
void sendInput(String name, String split, RecyclerView.Adapter DialogAdapter);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander = (RoutineDialogListener) getTargetFragment();
}catch(ClassCastException e){
throw new ClassCastException(context.toString() + "Must Implement RoutineDialogListener");
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_dialog, container, false);
Routine_name = view.findViewById(R.id.ExerciseRoutine_DialogNameInput);
Routine_split = view.findViewById(R.id.ExerciseRoutine_DialogSplitInput);
ActionOK = view.findViewById(R.id.ExerciseRoutine_DialogAction_OK);
ActionCANCEL = view.findViewById(R.id.ExerciseRoutine_DialogAction_CANCEL);
//recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
ActionCANCEL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getDialog().dismiss();
}
});
ActionOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = Routine_name.getText().toString();
String split = Routine_split.getText().toString();
if(!name.equals("") && !split.equals("")) {
Routine_information.add(new ExerciseRoutine_Information(name, split));
adapter = new ExerciseRoutineAdapter(getContext(), Routine_information);
activityCommander.sendInput(name, split, adapter);
adapter.notifyItemInserted(Routine_information.size());
}
getDialog().dismiss();
}
});
return view;
}
}
Your current approach seems to be to pass the RecyclerView.Adapter to the DialogFragment and try to insert the new data on-the-spot. I think this is a problematic setup. The dialog's purpose is to offer some means for the users to enter the required data, period. It should not be tasked with the job of managing the RecyclerView or its Adapter because that way your components will be very tightly coupled:
Imagine that first you use a ListView in your implementation, and suddenly someone decides to ban every ListView from your app (maybe for performance reasons) and has you exchange them all for RecyclerViews. Then your approach would force you to change the code for the DialogFragment (it would have to cater to a different type of Adapter). A more loosely coupled implementation would enable you to make changes to one component without having to rewrite too many others.
That's why I won't try to make your code work as-is, instead I'd like to show you another way:
Because the RecyclerView is part of the Fragment's UI, the Fragment is the place where code related to managing the RecyclerView belongs. It is basically possible to have the Adapter as an inner class of the Fragment but I prefer having it as a standalone class if the code gets a little longer, and also because it enforces "decoupling".
Interfaces play a very important part in good architecture, so the DialogFragment will still make use of an interface to send its data. But it's up to the class which actually implements the interface (here: the Fragment) to pass the data to any interested third parties, e.g. the RecyclerView.Adapter (The Adapter in turn could have its own interface to publish important events like clicks on list items).
Having said that, here are some code snippets:
The DialogFragment
public class ExerciseRoutine_Dialog extends DialogFragment {
private EditText Routine_name, Routine_split;
public interface RoutineDialogListener{
/**
* There is some new ExerciseRoutine_Information
*/
void sendInput(String name, String split);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_dialog, container, false);
Routine_name = view.findViewById(R.id.ExerciseRoutine_DialogNameInput);
Routine_split = view.findViewById(R.id.ExerciseRoutine_DialogSplitInput);
TextView actionOK = view.findViewById(R.id.ExerciseRoutine_DialogAction_OK);
TextView actionCANCEL = view.findViewById(R.id.ExerciseRoutine_DialogAction_CANCEL);
actionCANCEL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getDialog().dismiss();
}
});
actionOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = Routine_name.getText().toString();
String split = Routine_split.getText().toString();
if(!name.equals("") && !split.equals("")) {
// just send the input to the main Fragment
RoutineDialogListener listener = getListener();
if(listener != null) {
listener.sendInput(name, split);
}
}
getDialog().dismiss();
}
});
return view;
}
/**
* Tries to find a suitable listener, examining first the hosting Fragment (if any) and then the Activity.
* Will return null if this fails
* #return x
*/
private RoutineDialogListener getListener(){
RoutineDialogListener listener;
try{
Fragment onInputSelected_Fragment = getTargetFragment();
if (onInputSelected_Fragment != null){
listener = (RoutineDialogListener) onInputSelected_Fragment;
}
else {
Activity onInputSelected_Activity = getActivity();
listener = (RoutineDialogListener) onInputSelected_Activity;
}
return listener;
}catch(ClassCastException e){
Log.e("Custom Dialog", "onAttach: ClassCastException: " + e.getMessage());
}
return null;
}
}
The Fragment:
public class ExerciseRoutine extends Fragment implements ExerciseRoutine_Dialog.RoutineDialogListener{
public static final String ROUTINE_DIALOG = "Routine Dialog";
private ArrayList<ExerciseRoutine_Information> routineInformations = new ArrayList<>();
private RecyclerView.Adapter adapter;
public static ExerciseRoutine instance(){
return new ExerciseRoutine();
}
#Override
public void sendInput(String name, String split) {
routineInformations.add(new ExerciseRoutine_Information(name, split));
adapter.notifyItemInserted(routineInformations.size());
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_fragment, container, false);
setHasOptionsMenu(true);
RecyclerView recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new ExerciseRoutineAdapter(getContext(), routineInformations);
// So far you have a RecyclerView with an empty List.
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.exercise_routine_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_addRoutine:
showDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog(){
ExerciseRoutine_Dialog routineDialog = new ExerciseRoutine_Dialog();
routineDialog.setTargetFragment(ExerciseRoutine.this, 1);
routineDialog.show(getFragmentManager(), ROUTINE_DIALOG);
}
}

Doesnt work my text input layout

I'm a beginner in Android :( I'm using a text input layout but I cant catch any value of them! This is my code. For now I want to print the value "usuario" just to check if it has any value. I'm using Butterknife btw D:
public class IngresoFragment extends Fragment {
#Bind(R.id.tilUsuario) TextInputLayout mTilUsuario;
#Bind(R.id.tilPassword) TextInputLayout mTilPassword;
#Bind(R.id.btnIngresar) Button btnIngresar;
#Bind(R.id.tviLibre) TextView tviLibre;
public IngresoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ingreso, container, false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
app();
}
void app(){
mTilUsuario.setHint(getResources().getString(R.string.usuario));
mTilPassword.setHint(getResources().getString(R.string.password));
events();
}
void events(){
btnIngresar.setOnClickListener(new View.OnClickListener() {
String usuario = mTilUsuario.getEditText().getText().toString().trim();
String password = mTilPassword.getEditText().getText().toString().trim();
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), usuario, Toast.LENGTH_LONG).show();
}
});
}
in Android text input box is define as EditText just chenge TextInputLayout to EditText

Categories

Resources