gpt4 book ai didi

android - 从 RecyclerView EditText 获取值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:09 24 4
gpt4 key购买 nike

我对 recyclerView 很感兴趣,

image

这里的 name 和 balance 字段来自两个不同的数组。我需要的是,这里每一行都有一个 EditText 字段。我需要访问每一行的每个 EditText。并从中获取值.. 总计显示在 Total textView 上。可能吗?我试了很多。我没听懂。

我在这里附上我的类(class)。

主 Activity

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

数据持有者

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 上的数字发生变化时,您也希望总数发生变化。所以首先你需要添加一个接口(interface),

OnEditTextChanged 界面

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

然后您还需要将其包含在适配器的构造函数中。

在 Adapter.java 中

private List<DataHolder> mDataSet;
private OnEditTextChanged onEditTextChanged;

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

在适配器的 onBindViewHolder 中,您需要为文本更改设置一个监听器,并使用 onEditTextChanged 对象告诉 fragment 。

@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,以便您可以将值保存在 fragment 中并在需要时随时使用它们。

Integer[] enteredNumber = new Integer[1000];

更改 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,它工作得很好。

关于android - 从 RecyclerView EditText 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com