单击对话框之前突出显示列表视图 - Android

Listview highlighted before clicking dialog box - Android

I want to highlight a clicked ListView item after clicking the alert dialog box Read button; the code below works perfect, but the only issue is whenever I click the item, it's highlighted with the color before the dialog shows.

我曾怀疑我的代码是否遗漏了,请检查一下。谢谢

MainActivity.java

    private boolean[] isSelected; //global variable
    text_listview = findViewById(R.id.textlistview);
    arrayAdapter = new ArrayAdapter(this,R.layout.list_item, list_items);
    text_listview.setAdapter(arrayAdapter);
    isSelected=new boolean[arrayAdapter.getCount()];

    text_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            alert_dialog(position);

        }
    });
}


public void alert_dialog(final int position){
    final SweetAlertDialog pDialog = new SweetAlertDialog(
            text_message.this, SweetAlertDialog.WARNING_TYPE);
    pDialog.setTitleText("Highlight data?");
    pDialog.setContentText("Please choose the corresponding details");
    pDialog.setCancelText("Unread!");
    pDialog.setConfirmText("Read!");
    pDialog.showCancelButton(true);
    pDialog.show();
    pDialog.setCancelable(false);
    pDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.setTitleText("Successfully save!")
                            .setContentText("Item mark as Unread")
                            .setConfirmText("OK")
                            .showCancelButton(false)
                            .setCancelClickListener(null)
                            .setConfirmClickListener(null)
                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                            sDialog.setCanceledOnTouchOutside(false);
                    isSelected[position]=false;
                    text_listview.setItemChecked(position,false);
                }
            })
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {

                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.setTitleText("Successfully save!")
                            .setContentText("Item mark as Read")
                            .setConfirmText("OK")
                            .showCancelButton(false)
                            .setCancelClickListener(null)
                            .setConfirmClickListener(null)
                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                            sDialog.setCanceledOnTouchOutside(false);
                    isSelected[position]=true;
                    text_listview.setItemChecked(position,true);
                }
            })
            .show();
}

layout activitymain.xml

<ListView
    android:id="@+id/textlistview"
    android:layout_width="match_parent"
    android:choiceMode="multipleChoice"
    android:layout_height="match_parent" />

drawable myselecter

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@color/lightOrange" android:state_activated="true"
    /></selector>

而不是使用 selector 您可以通过编程设置颜色来解决这个问题。

所以,首先删除选择器xml文件,并删除其相关的布局属性。

将未读颜色添加到对话框 setCancelClickListener1() 并将已读颜色添加到 setConfirmClickListener 使用: view.setBackgroundColor(getResources().getColor(R.color.light_orange));

view是被点击的ListView行的根;你可以从onItemClick回调的参数中得到它。

因此,更改 alert_dialog() 方法的签名以接受视图:

text_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        alert_dialog(position, view); // <<<<<<<<<< here is the change

    }
});

并在对话框点击中添加调整颜色

public void alert_dialog(final int position, final View view){
    final SweetAlertDialog pDialog = new SweetAlertDialog(
            text_message.this, SweetAlertDialog.WARNING_TYPE);
    pDialog.setTitleText("Highlight data?");
    pDialog.setContentText("Please choose the corresponding details");
    pDialog.setCancelText("Unread!");
    pDialog.setConfirmText("Read!");
    pDialog.showCancelButton(true);
    pDialog.show();
    pDialog.setCancelable(false);
    pDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.setTitleText("Successfully save!")
                            .setContentText("Item mark as Unread")
                            .setConfirmText("OK")
                            .showCancelButton(false)
                            .setCancelClickListener(null)
                            .setConfirmClickListener(null)
                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                            sDialog.setCanceledOnTouchOutside(false);
                    isSelected[position]=false;
                    // text_listview.setItemChecked(position,false); // No need to this line

                    view.setBackgroundColor(getResources().getColor(android.R.color.transparent)); // Setting a transparent color for the unread item
                }
            })
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {

                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.setTitleText("Successfully save!")
                            .setContentText("Item mark as Read")
                            .setConfirmText("OK")
                            .showCancelButton(false)
                            .setCancelClickListener(null)
                            .setConfirmClickListener(null)
                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                            sDialog.setCanceledOnTouchOutside(false);
                    isSelected[position]=true;
                    // text_listview.setItemChecked(position,true); // No need to this
                   view.setBackgroundColor(getResources().getColor(R.color.light_orange)); // create  light_orange color resource in colors.xml
                }
            })
            .show();
            updateBooleanArray();
}
public void updateBooleanArray() {
        arrayAdapter = new ArrayAdapter(this,R.layout.list_item, list_items);
        text_listview.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
        boolean[] tempSelected=new boolean[arrayAdapter.getCount()];

        for(int i=0;i<isSelected.length;i++)
        {
             tempSelected[i]=isSelected[i];
             if(tempSelected[i])
             {
               text_listview.setItemChecked(i,true);
             }
        }

        isSelected = tempSelected;
}