在RecycleView
右侧Edit_text
,Edit_text
当用户更改Edit_text
中的虚拟数据时,即使列表行有效,也应保存在Edit_Text
中从视图上/下或通过按开关按钮用户转到其他视图。我想在RecycleView
为此,我实现了RecycleView
,Adapter类并开发了一个Simple Loader类。
我从Loader类获取数据到我的Edit_Text
类并将数据发送到适配器类。
从Adapter类Edit_Text获取数据后,我将数据发送回Loader Class进行保存。
但是当我更改RecycleView
中的数据时,它无法保存。请合作。
OnCreate
班级 // Dummy Data
String[] mobileArray = {"Android", "Skype", "mail", "twitter", "What's app", "facebook", "Windows7", "Max OS X", "Android", "IPhone", "WindowsMobile"};
int[] programImages = { R.drawable.ic_phone,R.drawable.ic_skype,R.drawable.ic_email, R.drawable.ic_twitter, R.drawable.ic_whatsapp, R.drawable.ic_facebook,
R.drawable.check_icon, R.drawable.check_icon, R.drawable.check_icon, R.drawable.check_icon, R.drawable.check_icon};
String[] data = {"Android", "Skype", "mail", "twitter", "What's app", "Ubuntu", "Windows7","koki","Andreas","Niko","Filip"};
//Getting EditText data From Loader class
String [] numbers=new DataLoader().getFirstdata();
String [] statics=new DataLoader().getSeconddata();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//Recycle View
final RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(new CustomRecycleAdapter(programImages,mobileArray,numbers));
//In Switch I am changing the data in Adapter
mySwitch.setChecked(true);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
recyclerView.setAdapter(new CustomRecycleAdapter(programImages,mobileArray,numbers));
Toast.makeText(getApplicationContext(), "Switch is checked", Toast.LENGTH_LONG).show();
} else {
recyclerView.setAdapter(new CustomRecycleAdapter(programImages,data,statics));
Toast.makeText(getApplicationContext(), "Switch is not checked", Toast.LENGTH_LONG).show();
}
}
});
}
}
方法:
public class CustomRecycleAdapter extends RecyclerView.Adapter<CustomRecycleAdapter.MyViewHolder> {
String [] result;
int [] imageId;
String [] numbers;
Context context;
// Loader object
DataLoader loader=new DataLoader();
LayoutInflater inflater=null;
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView image;
EditText editText;
TextView title;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.label);
image=(ImageView) view.findViewById(R.id.profile_list_icon);
editText=(EditText) view.findViewById(R.id.number);
}
}
public CustomRecycleAdapter(int[] programImages, String[] mobileArray, String[] numbers) {
// this.context=context;
imageId=programImages;
result=mobileArray;
this.numbers=numbers;
}
@Override
public CustomRecycleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_profile_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomRecycleAdapter.MyViewHolder holder, int position) {
holder.title.setText(result[position]);
holder.editText.setText(numbers[position]);
holder.image.setImageResource(imageId[position]);
// Loader getting Edited Text
loader.setFirstdata(position,holder.editText.getText().toString());
numbers[position]=holder.editText.getText().toString();
}
@Override
public int getItemCount() {
return result.length;
}
}
适配器类:
public class DataLoader {
String[] numbers = {"+4312345678", "aaaaa", "bbbbbbbbbbb", "+9255433654", "567567", "342245", "6545645", "456546", "123123", "324234", "1455433654"};
String[] statics = {"+4312345678", "ddddddddd", "kkkkkkkkkkk", "+9255433654", "567567", "0098", "+=0909", "++8883","+4312345678", "ssssss", "tttttttttttt"};
public void setFirstdata(int position, String data){
numbers[position]=data;
}
public void setSecondData(int position, String data){
statics[position]=data;
}
public String[] getFirstdata(){
return numbers;
}
public String[] getSeconddata(){
return statics;
}
}
我的装载机班级:
{{1}}
答案 0 :(得分:0)
@Shahek 有两个提示,首先不要创建三种类型的数组,只需创建一个用于存储数据的模型类。
public void YourModelClass {
String result;
int imageId;
String numbers;
public YourModelClass(String result,int imageId,String number){
this.result = result;
this.imageId = imageId;
this.number = number
}
}
并创建一个List<YourModelClass>
,然后您的Adapter Constructor看起来像
public CustomRecycleAdapter(List<YourModelClass> yourModelList) {
this.yourModelList = yourModelList;
}
和
创建一个单独的方法来更新editText
,每当您更新字段时,您必须notifyAdapter
未调用
private void updateListItemAtPos(int position){
loader.setFirstdata(position,holder.editText.getText().toString());
numbers[position]=holder.editText.getText().toString();
}
您只需更新List项目并调用notify,RecyclerView会自动保存已编辑的文本