我正在尝试使用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();
}
现在,当我点击带有点的密码字段时,该字段仍然是点状的,而某些但不是所有其他类型为user / email / phone的字段也变成了点
知道我做错了什么吗?
更新:解决了这个问题。 1)如果,而不是if / if,需要一个else。 2)根据其中一条评论
更改了适配器答案 0 :(得分:2)
在getView方法的适配器类中,您不会为类型为“no pass”的字段重置转换。
if(userAccountData.type.equals("notPass")){
holder.txtTitle.setTransformationMethod(null);
}
您可以使用'getItemAtPositionMethod'
获取相关数据,而不是直接将位置用作数组索引listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
UserAccountData uad = (UserAccountData)parent.getItemAtPosition(position);
//your code
});
尝试在每个getView调用中直接设置值:
@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);
}
UserAccountData userAccountData = data[position];
TextView tvTitle = (TextView) row.findViewById(R.id.txtTitle);
if(userAccountData.type.equals("pass")){
tvTitle.setTransformationMethod(new PasswordTransformationMethod());
}
if(userAccountData.type.equals("notPass")){
tvTitle .setTransformationMethod(null);
}
}
}