-2
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.vote_select_listview_item, null);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(R.id.AnnouncementDetailVoteListViewAdapter_view,
                viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView
                .getTag(R.id.AnnouncementDetailVoteListViewAdapter_view);
    }

convertView not null but viewholder is null because i add a new data call notifyDataSetChanged, the next action like this:

            mVoteItems.add(response4.data);
            notice_vote_adapter.notifyDataSetChanged();
            viewHeightUtil.setListViewHeight(notice_vote_options_lv);

the problem is I need dynamic change ListView height, if annotation it then there is no problem, I find issue here:

public void setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

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

        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new AbsListView.LayoutParams(
                    AbsListView.LayoutParams.WRAP_CONTENT,
                    AbsListView.LayoutParams.WRAP_CONTENT));
        }
        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();
}
herbertD
  • 10,657
  • 13
  • 50
  • 77
  • Post the full logic about getView() and notifyDataSetChanged. – herbertD Jan 13 '15 at 03:00
  • I find the problem at when notifyDataSetChanged i call mVoteItems.add(response4.data); notice_vote_adapter.notifyDataSetChanged(); viewHeightUtil.setListViewHeight(notice_vote_options_lv); – Qianglin Ao Jan 13 '15 at 05:10
  • edit problem again ,tkx – Qianglin Ao Jan 13 '15 at 05:18
  • It is not a good way to set list item height out of list adapter, try to set a height param before calling nofifyDataSetChanged(), then let getView() to do the refreshment. – herbertD Jan 14 '15 at 02:46

1 Answers1

0
if(convertView==null){
    LayoutInflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    viewholder =  inflater.inflate(R.layout.row, null);
}

Try this

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80