gpt4 book ai didi

android - notifyDataSetChanged 弄乱了我的 ListView

转载 作者:行者123 更新时间:2023-11-29 21:50:20 25 4
gpt4 key购买 nike

我正在尝试使用 notifyDataSetChanged 来更新我的 ListView ,以便将其中的一行从密码点更改为纯文本并返回密码点。

在我的适配器中(根据此处的评论对其进行了更改)我检查了该行中的对象类型。如果该行是密码,我将其设置为

setTransformationMethod(new PasswordTransformationMethod()); 

为了将密码文本变成点。稍后在我的 Activity 中单击该行会将其类型更改为“notPass”,我正在尝试使用 notifyDataSetChanged 刷新我的 ListView 以将其表示为可见文本。

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 的 Activity 部分是:

 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) 根据其中一条评论更改了适配器

最佳答案

在 getView 方法的适配器类中,您不会重置类型为“不通过”的字段的转换。

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);
}
}

关于android - notifyDataSetChanged 弄乱了我的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com