Skip to content

Commit

Permalink
Fix bug in expanded list that caused "random" value updates in the list.
Browse files Browse the repository at this point in the history
Remove delete button on Synonyms in list
  • Loading branch information
matthewnbrown committed Nov 10, 2020
1 parent 70fc1d0 commit bc70e0c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,35 @@ public long getChildId(int groupPosition, int childPosition) {
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final View row;
final Synonym synonym = (Synonym) getChild(groupPosition, childPosition);

ChildViewHolder holder;

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) parent.getContext()
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.synonym_list_item, parent, false);
row = inflater.inflate(R.layout.synonym_list_item, parent, false);

holder = new ChildViewHolder(convertView, groupPosition, childPosition, synonym.getWord());
convertView.setTag(holder);
holder = new ChildViewHolder(row);
row.setTag(holder);

} else {
holder = (ChildViewHolder) convertView.getTag();
row = convertView;
holder = (ChildViewHolder) row.getTag();
}


holder.updateChildInfo(groupPosition, childPosition, synonym.getWord());

holder.lblSynonym.setText(synonym.getWord());
holder.mScoreText.setText(Integer.toString(synonym.getScore()));

//holder.btnDeleteSyn.setOnClickListener(view -> );
holder.btnEditSyn.setOnClickListener(view -> onEditSynonymClick(view, null, holder));


return convertView;
return row;
}

@Override
Expand Down Expand Up @@ -110,12 +114,14 @@ public View getGroupView(int groupPosition, boolean isExpanded,
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.synonym_item_list_group, parent, false);

holder = new GroupViewHolder(convertView, groupPosition);
holder = new GroupViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}

holder.updateGroupInfo(groupPosition);

// Prevent group from not being expandable due to focusable button
holder.newSynBtn.setFocusable(false);

Expand Down Expand Up @@ -173,31 +179,23 @@ public void synonymEditAlert(View view, GroupViewHolder pHolder, ChildViewHolder
builder.setView(dialoglayout);

// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(!TextUtils.isDigitsOnly(scoreTxt.getText())
|| scoreTxt.getText().toString().length() == 0) {
return;
}

String word = wordTxt.getText().toString().toLowerCase().trim();
int score = Integer.parseInt(scoreTxt.getText().toString());

if(cHolder != null){
synonymCacher.updateSynonymScore((String) getGroup(cHolder.groupPos), word, score);
} else {
synonymCacher.addSynonymToBaseWord((String) getGroup(pHolder.groupPos), word, score);
}
notifyDataSetChanged();
builder.setPositiveButton("OK", (dialog, which) -> {
if(!TextUtils.isDigitsOnly(scoreTxt.getText())
|| scoreTxt.getText().toString().length() == 0) {
return;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();

String word = wordTxt.getText().toString().toLowerCase().trim();
int score = Integer.parseInt(scoreTxt.getText().toString());

if(cHolder != null){
synonymCacher.updateSynonymScore((String) getGroup(cHolder.groupPos), word, score);
} else {
synonymCacher.addSynonymToBaseWord((String) getGroup(pHolder.groupPos), word, score);
}
notifyDataSetChanged();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());


builder.show();
Expand All @@ -207,17 +205,16 @@ public void onNewSynonymClick(View view, GroupViewHolder holder) {
synonymEditAlert(view, holder, null);
}

public void onEditSynonymClick(View view, GroupViewHolder pHolder, ChildViewHolder cHolder) {
public void onEditSynonymClick(View view, GroupViewHolder pHolder, ChildViewHolder cHolder) {
synonymEditAlert(view, pHolder, cHolder);
}


public void onDeleteBaseWordClick(View view, GroupViewHolder holder) {
Context ctx = view.getContext();

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage
("Are you sure you want to delete \"" + ( (String) getGroup(holder.groupPos)) +"\"?");
("Are you sure you want to delete \"" + getGroup(holder.groupPos) + "\"?");
builder.setCancelable(true);

builder.setPositiveButton(
Expand Down Expand Up @@ -282,16 +279,18 @@ public static class GroupViewHolder
public final TextView lblListHeader;
public final ImageButton newSynBtn;
public final ImageButton delWordBtn;
public final int groupPos;
public int groupPos;

public GroupViewHolder(View view, int groupPos)
public GroupViewHolder(View view)
{
this.mView = view;
this.lblListHeader = view.findViewById(R.id.synonym_item_header);
this.newSynBtn = view.findViewById(R.id.btn_add_new_synonym);
this.delWordBtn = view.findViewById(R.id.btn_delete_baseword);
this.groupPos = groupPos;
}

public void updateGroupInfo (int groupPos) {
this.groupPos = groupPos;
}

}
Expand All @@ -302,18 +301,22 @@ public static class ChildViewHolder
public final TextView mScoreText;
public final TextView lblSynonym;
public final ImageButton btnEditSyn;
public final ImageButton btnDeleteSyn;
public final int groupPos;
public final int childPos;
public final String synonym;
//public final ImageButton btnDeleteSyn;
public int groupPos;
public int childPos;
public String synonym;

public ChildViewHolder(View view, int groupPos, int childPos, String synonym)
public ChildViewHolder(View view)
{
this.mView = view;
this.mScoreText = view.findViewById(R.id.synonym_item_score);
this.lblSynonym = view.findViewById(R.id.synonym_item_label);
this.btnEditSyn = view.findViewById(R.id.btn_edit_syn);
this.btnDeleteSyn = view.findViewById(R.id.btn_delete_syn);
//this.btnDeleteSyn = view.findViewById(R.id.btn_delete_syn);

}

public void updateChildInfo(int groupPos, int childPos, String synonym) {
this.groupPos = groupPos;
this.childPos = childPos;
this.synonym = synonym;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
expListView.setGroupIndicator(null);

ImageButton newBaseWordBtn = rootView.findViewById(R.id.btn_add_new_baseword);
newBaseWordBtn.setOnClickListener(view -> onNewBaseWordClick(view));
newBaseWordBtn.setOnClickListener(this::onNewBaseWordClick);

// Inflate the layout for this fragment
return rootView;
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/res/layout/synonym_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@
android:id="@+id/btn_edit_syn"
app:layout_constraintTop_toTopOf="@id/synonym_item_score"
app:layout_constraintBottom_toBottomOf="@id/synonym_item_score"
app:layout_constraintRight_toLeftOf="@id/btn_delete_syn"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/btn_edit_synonym_desc"
android:background="@drawable/ic_baseline_edit_24"/>

<ImageButton
android:id="@+id/btn_delete_syn"
app:layout_constraintTop_toTopOf="@id/synonym_item_score"
app:layout_constraintBottom_toBottomOf="@id/synonym_item_score"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/btn_delete_synonym_desc"
android:background="@drawable/ic_baseline_delete_24"/>
<!-- <ImageButton-->
<!-- android:id="@+id/btn_delete_syn"-->
<!-- app:layout_constraintTop_toTopOf="@id/synonym_item_score"-->
<!-- app:layout_constraintBottom_toBottomOf="@id/synonym_item_score"-->
<!-- app:layout_constraintRight_toRightOf="parent"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:contentDescription="@string/btn_delete_synonym_desc"-->
<!-- android:background="@drawable/ic_baseline_delete_24"/>-->

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit bc70e0c

Please sign in to comment.