2016-08-11 109 views
0

enter image description hereRecycleView從右側有Edit_text當用戶在Edit_text改變虛擬數據,即使列表行從視圖或按開關按鈕用戶進入上升/下降應該被保存在Edit_text其他觀點。我想保存數據在Edit_Text保存數據

爲此我實現了RecycleView,Adapter類和開發了一個Simple Loader類。

我正在將裝載程序類中的數據提取到我的RecycleView類中,並將數據發送到適配器類中。 從適配器類Edit_Text獲取數據後,我將數據發送回Loader類進行保存。

但是,當我改變Edit_Text中的數據它不會保存。請合作。
RecycleViewOnCreate方法:

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

} 
+0

「如果我移動到其他視圖仍然我應該得到編輯的數據」我不明白這是什麼意思。請詳細說明用戶的行爲以及您希望應用具有的行爲。儘可能具體。而不是「用戶輸入文字」給出了用戶將鍵入的實際示例。 –

+0

用戶在Edit_text中更改數據,如果他/她移動到其他View並返回,則顯示在Edite_Text中的數據應該是已更改的數據(該用戶所做的)而不是最初的數據。 – Shahek

+0

填空:1. EditText最初有_____作爲其文本。 2.用戶點擊EditText並鍵入_____。 3.然後用戶_____。從那裏繼續。 (這是**具體**的意思。)請編輯您的問題以包含此信息。 –

回答

0

@Shahek有兩個小技巧你先不要創建三種類型的數組簡單地創建一個模型類來存儲你的dat一個。

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>,然後你的適配器構造看起來像

public CustomRecycleAdapter(List<YourModelClass> yourModelList) { 
    this.yourModelList = yourModelList; 
} 

創造更新editText一個單獨的方法,只要你更新字段,你必須notifyAdapter你沒't call

private void updateListItemAtPos(int position){ 
    loader.setFirstdata(position,holder.editText.getText().toString()); 
    numbers[position]=holder.editText.getText().toString(); 
} 

你只需要更新列表項目和呼叫通知和RecyclerView自動保存編輯的文本