1

我正在尝试使用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)根据评论之一更改了适配器

4

1 回答 1

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

}

于 2013-01-28T16:13:05.263 回答