Android nested listview adapter adding only first element

无人久伴 提交于 2020-02-06 05:40:46

问题


My purpose is a list in a list like this:

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

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

What is the problem?

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() is not affected.


回答1:


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

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.

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

This is an example:

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();
    }     
}


来源:https://stackoverflow.com/questions/29927545/android-nested-listview-adapter-adding-only-first-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!