[使用警报对话框的android颜色列表视图项目

问题描述 投票:0回答:1

我想为列表视图中的项目着色。当我单击列表视图中的项目时,如果您在对话框中使用setChoiceMode multiple在对话框中单击“是”,则警报对话框将显示该项目的颜色,问题是我在警报对话框中显示该列表视图项目已着色之前单击该项目,请检查一下,我只是android的新手,谢谢。

enter image description here

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

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>

arrayadapter

    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个回答
0
投票
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(); }
© www.soinside.com 2019 - 2024. All rights reserved.