自定义适配器用于Alert对话框android

时间:2017-06-11 07:57:35

标签: android dialog alertdialog

我想制作一个如下所示的警告对话框。

Installed Apps with icons

并且有我的代码

    final item[] items = {

            new item("Email", android.R.drawable.ic_menu_add),
            new item("Facebook", android.R.drawable.ic_menu_delete),

    };



    ListAdapter adapter = new ArrayAdapter<item>(
            this,
            android.R.layout.select_dialog_item,
            android.R.id.text1,
            items){

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
        {
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);



            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            tv.setCompoundDrawablePadding(dp5);

            return v;
        }
    };


    new AlertDialog.Builder(this)
            .setTitle("Share Appliction")
            .setNegativeButton("asddasd", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                }
            })
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Log.d("Fsdf","Sfss");
                }
            }).show();

和那里的项目类

public class item

{

public final String text;
public final int icon;

public item(String text, Integer icon) {
    this.text = text;
    this.icon = icon;
}
@Override
public String toString() {
    return text;
}

}

现在我不知道如何在每次使用异步任务从包管理器获取项目时将项目放在项目数组中

1 个答案:

答案 0 :(得分:0)

  

尝试我的答案,我认为它会对你有所帮助....如果你没事的话   问题然后让我知道。

Filter from Android onCreateOptionsMenu Item