ArrayListの中身をRecyclerViewに表示するアプリを作っています。Listをタップした時、
dialogが開いて指定した行のデータを変更する処理(changeAtPosition)とデータを削除する処理(removeAtPosition)を
KujiAdapterに設置しました。
changeAtPositionとremoveAtPositionにはアクションが発生した時にRecyclerViewを更新するように
notifyItemChangedとnotifyItemRemovedを設置しました。
テストrunするとremoveAtPositionは問題なく「データが1行なくなって再表示」をするのですが
changeAtPositionは「データが変わって再表示」する挙動がおきません。なぜか再表示されず変わりません。
・ArrayListの中身を確認すると変更されています。
・他のActivityに行って戻ってくると変わっています。
ネットで検索しましたがRecyclerViewのnotifyItemChangedに関しての情報は少なく該当するものを
見つけることができませんでした。
どういった要因が考えれられるでしょうか?ヒントありましたらよろしくお願いします。
環境 os mac
開発環境 Android studio 2.2.3
KujiAdapter.java
public class KujiAdapter extends RecyclerView.Adapter<KujiViewHolder> { private AlertDialog alertDialog; public Context context; private ArrayList<DataType> arrayList; private int tabNumber; public KujiAdapter(Context context, ArrayList<DataType> arrayList, int tabNumber) { this.context = context; this.arrayList = arrayList; this.tabNumber = tabNumber; } private ArrayList<DataType> lot1Array = new ArrayList<>(); private ArrayList<DataType> lot2Array = new ArrayList<>(); @Override public KujiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.card_layout, parent, false ); return new KujiViewHolder(view); } @Override public void onBindViewHolder(final KujiViewHolder holder, final int listposition) { final EditText editText = new EditText(holder.base.getContext()); final EditText editNum = new EditText(holder.base.getContext()); final Editable lotText = editText.getText(); final Editable lotNum = editNum.getText(); holder.kujiContents.setText(arrayList.get(listposition).getKuji_contents()); holder.kujiNumber.setText(arrayList.get(listposition).getKuji_number()); holder.base.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(holder.base.getContext()); if (alertDialog == null) { LinearLayout layout = new LinearLayout(holder.base.getContext()); layout.setOrientation(LinearLayout.VERTICAL); TextView conponentText = new TextView(holder.base.getContext()); conponentText.setText(holder.base.getContext().getString(R.string.change_result)); conponentText.setHint(R.string.change_result_input); layout.addView(conponentText); layout.addView(editText); TextView numText = new TextView(holder.base.getContext()); numText.setText(holder.base.getContext().getString(R.string.change_num)); numText.setHint(R.string.change_num_input); layout.addView(numText); layout.addView(editNum); alertDialogBuilder .setTitle(holder.base.getContext().getString(R.string.change_result)) .setView(layout) .setPositiveButton(holder.base.getContext().getString(R.string.save_button), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (editText.length() == 0 | editNum.length() == 0) { Toast.makeText(holder.base.getContext(), "noting", Toast.LENGTH_SHORT).show(); } else { switch (tabNumber) { case 0: lot1Array = PrefManager.loadDataList(holder.base.getContext(), "lot1Array"); changeAtPosition(listposition, lot1Array, "lot1Array", lotText, lotNum); break; case 1: lot2Array = PrefManager.loadDataList(holder.base.getContext(), "lot2Array"); changeAtPosition(listposition, lot2Array, "lot2Array", lotText, lotNum); break; } } } }) .setNeutralButton("remove", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (tabNumber){ case 0: removeAtPosition(listposition,arrayList, "lot1Array"); break; case 1: removeAtPosition(listposition,arrayList, "lot2Array"); break; } } }) .setNegativeButton(holder.base.getContext().getString(R.string.cancel_button), null); alertDialog = alertDialogBuilder.create(); alertDialog.show(); } alertDialog.show(); } }); } public void changeAtPosition(int listPosition, ArrayList<DataType> arrayList,String key, Editable lotText, Editable lotNum){ arrayList.set(listPosition,new DataType(lotText.toString(),lotNum.toString())); notifyItemChanged(listPosition); PrefManager.saveDataList(context,key,arrayList); } public void removeAtPosition(int listPosition, ArrayList<DataType> arrayList, String key){ arrayList.remove(listPosition); notifyItemRemoved(listPosition); PrefManager.saveDataList(context,key,arrayList); } @Override public int getItemCount() { return arrayList.size(); } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private AlertDialog alertDialog; private ArrayList<DataType> lot1Array; private ArrayList<DataType> lot2Array; //タブFlag private int tabNumber = 0; private ArrayList<DataType> defaultArray; RecyclerView recyclerView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.cardList); recyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); lot1Array = PrefManager.loadDataList(getApplicationContext(),"lot1Array"); lot2Array = PrefManager.loadDataList(getApplicationContext(),"lot2Array"); } //ArrayListの初期値設定して保存 if (lot1Array.isEmpty()){ Log.d("UAA:isempty", String.valueOf(lot1Array.isEmpty())); initArray(lot1Array); initArray(lot2Array); PrefManager.saveDataList(getApplicationContext(),"lot1Array", lot1Array); PrefManager.saveDataList(getApplicationContext(),"lot2Array", lot2Array); } //既にデータがあるので lot1Array = PrefManager.loadDataList(getApplicationContext(),"lot1Array"); IndicateArray(lot1Array, tabNumber); Log.d("UAA:default2", String.valueOf(defaultArray)); Log.d("UAA:lot1", String.valueOf(lot1Array)); } //Arraylistを表示するメソッド public void IndicateArray(ArrayList<DataType> defaultArray, int tabNumber){ RecyclerView.Adapter adapter = new KujiAdapter(this,defaultArray,tabNumber); recyclerView.setAdapter(adapter); // recyclerView.setItemAnimator(null); recyclerView.addItemDecoration(new DividerItemDecoration(this)); adapter.notifyDataSetChanged(); } //Arraylistを初期化するメソッド public void initArray(ArrayList<DataType> lotArray){ String[] contentData = { this.getString(R.string.setting_first), this.getString(R.string.setting_second) }; String[] numberData = { "1", "3" }; for (int i = 0; i < contentData.length ; i++){ String kuji_contents = contentData[i]; String kuji_number = numberData[i]; lotArray.add(new DataType( kuji_contents, kuji_number )); } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/09 22:56