Android嵌套listview适配器仅添加第一个元素

时间:2015-04-28 18:50:27

标签: android listview android-adapter baseadapter

我的目的是列表如下:

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

MainAdapter类与其他元素一起正常工作,但PlaceAdapter只将第一个(或最后一个,我无法想象)项添加到'嵌套'列表中。

有什么问题?

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:

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()不受影响。

1 个答案:

答案 0 :(得分:0)

当我在 <div class="row"> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pull-left"> //Main content here on left of page </div> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pull-right"> //Content on right of page for large and medium devices </div> </div> 内放置ListView时,我遇到了类似的问题。

问题是:在可滚动视图中设置我的ScrollViewListView(在我的情况下为wrap_content)只会让它跨越第一个孩子,而其他孩子会在溢出。

作为解决方法:在更新适配器数据(例如添加新项目)之后,我还会从代码更新ScrollView的高度(基于子项)。

这是example

ListView