如何将文本视图中的值从列表视图的一行中放入片段

How to put values from a textview in a row of a listview to a fragment

正在开发 android 应用程序',我想从列表视图的一行中的文本视图传递值并将其放入片段中。我听说 convertView.gettag 和 settag 方法 put 我不知道如何处理。 我的代码:

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

你做得很好,你需要 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(新评估适配器(getActivity(),mEvaluation));

mEvaluation 是你必须在其上放置数据的 List

然后在您的 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());
 }
}