本文介绍了从RecyclerView EditText获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对recyclerView感到震惊,

I am struck in recyclerView,

这里,名称和余额字段来自两个不同的数组.我需要的是,这里的每一行都有一个EditText字段.我需要访问每一行上的每个EditText.并从中获取值..总计将显示在总计textView上.是否有可能?我尝试了很多.我不明白.

Here the name and balance fields are coming from two different arrays.What I need is, here each row has an EditText field. I need to access each EditText on each row. And get values from it.. and a total is displayed on the Total textView. Is it possible? I tried a lot. I didn't get it.

我在这里附上我的课程.

i am attaching my classes here.

MainActivity

public class GroupCollectionFragment extends Fragment {
String[] nameArray = {"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"
        ,"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"
        ,"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"};
String[] balanceArray={"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"
        ,"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"
        ,"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"};
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
LinearLayoutManager mLayoutManager;
List<DataHolder> holderList=new ArrayList<DataHolder>();

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview=inflater.inflate(R.layout.group_collection_layout,container,false);
    mRecyclerView = (RecyclerView) rootview.findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    setItems();
    mAdapter = new Adapter(holderList);
    mRecyclerView.setAdapter(mAdapter);
    return rootview;
}

private void setItems() {

    for(int i=0;i<nameArray.length;i++){
        DataHolder item=new DataHolder();
        item.setDname(nameArray[i]);
        item.setDbalance(balanceArray[i]);
        holderList.add(item);
    }
}

}

DataHolder

public class DataHolder {

String dname,dbalance;
public DataHolder(){
}
public String getDname(){
    return dname;
}
public void setDname(String name){
    this.dname=name;
}
public String getDbalance(){
    return dbalance;
}
public void setDbalance(String balance){
    this.dbalance=balance;
}

}

适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<DataHolder> mDataSet;

public static class ViewHolder extends RecyclerView.ViewHolder{
    private TextView anameTxtView,abalanceTxtView;
    private EditText adepositEditText;
    public ViewHolder(View v){
        super(v);
        anameTxtView=(TextView)v.findViewById(R.id.nameTextView);
        abalanceTxtView=(TextView)v.findViewById(R.id.balanceTextView);
        adepositEditText=(EditText)v.findViewById(R.id.depositEditText);
    }
}

public Adapter(List<DataHolder> myData){
    mDataSet=myData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_main,parent,false);
    ViewHolder vh=new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.anameTxtView.setText(mDataSet.get(position).getDname());
    holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());


}

@Override
public int getItemCount() {
    return mDataSet.size();
}

@Override
public int getItemViewType(int position) {
    return position;
}

}

推荐答案

我认为您正在寻找回调,这意味着每当修改EditText之一上的数字时,您也希望总数也发生变化.所以首先您需要添加一个界面,

I think you are looking for a callback, which means whenever a number on one of the EditTexts is changed you want the total number change too.So first of all you need to add an interface,

OnEditTextChanged接口

public interface OnEditTextChanged {
    void onTextChanged(int position, String charSeq);
}

然后,您也需要在适配器的构造函数中添加它.

Then you need too include this in the constructor of the adapter.

在Adapter.java中

private List<DataHolder> mDataSet;
private OnEditTextChanged onEditTextChanged;

public Adapter(List<DataHolder> myData, OnEditTextChanged onEditTextChanged) {
    mDataSet = myData;
    this.onEditTextChanged = onEditTextChanged;
}

在适配器的onBindViewHolder中,您需要设置一个用于文本更改的侦听器,并使用 onEditTextChanged 对象告诉片段.

In the onBindViewHolder of your Adapter you need to set a listener for text change and tell the fragment using onEditTextChanged object.

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.anameTxtView.setText(mDataSet.get(position).getDname());
    holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());

    holder.adepositEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            onEditTextChanged.onTextChanged(position, charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {}
    });
}

将此数组添加到GroupCollectionFragment中,以便可以将值保存在片段中,并在需要时使用它们.

Add this array to your GroupCollectionFragment so you can save the values in your fragment and use them whenever you want them.

Integer[] enteredNumber = new Integer[1000];

在GroupCollectionFragment中更改构造函数调用

change your constructor call in GroupCollectionFragment

mAdapter = new Adapter(holderList, new OnEditTextChanged() {
        @Override
        public void onTextChanged(int position, String charSeq) {
            enteredNumber[position] = Integer.valueOf(charSeq);
            updateTotalValue();
        }
    });

private void updateTotalValue() {
    int sum = 0;
    for (int i = 0; i < 1000; i++) {
        sum += enteredNumber[i];
    }

    totalValue.setText(String.valueOf(sum));
}

让我知道您是否需要整个文件.我写了它并构建了apk,它工作正常.

Let me know if you want the whole files. I wrote it and built the apk, it works just fine.

这篇关于从RecyclerView EditText获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:17