使用自定义适配器在ListVIew中设置密码可见/不可见

时间:2017-06-06 19:55:56

标签: android listview

我有一个显示wifi设置的ListView。我的视图中的每个项目都有一个按钮,可以将密码的可见性设置为可见或不可见。我能够将密码设置为可见但不能返回。这是我的代码:

Customlistadapter

package com.app.wifibackup;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.wifibackup.R;

import java.security.acl.Owner;
import java.util.regex.Pattern;

import static android.R.attr.label;

class CustomListAdapter extends ArrayAdapter<String> {

    final Context context = getContext();

    String ssid;
    String password;
    String owner;
    String comment;
    String location;
    String provider;

    Boolean showPwd = false;
    String hiddenPassword = "";
    String bulletChar = "\u2022";
    String passwordToShow;


    public CustomListAdapter(Context context, String[] dataListFinal) {
        super(context, R.layout.listitem_wifilist ,dataListFinal);
    }
    public String allElementsAdapter = "";

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater iteminflater = LayoutInflater.from(getContext());
        View customView = iteminflater.inflate(R.layout.listitem_wifilist, parent, false);

        ImageView image = (ImageView) customView.findViewById(R.id.list_icon_provider);
        TextView textViewSsid = (TextView) customView.findViewById(R.id.list_item_datalist_textview_ssid);
        final TextView textViewPassword = (TextView) customView.findViewById(R.id.list_item_datalist_textview_password);
        TextView textViewOwner = (TextView) customView.findViewById(R.id.list_item_datalist_textview_owner);

        final String singleListItem = getItem(position);
        final String[] singleListItemArray = singleListItem.split("\t");
        final String id = singleListItemArray[0];
        ssid = singleListItemArray[1];
        password = singleListItemArray[2];
        owner = singleListItemArray[3];
        comment = singleListItemArray[4];
        location = singleListItemArray[5];
        provider = singleListItemArray[6];

        ImageButton imagebuttonEdit = (ImageButton) customView.findViewById(R.id.imageButton_edit);
        ImageButton imagebuttonSetVisibility = (ImageButton) customView.findViewById(R.id.imageButton_setPasswordVisibility);

        allElementsAdapter = id + ssid + password + owner + comment + location + provider;

        switch (provider) {
            case "empty":
                image.setImageResource(R.drawable.icon_provider_dummy);
                break;
            case "FritzBox":
                image.setImageResource(R.drawable.icon_provider_fritzbox);
                break;
            case "Speedport":
                image.setImageResource(R.drawable.icon_provider_speedport);
                break;
            case "Unitymedia":
                image.setImageResource(R.drawable.icon_provider_unitymedia);
                break;
        }

        String ownerString = owner;

        textViewSsid.setText(ssid);

        textViewPassword.setText(setPasswordInvisibile(password));
        textViewOwner.setText(ownerString);


        imagebuttonEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String [] tmpDataArray = singleListItem.split("\t");
                ((MainActivity)context).openDialog("Edit", tmpDataArray);
            }
        });

        imagebuttonSetVisibility.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String [] tmpDataArray = singleListItem.split("\t");
                if (!showPwd){
                    showPwd = true;
                    textViewPassword.setText(setPasswordInvisibile(tmpDataArray[2]));
                }
                if (showPwd){
                    showPwd = false;
                    textViewPassword.setText(tmpDataArray[2]);
                }
            }
        });

        return customView;
    }

    private String setPasswordInvisibile(String pwd){
        int passwordLength = pwd.length();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < passwordLength; i++) {
            builder.append(bulletChar);
            passwordToShow = builder.toString();
        }

        return passwordToShow;
    }
}

你对我有什么建议吗?

0 个答案:

没有答案