【问题标题】:Color ListView item using alert dialog android使用警报对话框android的颜色ListView项目
【发布时间】:2020-09-24 06:52:29
【问题描述】:

我想为列表视图中的项目着色。当我单击列表视图中的项目时,警报对话框显示然后如果您在对话框中使用 setChoiceMode 多个单击是,它会为项目着色,问题是每当我在警报对话框显示列表视图项目已着色之前单击该项目时,请检查一下,我只是android的新手,谢谢。

MainActivity.java

 text_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            isSelected[position]=!isSelected[position];
            alert_dialog();

        }
    });
}
public void alert_dialog(){
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Are you sure you want to color this item?")
            .setMessage("Please Confirm")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    text_listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
                }
            })
            .show();
}

我的选择器

<?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>

阵列适配器

    ArrayList<String> list_items = new ArrayList<String>();
    ArrayAdapter arrayAdapter;
    private boolean[] isSelected;  //declaration

    arrayAdapter = new ArrayAdapter(this,R.layout.list_item, list_items);
    text_listview.setAdapter(arrayAdapter);
    isSelected=new boolean[arrayAdapter.getCount()];

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    您应该在对话中单击“是”后设置选择。像这样修改你的代码。

    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){
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Are you sure you want to color this item?")
                .setMessage("Please Confirm")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                         isSelected[position]=true;
                         text_listview.setItemChecked(position,true); //Setting selected state here 
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                          isSelected[position]=false;
                          text_listview.setItemChecked(position,false); 
                        Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
                    }
                })
                .show();
    }
    

    【讨论】:

    • 嘿,又是你,我将我的选择模式设置为我的 mainactivity 以突出显示项目,但似乎你将选择模式多次删除到你的示例中并且它没有突出显示列表视图,如何去做吧?谢谢@PraveenSP
    • 不,您已经在 xml 本身中提到了 listview 的多项选择模式...
    • 该功能运行良好,但为什么在对话框显示之前单击该项目时突出显示?与上图相同,我认为我的代码缺少 @PraveenSP
    • 为什么单击时总是突出显示它已经在对话框中出现,仍然不知道该怎么办@PraveenSp
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2020-06-21
    • 2020-07-06
    • 2017-05-10
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多