0

我有一个带有 的文本视图visibility=gone,如果某些条件为真,我想将其设置为setVisibility(View.VISIBLE).

此小部件位于 list_item 布局内,而该布局又位于 listview 内。

这是我将其设置为可见的Java代码:

if(sex_affiche && favori_affiche){
        mSchedule = new SimpleAdapter(this.getBaseContext(),
                listItem, R.layout.affichage_item_listview, new String[] {
                        "nom", "tel","sex","favori" }, new int[] { R.id.item_nom,
                        R.id.item_tel, R.id.item_sex_jdida, R.id.item_favori });

        listedescontacts.setAdapter(mSchedule);
        registerForContextMenu(listedescontacts);

        if(sex == null){
            Log.d("null", "yes");
            TextView sex2  = (TextView)findViewById(R.id.item_sex_jdida);
            if(sex2 == null)
            Log.d("still null", "yes");
            else
                sex2.setVisibility(View.VISIBLE);
        }


        //favori.setVisibility(View.VISIBLE);

    }

我已经将它实例化oncreate并在此方法中重做,但它始终为空。

4

1 回答 1

4

您尝试访问的文本视图在列表中;所以你不能直接访问它们。您可以做的是getView()从适配器覆盖并在其中获取视图并使其可见(盲编码):

    mSchedule = new SimpleAdapter(this.getBaseContext(),
            listItem, R.layout.affichage_item_listview, new String[] {
        "nom", "tel","sex","favori" }, new int[] { R.id.item_nom,
        R.id.item_tel, R.id.item_sex_jdida, R.id.item_favori) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View superView = super.getView(position, convertView, parent);
            TextView sex2  = (TextView)superView.findViewById(R.id.item_sex_jdida);
            if(some_condition) {
                sex2.setVisibility(View.VISIBLE);
            } else {
                sex2.setVisibility(View.GONE);
            }
            return superView;
        }
    };
于 2013-10-08T09:30:07.103 回答