4

Working on android app' and I want to pass value from my textview that is in a row of a listview and put it in a fragment. I hear about convertView.gettag and settag method put I don't know how to deal with. My code:

public class EvaluationsFragment extends CustomFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {

private Patient mPatient;
private View myView;
private Context context;
private Button btnAjoutEvaluation;
private ListView evaluations_historic_liste;
private List<Evaluation>mEvaluation;
private EvaluationDto mEvaluationDto;







@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
   myView = inflater.inflate(R.layout.fragment_evaluations, container,false);
   return myView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    init();

}

private void init(){

    evaluations_historic_liste = (ListView)myView.findViewById(R.id.evaluations_historic_liste);
    btnAjoutEvaluation = (Button)myView.findViewById(R.id.evaluations_add__btn);
    btnAjoutEvaluation.setOnClickListener(this);
    TypeEvaluation mNouveauTypeEvaluation;
    mPatient =((DossierActivity)mRootActivity).mPatient;
    mEvaluationDto = new EvaluationDto(mRootActivity.getApplicationContext());
    evaluations_historic_liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EvaluationAdapter.EvaluationItem item =  (EvaluationAdapter.EvaluationItem)view.getTag();
            openDetailEvaluation(item);
        }
    });
    mEvaluationDto.open();
    try{
        mEvaluation = mEvaluationDto.getEvaluationsByPatientId(mPatient.getId());
    }catch (SQLException e) {
        Log.e(Constants.APP_LOG_TAG, e.getMessage());
        ACRA.getErrorReporter().handleException(e);
    } finally{
        mEvaluationDto.close();
    }

    if(mEvaluation != null && mEvaluation.size() >0){
        evaluations_historic_liste.setAdapter(new EvaluationAdapter(mRootActivity,mEvaluation));
    }else {
        evaluations_historic_liste.setVisibility(View.GONE);
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case (R.id.evaluations_add__btn):
            openNewEvaluation();
            break;
    }

}

public void openNewEvaluation(){
    Fragment fragment = new EvaluationNouvelleFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, new EvaluationNouvelleFragment());
    fragmentTransaction.commit();

}

public void openDetailEvaluation(EvaluationAdapter.EvaluationItem item){
    EvaluationAdapter evaluationAdapter = new EvaluationAdapter();
    EvaluationDetailHistoriqueFragment detail = new EvaluationDetailHistoriqueFragment();
    Bundle args = new Bundle();

    args.putString(EvaluationDetailHistoriqueFragment.DATA_RECEIVE, /*HERE I NEED TO PUT item_evaluation_nom.setText()*/  );
    detail.setArguments(args);
    getFragmentManager().beginTransaction().replace(R.id.content_frame, detail).commit();

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

public class EvaluationAdapter extends BaseAdapter {

    private Context mcontext;
    private List<EvaluationItem> mItems = new ArrayList<EvaluationItem>();

    public EvaluationAdapter(){}
    public EvaluationAdapter(Context context, List<Evaluation> values) {
        this.mcontext = context;
        for (Evaluation e : values) {
            mItems.add(new EvaluationItem(e));
        }

    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public EvaluationItem getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        EvaluationItemHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.list_items_historic_evaluations, parent, false);
            holder = new EvaluationItemHolder();

            holder.item_evaluation_date = (TextView) convertView.findViewById(R.id.item_evaluation_date);
            holder.item_evaluation_nom = (TextView) convertView.findViewById(R.id.item_evaluation_nom);
            holder.item_evaluation_personnel = (TextView) convertView.findViewById(R.id.item_evaluation_personnel);
            holder.item_evaluation_score = (TextView) convertView.findViewById(R.id.item_evaluation_score);
            holder.item_evaluation_date_reevaluation = (TextView) convertView.findViewById(R.id.item_evaluation_date_reevaluation);

            convertView.setTag(holder);
        }else{
            holder = (EvaluationItemHolder)convertView.getTag();
        }
         final EvaluationItem item = getItem(position);
         int score = (item.getScoreTV()).intValue();

         holder.item_evaluation_date.setText( + " " + item.getDateTV());
         holder.item_evaluation_nom.setText(item.getTypeEvalTV());
         if (item.getPersonnelTV() != null) {
                holder.item_evaluation_personnel.setText( + " " + item.getPersonnelTV());
            }
         holder.item_evaluation_score.setText( + String.valueOf(score));
         if (item.getDateReevalTV() != null) {
                holder.item_evaluation_date_reevaluation.setText( item.getDateReevalTV());
            }

    //   convertView.setTag(1,item.getTypeEvalTV());
        return convertView;
    }

    public class EvaluationItem {
        private String dateTV;
        private String typeEvalTV;
        private String personnelTV;
        private Double scoreTV;
        private String dateReevalTV;

        public String getDateTV() {return dateTV;}

        public void setDateTV(String dateTV) {
            this.dateTV = dateTV;
        }

        public String getTypeEvalTV() {
            return typeEvalTV;
        }

        public void setTypeEvalTV(String typeEvalTV) {
            this.typeEvalTV = typeEvalTV;
        }

        public String getPersonnelTV() {
            return personnelTV;
        }

        public void setPersonnelTV(String personnelTV) {
            this.personnelTV = personnelTV;
        }

        public Double getScoreTV() {
            return scoreTV;
        }

        public void setScoreTV(Double scoreTV) {
            this.scoreTV = scoreTV;
        }

        public String getDateReevalTV() {
            return dateReevalTV;
        }

        public void setDateReevalTV(String dateReevalTV) {
            this.dateReevalTV = dateReevalTV;
        }

        public EvaluationItem(Evaluation e){

            this.dateTV = e.getDate();
            this.typeEvalTV = e.getTypeEvaluation().getLibelle();
            this.personnelTV = e.getPersonnelLibelle();
            this.scoreTV = e.getScore();
            this.dateReevalTV = e.getDateReevaluation();

        }

    }
}
        public static class EvaluationItemHolder{
            public TextView item_evaluation_date;
            public TextView item_evaluation_nom;
            public TextView item_evaluation_personnel;
            public TextView item_evaluation_score;
            public TextView item_evaluation_date_reevaluation;
        }
}
Skizo-ozᴉʞS
  • 16,233
  • 16
  • 66
  • 129
BarryF
  • 89
  • 6

1 Answers1

1

You were doing good, you need a Bundle to do this.

First of all create a String where you can keep the value of

holder.item_evaluation_nom.getText();

Then for example you do this :

String itemEvaluation = holder.item_evaluation_nom.getText();

This is the correct Bundle

EvaluationDetailHistoriqueFragment detail = new EvaluationDetailHistoriqueFragment();
Bundle bundle = new Bundle();
bundle.putString("text", itemEvaluation);
detail.setArguments(bundle);

Then to retrieve this you do this :

Bundle bundle = this.getArguments();
if (bundle != null) {
   String itemEvaluation = bundle.getString("text", "");
}

hope it helps.

EDIT

You'll need to add an Adapter to your evaluations_historic_liste list.

You can do this doing :

evaluations_historic_liste.setAdapter(new EvaluationAdapter(getActivity(),mEvaluation));

mEvaluation is the List that you have to put data on it.

Then in your ListView add this :

evaluation_historic_liste.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id){ 
  String itemEvaluation = (String) ((TextView)view.findViewById(R.id.item_evaluation_nom)).getText();

  //or

 String itemEvaluation=(evaluation_historic_liste.getItemAtPosition(position).toString());
 }
}
Skizo-ozᴉʞS
  • 16,233
  • 16
  • 66
  • 129
  • If I create String itemEvaluation = holder.item_evaluation_nom.setText(item.getTypeEvalTV()); in my method public void openDetailEvaluation(EvaluationAdapter.EvaluationItem item) android studio cannot resolve symbol holder – BarryF Dec 22 '15 at 11:02
  • I misstyped it's `String itemEvaluation = holder.item_evaluation_nom.getText();` – Skizo-ozᴉʞS Dec 22 '15 at 11:03
  • there is the same problem with holder – BarryF Dec 22 '15 at 11:04
  • Oh, now I understand your problem.... but do you need something to get that even like `onClickListener` on your ListView or something – Skizo-ozᴉʞS Dec 22 '15 at 11:09
  • I need onClickListener for another function in my app' – BarryF Dec 22 '15 at 11:13
  • Is this a ListView? I can give you a sample to get the text when you click on your item that you want to pass within your Fragment, but I don't know if it's what you want – Skizo-ozᴉʞS Dec 22 '15 at 11:20
  • Sorry I didn't understand :-) yes this is exactly what I want to do – BarryF Dec 22 '15 at 11:21
  • You're welcome if my help helped to you feel free to upvote and mark this answer as a correct one ;) – Skizo-ozᴉʞS Dec 22 '15 at 13:03