Android AlertDialog不显示适配器名称

时间:2015-09-02 10:14:04

标签: java android android-arrayadapter android-alertdialog

我制作了一个应该显示应用列表的AllertDialog,当我点击其中一个alerdialog成员时,它会从ArrayAdapter返回正确的字符串。

这是它的外观。

current look of the dialog

    public class AppList {

    private Activity main_activity;
    private AlertDialog app_list_dialog;

    private ArrayList<RemoteAppDetail> remoteAppDetails;

    public AppList(Activity activity){
        this.main_activity = activity;
//      init();
    }

    /**
     * Initialize the Dialog, this needs to be called before .show()/.hide().dispose();
     */
    public void init(){
        this.remoteAppDetails = ConnectionHandler.getLibrary();

        AlertDialog.Builder app_list_builder = new AlertDialog.Builder(main_activity);
        app_list_builder.setIcon(R.drawable.ic_input_add);
        app_list_builder.setTitle("Installable/Updatable Apps: ");

        final ArrayAdapter<String> app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice);
        final ArrayList<String> app_name_list = getInstallableApps();

        app_list.addAll(app_name_list);


        if(!app_list.isEmpty()){
            //Add the app_list adapter (The button list)
            app_list_builder.setAdapter(app_list, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i("DIALOG SELECTED", app_list.getItem(which));
                }
            });
        }else{
            app_list_builder.setMessage("There are no apps available for download/update.");
        }

        //Close Button AppList
        app_list_builder.setNegativeButton("Close", 
                new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        this.app_list_dialog = app_list_builder.create();
    }


    /**
     * Show the list.
     */
    public void show(){
        if(this.app_list_dialog != null){
            app_list_dialog.show();
        }
    }

    /**
     * Hide the list.
     */
    public void hide(){
        if(this.app_list_dialog != null){
            app_list_dialog.hide();
        }
    }

    /**
     * Disposes the list, it will need to be reinitialized of you change your mind after disposing of the list.
     */
    public void dispose(){
        if(this.app_list_dialog != null){
            app_list_dialog.dismiss();
        }
    }

    /**
     * Gets a list of apps that can be installed
     * @return Arraylist installable apps
     */
    private ArrayList<String> getInstallableApps(){
        ArrayList<String> installable_apps_list = new ArrayList<String>();
        if(this.remoteAppDetails != null){
            for(RemoteAppDetail rea : this.remoteAppDetails){
                BasicNameValuePair remote_app_info = new BasicNameValuePair(rea.filename, String.valueOf(rea.version));
                BasicNameValuePair local_app_info = getLocalAppInfo(rea.filename);

                if(local_app_info != null){             
                    if(isRemoteVersionNewer(remote_app_info, local_app_info)){
                        installable_apps_list.add(rea.filename);
                    }
                }else{
                    if(rea.unlock_status == 0){
                        installable_apps_list.add(rea.filename);
                    }
                }

            }
        }
        return installable_apps_list;
    }


    /**
     * Check if the app exists locally.
     * @param app_name_remote
     * @return Local app info as BasicNameValuePair
     */
    private BasicNameValuePair getLocalAppInfo(String app_name_remote){
        List<PackageInfo> packs = main_activity.getPackageManager().getInstalledPackages(0);
        for(PackageInfo pi : packs){
            String app_name_local = pi.applicationInfo.loadLabel(main_activity.getPackageManager()).toString();
            String app_version = String.valueOf(pi.versionCode);
            if(app_name_remote == app_name_local){
                return new BasicNameValuePair(app_name_local, app_version);
            }
        }

        return null;
    }


    /**
     * Compare local and remote app info.
     * @param remote_app_info
     * @param local_app_info
     * @return Return whether or not the remote version is newer as Boolean.
     */
    private boolean isRemoteVersionNewer(BasicNameValuePair remote_app_info, BasicNameValuePair local_app_info){
        if(remote_app_info.getName().contains(local_app_info.getName())){
            if(remote_app_info.getValue() == local_app_info.getValue()){
                return true;
            }
        }

        return false;
    }

所以我真的想知道我做错了什么,以及这是否足够了。

更新1: 这就是我称之为这个课程的方式:

Applist main_app_list = new Applist(this);
        main_app_list.init();
        main_app_list.show();

在我的主要活动的onCreate方法中就像这样。

有人建议我的getInstallableApps()可能会返回空值, 但是,如果仔细检查代码,则不可能。因为如果它们是空的,它甚至不会有任何适配器成员。

无论哪种方式,我都添加了一些测试值,并且仍然可以做同样的事情:

        final ArrayAdapter<String> app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice);
        final ArrayList<String> app_name_list = getInstallableApps();

//      app_list.addAll(app_name_list);

        app_list.add("TEST");
        app_list.add("TEST2");

其余代码保持不变。

更新2:

当我触摸其中一个成员时,它会在短时间内显示出他们的名字,或者当我按住它时,只要我按住它就会显示它们。

2 个答案:

答案 0 :(得分:0)

调试您的getInstallableApps()方法,您会看到它正在输入for语句,因为this.remoteAppDetails中有两个元素,但在内部,您从中获取数据的元素未被正确调用或空(remote_app_infolocal_app_info),或文件名字段为空。一旦你发现什么是为你的数组放置空值,你可以修复它。

答案 1 :(得分:0)

所以我最终强迫颜色来解决这个问题,因为自定义布局对此非常麻烦。

        app_list = new ArrayAdapter<String>(main_activity, R.layout.simple_list_item_single_choice){
            public View getView(int position, View convertView, android.view.ViewGroup parent) {
                TextView textView = (TextView) super.getView(position, convertView, parent);

                textView.setTextColor(Color.BLACK);

                return textView;
                }
        };

如果您认为它没有显示适配器名称以供将来参考,只需按住其中一个成员以查看它是否显示成员名称,如果它确实您知道您遇到了字体问题