我正在尝试使用notifyDataSetChanged
, 来更新我的列表视图,以便将其中的一行从密码点更改为纯文本并返回密码点。
在我的适配器中(根据此处的评论对其进行了更改),我检查了该行中对象的类型。如果该行是密码,我将其设置为
setTransformationMethod(new PasswordTransformationMethod());
为了将密码文本变成点。稍后在我的活动中,单击该行会将其类型更改为“notPass”,并且我正在尝试刷新我的列表视图以notifyDataSetChanged
将其表示为可见文本。
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;
}
}
我给适配器的对象是:
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;
}
}
我处理 clickListener 的活动部分是:
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();
}
现在,当我单击带点的密码字段时,该字段仍然是点,并且用户/电子邮件/电话类型的一些但不是所有其他字段也变成点
知道我在做什么错吗?
更新:解决了这个问题。1) 需要一个 else if,而不是 if / if。2)根据评论之一更改了适配器