我在recyclerView中卡住了,
这里的名称和余额字段来自两个不同的数组。 我需要的是,每一行都有一个EditText字段。我需要访问每一行上的每个EditText。并从中获取值..并在Total textView上显示总数。这可能吗?我尝试了很多次。但没有成功。
我在这里附上我的类。
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);
}
}
}
数据持有者
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;
}
}
//高级for循环 for (int num : enteredAmount) { sum = sum + num; } textViewRemaining.setText(String.valueOf(sum));
} - Goodlife