简体   繁体   English

如何创建动态列表视图并设置Textview的可见性

[英]how to create dynamic listview and set visibility of Textview

I'm new to Android development. 我是Android开发的新手。 I successfully created a demo project with a dynamic listview but now my problem is that I have two textviews in my list_row xml file. 我成功创建了一个带有动态列表视图的演示项目,但是现在我的问题是我的list_row xml文件中有两个list_row

Textview2 is default hide(Visibility=Gone). Textview2是默认的hide(Visibility = Gone)。

Now I set the visibility of that textview visible only in fifth position in listview. 现在,我将该文本视图的可见性设置为仅在列表视图的第五个位置可见。 I wrote code for that but the textview shows up randomly, not just in the fifth position. 我为此编写了代码,但textview随机显示,而不仅仅是第五个位置。 I have 100 records in the listview. 我在列表视图中有100条记录。

What am I doing wrong? 我究竟做错了什么?

EDIT : 编辑:

public View getView(int position, View convertView, ViewGroup parent) 
{
    // TODO Auto-generated method stub
    ViewHolder holder;

    if (convertView == null) 
    {
        convertView = mLayoutInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.mytext);
        holder.text2 = (TextView) convertView.findViewById(R.id.invisibletext);
        convertView.setTag(holder);
        if (position == 5) 
            holder.text2.setVisibility(View.VISIBLE);
        else
            holder.text2.setVisibility(View.GONE);
    } 
    else 
    {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

static class ViewHolder 
{
    TextView text1;
    TextView text2;
}

the issue is that convertViews in the Adapter are recycled. 问题是适配器中的convertViews被回收了。

I'm assuming you're testing for position in the getView() function in your adapter. 我假设您正在测试适配器的getView()函数中的位置。 Modify your code so that it does the following: 修改您的代码,使其执行以下操作:

if (position == 4)
    convertView.setVisibility(View.INVISIBLE);
else 
    convertView.setVisibility(View.VISIBLE);

this will force the convertView to reappear after being set to visible in the 5th position 这将迫使convertView在第5个位置设置为可见后重新出现

Edit: the if else condition is in the wrong place 编辑:如果其他条件在错误的位置

 public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.list_row, null);

        holder = new ViewHolder();

        holder.text1 = (TextView) convertView.findViewById(R.id.mytext);

        holder.text2 = (TextView) convertView

        .findViewById(R.id.invisibletext);

        convertView.setTag(holder);


    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    if (position == 5) {
        holder.text2.setVisibility(View.INVISIBLE);
    } else {
        holder.text2.setVisibility(View.VISIBLE);
    }

    return convertView;

}

}

static class ViewHolder {
TextView text1;

TextView text2;

}

if your convertView is not null then if-else condtion not fire, So when convertView is null then its fire so it works randomly. 如果您的convertView不为null,则不会触发if-else条件,因此,当convertView为null时,将触发它,因此它可以随机工作。

Update your code as per below... 根据以下内容更新代码...

   public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_row, null);

            holder = new ViewHolder();

            convertView.setTag(holder);
         } else {
                holder = (ViewHolder) convertView.getTag();
            }


      holder.text1 = (TextView) convertView.findViewById(R.id.mytext);
      holder.text2 = (TextView) convertView.findViewById(R.id.invisibletext);

      if (position == 5) {
                holder.text2.setVisibility(View.VISIBLE);

            } else {
                holder.text2.setVisibility(View.GONE);
            }

        return convertView;

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM