简体   繁体   English

我可以从自定义适配器的getView填充一个listview吗?

[英]Can I populate a listview from getView of a custom adapter?

I have a listview that contains items that have listviews. 我有一个列表视图,其中包含具有列表视图的项目。 I am trying to populate item's listviews from inside the getView of the custom adapter that populates the "parent" listview: 我试图从填充“父”列表视图的自定义适配器的getView内填充项目的列表视图:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    System.out.println("session adapter: here1");
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        v = inflater.inflate(R.layout.single_session, null);


    }

    SessionObject i = sessions.get(position);

     if (i != null) {
         tvTrackName = (TextView) v.findViewById(R.id.textViewTrackName);
         tvTrackName.setText(i.trackName);
         tvSessionName = (TextView) v.findViewById(R.id.textViewSessionName);
         tvSessionName.setText(i.sessionName);

         tvSessionModerator = (TextView) v.findViewById(R.id.textViewModeratorName);
         tvSessionModerator.setText("Moderator: "+i.sessionModerator);

         listAbstracts = i.abstractList;

         lvAbstracts = (ListView) v.findViewById(R.id.listViewAbstracts);

         AbstractObjectAdapter adapter = new AbstractObjectAdapter(act, R.id.listViewAbstracts, listAbstracts);
         lvAbstracts.setAdapter(adapter);  
      }
     return v;  

}

The "interior" adapter seems to only be calling its getView function once, regardless of the number of items in its list. “内部”适配器似乎只调用一次其getView函数,而不管其列表中的项目数如何。 If there are 2 items, only the first gets put into the listview. 如果有2个项目,则只有第一个进入列表视图。 Is this the wrong way to do this? 这是错误的方法吗? Am I missing something? 我想念什么吗?

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Just create your item view dynamically, adding textviews to a linearlayout. 只需动态创建项目视图,即可将textviews添加到linearlayout。 You'll just have to be careful about handling the convertView, initially it will be easiest to always ingore it but you'll take a bit of a performance hit. 您只需要在处理convertView时小心一点,一开始最容易始终记住它,但是会给性能带来一些损失。

Optimise later. 稍后进行优化。

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

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