AlertDialog中的ListView重复调用getView

时间:2015-12-15 09:11:43

标签: android android-listview android-alertdialog baseadapter

我有一个对话框,显示我的应用上的文件夹列表。 BaseAdapter与foldersList一起使用,它的大小为3,但日志显示重复的位置。这是我的代码:

public class ChangeLabelsDialog extends DialogFragment {
private static final Logger LOGGER = LoggerFactory.getLogger(ChangeLabelsDialog.class);
private Context context;
private List<Email> emailList;
private ListView foldersListView;
private FolderAdapter folderAdapter;
private List<EmailLabelJoin> emailLabelJoins = new ArrayList<EmailLabelJoin>();
private List<Folder> allFoldersList;

public ChangeLabelsDialog() {}

public void showDialog(final Context context, final List<Email> emailList, Long mailAccountIdExtra, final ActionMode mode) {
    this.context = context;
    this.emailList = emailList;

    LOGGER.info("CHECKING Email size: " + emailList.size());

    for (Email email : emailList) {
        emailLabelJoins.addAll(getEmailJoins(email));
    }

    allFoldersList = MyMobileApplication.getApplication(context)
            .getDaoSession()
            .getFolderDao()
            ._queryMailAccount_FolderList(mailAccountIdExtra);


    folderAdapter = new FolderAdapter(context, R.layout.simple_multiple_choice_label, new ArrayList<>(emailLabelJoins));

    View customView = LayoutInflater.from(context).inflate(R.layout.choose_label_dialog, null);
    foldersListView = (ListView) customView.findViewById(R.id.folders);
    TextView emptyLabel = (TextView) customView.findViewById(R.id.empty_label);

    foldersListView.setItemsCanFocus(false);
    foldersListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    foldersListView.setAdapter(folderAdapter);
    foldersListView.setEmptyView(emptyLabel);
    foldersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            folderAdapter.swapLabelsElemAt(position);
        }
    });

    final AlertDialog alertDialog = new AlertDialog.Builder(context)
        .setIcon(R.drawable.ic_collections_labels)
        .setCancelable(false)
        .setView(customView)
        .setTitle((folderAdapter.getCount() > 0) ? context.getResources().getString(R.string.choose_label) : context.getResources().getString(R.string.no_labels))
        .setPositiveButton(context.getString(R.string.ok), null)
        .setNegativeButton(context.getString(R.string.action_cancel), null)
        .create();

    alertDialog.show();

    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (emailList.size() == 0) {
                    alertDialog.dismiss();
                    Toast.makeText(context, context.getString(R.string.select_one_item), Toast.LENGTH_SHORT).show();
                    return;
                }
                getSelectedLabels();
                alertDialog.dismiss();
                if (mode != null)
                    mode.finish();
            }
        }
    );

    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        }
    );
}

private List<EmailLabelJoin> getEmailJoins(Email email) {
    return MyMobileApplication.getApplication(context)
            .getDaoSession()
            .getEmailLabelJoinDao()
            .queryBuilder()
            .where(EmailLabelJoinDao.Properties.EmailId.eq(email.getId()))
            .build().list();
}

private void getSelectedLabels() {
    SparseBooleanArray booleanArray = foldersListView.getCheckedItemPositions();
    ArrayList<Folder> selectedFolders = new ArrayList<Folder>();
    for (int i = 0; i < booleanArray.size(); i++) {
        int position = booleanArray.keyAt(i);
        if (booleanArray.valueAt(i))
            selectedFolders.add((Folder) folderAdapter.getItem(position));
    }
    saveOrUpdateFolders(selectedFolders);
}

private void saveOrUpdateFolders(ArrayList<Folder> selectedFolders) {
    for (Email email : emailList) {
        for (EmailLabelJoin join : getEmailJoins(email)) {
            join.delete();
        }
        for (Folder originalFolder: allFoldersList) {
            if (selectedFolders.contains(originalFolder)) {
                EmailLabelJoin emailLabelJoin = new EmailLabelJoin();
                emailLabelJoin.setEmail(email);
                emailLabelJoin.setFolder(originalFolder);
                MyMobileApplication.getApplication(context)
                        .getDaoSession()
                        .getEmailLabelJoinDao()
                        .insert(emailLabelJoin);
            }
        }
    }

    Intent intent = new Intent(ActionNames.ACTION_EMAIL_LIST_CHANGED);
    context.sendBroadcast(intent);

    if (context instanceof EmailPagerActivity_)
        context.sendBroadcast(new Intent(ActionNames.ACTION_VIEW_EMAIL_FRAGMENT_CHANGED));

    if (selectedFolders.size() > 0)
        Toast.makeText(context, context.getResources().getString(R.string.messages_moved_to) + selectedFolders.toString(), Toast.LENGTH_SHORT).show();
}

private class FolderAdapter extends BaseAdapter {

    private Context mContext;
    private List<EmailLabelJoin> emailLabelsJoinClone;
    private int resId;

    public FolderAdapter(Context context, int checkedTextView, List<EmailLabelJoin> emailLabelJoinsClone) {
        this.mContext = context;
        this.emailLabelsJoinClone = emailLabelJoinsClone;
        LOGGER.error("emailLabelsJoinClone = " + emailLabelsJoinClone.size());
        this.resId = checkedTextView;
    }

    @Override
    public int getCount() {
        return allFoldersList.size();
    }

    @Override
    public Object getItem(int position) {
        return allFoldersList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return allFoldersList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LOGGER.error("Position = " + position);
        Folder mFolder = allFoldersList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(resId, parent, false);
            holder = new ViewHolder();
            holder.checkedTextView = (CheckedTextView) convertView.findViewById(android.R.id.text1);
            holder.colorView = (TextView) convertView.findViewById(R.id.colorPreView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.checkedTextView.setText(mFolder.getName());
        if (mFolder.getColor() != -1) {
            holder.colorView.setBackgroundColor(mFolder.getColor());
        }

        boolean isChecked = isContainEmailLabelJoinsList(mFolder.getId()) != -1;
        holder.checkedTextView.setChecked(isChecked);

        return convertView;
    }

    public void swapLabelsElemAt(int position) {
        Folder folder = allFoldersList.get(position);
        int index = isContainEmailLabelJoinsList(folder.getId());
        if (index != -1) {
            emailLabelsJoinClone.remove(index);
        } else {
            EmailLabelJoin emailLabelJoin = new EmailLabelJoin();
            emailLabelJoin.setFolder(folder);
            emailLabelsJoinClone.add(emailLabelJoin);
        }
        notifyDataSetChanged();
    }

    private int isContainEmailLabelJoinsList(Long id) {
        for (int i = 0; i < emailLabelsJoinClone.size(); i++) {
            EmailLabelJoin join = emailLabelsJoinClone.get(i);
            if (join.getFolderId() == id ) {
                return i;
            }
        }
        return -1;
    }
}

 static class ViewHolder {
        CheckedTextView checkedTextView;
        public TextView colorView;
    }
}

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <View android:layout_width="match_parent" android:layout_height="2dp"
        android:background="@color/light_blue"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/folders"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:divider="@null"/>

        <TextView
            android:id="@+id/empty_label"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="18sp"
            android:visibility="gone"
            android:text="@string/you_have_no_labels"/>

    </FrameLayout>
</LinearLayout>

日志:

ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2
ChangeLabelsDialog: Position = 0
ChangeLabelsDialog: Position = 1
ChangeLabelsDialog: Position = 2

你能帮我解释为什么我的适配器重复以呈现文件夹列表吗?

0 个答案:

没有答案