Android的嵌套的ListView适配器只加入第一elemet [英] Android nested listview adapter adding only first elemet

查看:349
本文介绍了Android的嵌套的ListView适配器只加入第一elemet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是在这样的名单列表:

My purpose is a list in a list like this:

-alllist--------
----row---------
----row---------
-----(list)-----
-------textview
-------textview
---row---------
-----(list)----
.....

MainAdapter类是工作的罚款与其他元素,但PlaceAdapter只添加第一个(或最后,我coudn't图)项的嵌套列表中。

MainAdapter Class is working fine with other elements but PlaceAdapter adding only first(or last, I coudn't figure) item to the 'nested' list.

这是什么问题?我至少花了我5小时找到它。

What is the problem? I spent at least my 5 hours to find it.

MainAdapter:

MainAdapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

   PlaceHolder placeHolder = null;

   if (convertView == null) {
     placeHolder = new PlaceHolder();
     convertView =      mInflater.inflate(R.layout.convers_place,  null);
     placeHolder.listView = (ListView) convertView.findViewById(R.id.listView);
     convertView.setTag(placeHolder);

   } else {
      placeHolder = (PlaceHolder)convertView.getTag();
}


PlaceAdapter adapter = new PlaceAdapter(ctx);
placeHolder.listView.setAdapter(adapter);
String arr[] = conversion.message.split(Pattern.quote("!!")); // Not empty
adapter.add(new PlaceModel(arr[0]));
adapter.add(new PlaceModel(arr[1]));

}

PlaceAdapter:

PlaceAdapter:

public class PlaceAdapter extends BaseAdapter {

    Context ctx;
    private List<PlaceModel> places = new ArrayList<>();
    private LayoutInflater mInflater;

    public PlaceAdapter(Context ctx){
       this.ctx = ctx;
       mInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void add(PlaceModel object) {
        this.places.add(object);
    }

    @Override
    public int getCount() {
        return this.places.size();
    }

    @Override
    public PlaceModel getItem(int position) {
        return this.places.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        PlaceHolder_ placeHolder = null;

        PlaceModel place = getItem(position);

        if (convertView == null) {
            placeHolder = new PlaceHolder_();
            convertView = mInflater.inflate(R.layout.convers_place_row,  null);
            placeHolder.name= (TextView)convertView.findViewById(R.id.row_name);
            placeHolder.address= (TextView)convertView.findViewById(R.id.row_address);
            placeHolder.map= (Button)convertView.findViewById(R.id.row_map);
            convertView.setTag(placeHolder);
        }

       else {
            placeHolder = (PlaceHolder_)convertView.getTag();
       }

        placeHolder.name.setText(place.getName());
        placeHolder.address.setText(place.getAddress());

        return convertView;
    }

    public static class PlaceHolder_ {
        public TextView name;
        public TextView address;
        public Button map;
    }

}

编辑: notifyDataSetChanged()不影响

推荐答案

我有一个类似的问题,当我把的ListView 滚动型

I've a similar problem when i put a ListView inside a ScrollView.

问题是:设置我的的ListView 可滚动视图中为 WRAP_CONTENT 滚动型在我的情况)只会使跨越周围的第一个孩子,而其他人将在溢出。

The problem was: setting my ListView to wrap_content inside a scrollable view (ScrollView in my case) will only make it span around the first child while the others would be in the overflow.

至于解决方法:更新适配器的数据(如添加新项),我也更新从code中的的ListView 的高度(基于儿童)后

As workaround: after updating adapter data (e.g adding new item), i also update the ListView's height (based on children) from code.

这是一个 的例子:

public class Utils {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }     
}

这篇关于Android的嵌套的ListView适配器只加入第一elemet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆