1

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.

4
  • Sir, are you adding a listview in a listview? why not expandable listview? Commented Apr 28, 2015 at 20:09
  • Hey, yeah I'm doing like it. I won't use ExpandableListView because I can't make a change in code base now and I already set 3 views in this list, except of nested listview. Commented Apr 28, 2015 at 20:14
  • Where are you calling notifyDataSetChanged()? Commented Apr 28, 2015 at 20:31
  • I tried it in add(PlaceModel object) method. Commented Apr 28, 2015 at 20:33

1 Answer 1

0

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();
    }     
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.