4

在 android app 上工作,我想从我的 textview 中传递值,该值位于 listview 的一行中,并将其放在一个片段中。我听说 convertView.gettag 和 settag 方法放在我不知道如何处理。我的代码:

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;
        }
}
4

1 回答 1

1

你做得很好,你需Bundle要这样做。

首先创建一个String可以保持价值的地方

holder.item_evaluation_nom.getText();

然后例如你这样做:

String itemEvaluation = holder.item_evaluation_nom.getText();

这是正确的Bundle

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

然后要检索这个,你这样做:

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

希望能帮助到你。

编辑

您需要将一个添加Adapter到您的evaluations_historic_liste列表中。

你可以这样做:

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

mEvaluationList你必须把数据放在上面。

然后在你ListView添加这个:

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());
 }
}
于 2015-12-22T10:56:10.623 回答