I am trying to use notifyDataSetChanged , to update my list view in order to change one row in it from password dots, to plain text and back to password dots.
In my adapter (changed it based on a comment here ) I check the type of the object that I have in that row. If the row is a password I set it to
setTransformationMethod(new PasswordTransformationMethod());
in order to turn the password text to dots. Later in my activity a click on that row will change it type to “notPass”, and I am trying to refresh my listview with notifyDataSetChanged to represent it as a visible text.
public class UserAccountAdapter extends ArrayAdapter<UserAccountData> {
Context context;
int layoutResourceId;
UserAccountData data[] = null;
TreeSet<String> mSeparatorsSet = new TreeSet<String>();
public UserAccountAdapter(Context context, int layoutResourceId,
UserAccountData[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserAccountDataHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserAccountDataHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imgUserAccountIcon);
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
row.setTag(holder);
} else {
holder = (UserAccountDataHolder) row.getTag();
}
UserAccountData userAccountData = data[position];
holder.txtTitle.setText(userAccountData.title);
//hold
er.txtTitle.setTransformationMethod(new PasswordTransformationMethod());
holder.imgIcon.setImageResource(userAccountData.icon);
if(userAccountData.type.equals("pass")){
((TextView) row.findViewById(R.id.txtTitle)).setTransformationMethod(new PasswordTransformationMethod());
}
else{
((TextView) row.findViewById(R.id.txtTitle)).setTransformationMethod(null);
}
return row;
}
static class UserAccountDataHolder {
ImageView imgIcon;
TextView txtTitle;
}
}
The object that I am giving the adapter is:
public class UserAccountData {
public int icon;
public String type;
public String title;
public UserAccountData(){
super();
}
public UserAccountData(int icon, String title, String type) {
super();
this.icon = icon;
this.title = title;
this.type = type;
}
public void setType(String type){
this.type = type;
}
}
And my activity part that deals with the clickListener is:
usersArray = new UserAccountData[user_data.size()];
user_data.toArray(usersArray);
final UserAccountAdapter adapter = new UserAccountAdapter(this,
R.layout.user_accounts_row, usersArray);
userAccountsListView = (ListView) findViewById(R.id.userAccounts);
userAccountsListView.setAdapter(adapter);
userAccountsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).type.equals("pass")){
adapter.getItem(position).setType("notPass");
adapter.notifyDataSetChanged();
}
else if(adapter.getItem(position).type.equals("notPass")){
adapter.getItem(position).setType("pass");
adapter.notifyDataSetChanged();
}
Right now when I am clicking on a password field with dots, that field remains dotted and some but not all of the other fields that are of type user/email/phone are also turning into dots
Any idea what am I doing wrong?
UPDATE: solved the issue.
1) needed an else if, and not if / if.
2) changed the adapter based on one of the comments
In your adapter class in the getView Method you don’t reset the transformation for fields with type ‘no pass’.
Instead of directly using the position as an array index, you could get the associated data with the ‘getItemAtPositionMethod’
Try to set values directly in each getView call:
}