Hello I am new on Dagger2.I want to create an object in the ExpenseFragment that has all the dependencies of the constructor of the module including the 3 string objects(type,note,post_key).But i get an error of multiple bindings due to the fact that i have these 3 strings.
I have tried different things like separating them with the @Named annotation but i get confused.
ExpenseFragmentModule.java
package georgiosApp.manager.ExpenseFragment;
import android.app.Activity;
import android.content.Context;
import com.google.firebase.database.DatabaseReference;
import javax.inject.Named;
import dagger.Module;
import dagger.Provides;
@Module
public class ExpenseFragmentModule {
Activity activity;
Context context;
public String type;
public String note;
public int amount;
public String post_key;
public DatabaseReference mExpenseDatabase;
public ExpenseFragmentModule(Context context, Activity activity,
DatabaseReference mExpenseDatabase, String type, String note, String post_key, Integer amount) {
this.activity = activity;
this.context = context;
this.mExpenseDatabase = mExpenseDatabase;
this.type = type;
this.note = note;
this.post_key = post_key;
this.amount = amount;
}
@Provides
String providetype1(){
return type;
}
@Provides
String providenote1(){
return note;
}
@Provides
String providepost1(){
return post_key;
}
@Provides
Integer provideamount1(){ return amount; }
@Provides
DatabaseReference providedata1(){
return mExpenseDatabase;
}
@Provides
Context provideContext1(){ return context;}
@Provides
Activity provideActivity1(){ return activity; }
}
ExpenseFragment.java
package georgiosApp.manager.ExpenseFragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Fragment;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.ValueEventListener;
import javax.inject.Inject;
import javax.inject.Named;
import georgiosApp.manager.Model.Data;
import georgiosApp.manager.R;
/**
* A simple {@link Fragment} subclass.
*/
public class ExpenseFragment extends Fragment implements
ExpenseFragmentContract.View{
//Firebase database..
private FirebaseAuth mAuth;
private DatabaseReference mExpenseDatabase;
@Inject
ExpensePresenter xp;
//Recyclerview..
private RecyclerView recyclerView;
private TextView expenseSumResult;
//Edt data item;
private EditText edtAmmount;
private EditText edtType;
private EditText edtNote;
private Button btnUpdate;
private Button btnDelete;
//Data variable..
private String type;
private String note;
private int ammount;
private String post_key;
public ExpenseFragmentComponent component;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myview=inflater.inflate(R.layout.fragment_expense, container,
false);
mAuth=FirebaseAuth.getInstance();
FirebaseUser mUser=mAuth.getCurrentUser();
String uid=mUser.getUid();
mExpenseDatabase=
FirebaseDatabase.getInstance().
getReference().child("ExpenseDatabase").child(uid);
expenseSumResult=myview.findViewById(R.id.expense_txt_result);
recyclerView=myview.findViewById(R.id.recycler_id_expense);
LinearLayoutManager layoutManager=new
LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
//component=DaggerExpenseFragmentComponent
mExpenseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int expesnSum=0;
for (DataSnapshot mysanapshot:dataSnapshot.getChildren()){
Data data=mysanapshot.getValue(Data.class);
expesnSum+=data.getAmount();
String strExpensesum=String.valueOf(expesnSum);
expenseSumResult.setText(strExpensesum+".00");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return myview;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Data,MyViewHolder>adapter=new
FirebaseRecyclerAdapter<Data, MyViewHolder>
(
Data.class,
R.layout.expense_recycler_data,
MyViewHolder.class,
mExpenseDatabase
) {
@Override
protected void populateViewHolder(MyViewHolder viewHolder, final
Data model, final int position) {
viewHolder.setDate(model.getDate());
viewHolder.setType(model.getType());
viewHolder.setNote(model.getNote());
viewHolder.setAmmount(model.getAmount());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
post_key=getRef(position).getKey();
type=model.getType();
note=model.getNote();
ammount=model.getAmount();
xp=new
ExpensePresenter(getContext(),getActivity(),
mExpenseDatabase,type,note,post_key,ammount);
xp.updateDataItem();
//updateDataItem();
}
});
}
};
recyclerView.setAdapter(adapter);
}
private static class MyViewHolder extends RecyclerView.ViewHolder{
View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
private void setDate(String date){
TextView mDate=mView.findViewById(R.id.date_txt_expense);
mDate.setText(date);
}
private void setType(String type){
TextView mType=mView.findViewById(R.id.type_txt_expense);
mType.setText(type);
}
private void setNote(String note){
TextView mNote=mView.findViewById(R.id.note_txt_expense);
mNote.setText(note);
}
private void setAmmount(int ammount){
TextView mAAmmount=mView.findViewById(R.id.ammount_txt_expense);
String strammount=String.valueOf(ammount);
mAAmmount.setText(strammount);
}
}
/*
private void updateDataItem(){
AlertDialog.Builder mydialog=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=LayoutInflater.from(getActivity());
View myview=inflater.inflate(R.layout.update_data_item,null);
mydialog.setView(myview);
edtAmmount=myview.findViewById(R.id.ammount_edt);
edtNote=myview.findViewById(R.id.note_edt);
edtType=myview.findViewById(R.id.type_edt);
edtType.setText(type);
edtType.setSelection(type.length());
edtNote.setText(note);
edtNote.setSelection(note.length());
edtAmmount.setText(String.valueOf(ammount));
edtAmmount.setSelection(String.valueOf(ammount).length());
btnUpdate=myview.findViewById(R.id.btn_upd_Update);
btnDelete=myview.findViewById(R.id.btnuPD_Delete);
final AlertDialog dialog=mydialog.create();
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
type=edtType.getText().toString().trim();
note=edtNote.getText().toString().trim();
String stammount=String.valueOf(ammount);
stammount=edtAmmount.getText().toString().trim();
int intamount=Integer.parseInt(stammount);
String mDate= DateFormat.getDateInstance().format(new Date());
Data data=new Data(intamount,type,note,post_key,mDate);
mExpenseDatabase.child(post_key).setValue(data);
dialog.dismiss();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mExpenseDatabase.child(post_key).removeValue();
dialog.dismiss();
}
});
dialog.show();
}
*/
}
ExpenseFragmentComponent.java
package georgiosApp.manager.ExpenseFragment;
import dagger.Component;
@Component(modules = ExpenseFragmentModule.class)
public interface ExpenseFragmentComponent {
void inject(ExpenseFragment expenseFragment);
}
ExpensePresenter.java
package georgiosApp.manager.ExpenseFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.firebase.database.DatabaseReference;
import java.text.DateFormat;
import java.util.Date;
import javax.inject.Inject;
import georgiosApp.manager.Model.Data;
import georgiosApp.manager.R;
public class ExpensePresenter implements ExpenseFragmentContract.Presenter
{
Activity activity;
public EditText edtAmmount;
public EditText edtType;
public EditText edtNote;
Context context;
public String type;
public String note;
public int amount;
public String post_key;
Button btnUpdate,btnDelete;
public DatabaseReference mExpenseDatabase;
@Inject
public ExpensePresenter(Context context, Activity
activity,DatabaseReference mExpenseDatabase,
String type,String note,String post_key,Integer
amount){
this.activity=activity;
this.context=context;
this.mExpenseDatabase=mExpenseDatabase;
this.type=type;
this.note=note;
this.post_key=post_key;
this.amount=amount;
}
public void updateDataItem(){
AlertDialog.Builder mydialog=new AlertDialog.Builder(activity);
LayoutInflater inflater=LayoutInflater.from(activity);
View myview=inflater.inflate(R.layout.update_data_item,null);
mydialog.setView(myview);
edtAmmount=myview.findViewById(R.id.ammount_edt);
edtNote=myview.findViewById(R.id.note_edt);
edtType=myview.findViewById(R.id.type_edt);
edtType.setText(type);
edtType.setSelection(type.length());
edtNote.setText(note);
edtNote.setSelection(note.length());
edtAmmount.setText(String.valueOf(amount));
edtAmmount.setSelection(String.valueOf(amount).length());
btnUpdate=myview.findViewById(R.id.btn_upd_Update);
btnDelete=myview.findViewById(R.id.btnuPD_Delete);
final AlertDialog dialog=mydialog.create();
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
type=edtType.getText().toString().trim();
note=edtNote.getText().toString().trim();
String stammount=String.valueOf(amount);
stammount=edtAmmount.getText().toString().trim();
int intamount=Integer.parseInt(stammount);
String mDate= DateFormat.getDateInstance().format(new Date());
Data data=new Data(intamount,type,note,post_key,mDate);
mExpenseDatabase.child(post_key).setValue(data);
dialog.dismiss();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mExpenseDatabase.child(post_key).removeValue();
dialog.dismiss();
}
});
dialog.show();
}
}
Aucun commentaire:
Enregistrer un commentaire