首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化ListView适配器以显示各个ListView项中的动态内容

优化ListView适配器以显示各个ListView项中的动态内容
EN

Stack Overflow用户
提问于 2015-01-23 17:56:43
回答 1查看 234关注 0票数 0

我正在尝试显示一个ListView,其中每个列表项都具有一个动态元素数。每个项目列表都有一个日期的TextView。在日期之后,可以有2-5个元素。

  • ListItemA
    • DateTextView
    • 2-5 TextView

  • ListItemB
    • DateTextView
    • 2-5 TextView

  • ListItemC
    • DateTextView
    • 2-5 TextView

..。

使用此当前方法,它在ListView的项的底部显示空白的TextViews。有什么更好的方法吗?

我认为convertView应该有方法向其添加布局元素,但事实并非如此。

适配器的getView方法:

代码语言:javascript
复制
@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder;


    if(convertView == null){
        holder = new ViewHolder();
        convertView = this.inflater.inflate(R.layout.layout_list_item, parent, false);

        holder.text_date_cashout = (TextView) convertView.findViewById(R.id.text_date_cashout);

        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout1));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout2));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout3));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout4));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout5));

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

    //get and set date from current cashOutHistory
    CashOutHistory coh = cashOutHistory.get(position);
    holder.text_date_cashout.setText(coh.getDate());

    //loop individual cashOuts to display their values
    int i = 0;
    for (CashOut co : coh.getCashOutHistory() ){
        holder.dynamic_views.get(i).setText(co.toString());
        i++;
    }

    return convertView;
}

private class ViewHolder{
    TextView text_date_cashout;
    ArrayList<TextView> dynamic_views = new ArrayList<TextView>(5);
}

列表项目布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_date_cashout" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout1" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout3" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout4" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout5" />

</LinearLayout>

编辑#1

循环显示并隐藏列表项中的TextViews:

代码语言:javascript
复制
//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
    holder.dynamic_views.get(i).setText(co.toString());
    i++;
}

while(i < 5){
    holder.dynamic_views.get(i).setVisibility(View.GONE);
    i++;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-23 18:15:51

您可以使用以下方法隐藏不需要为该特定项显示的每个视图:

代码语言:javascript
复制
Boolean someCondition = true;

if (someCondition)
    holder.dynamic_views.get(i).setVisibility(View.GONE);

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28115866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档