android listview order changed when called notifyDataSetChanged

China☆狼群 提交于 2019-12-22 19:08:21

问题


all. when I use notifyDataSetChanged(), the listview display order will be change .

like this

  • 3
  • 2
  • 1

    when current activy was created. but when I change the data. it will be

  • 1
  • 2
  • 3

I don't want the order changed and i dont understand why its happening.

This is a piece of code from my adapter class

    public static class ItemAdapter extends BaseAdapter {

        private String[] mData;
        private LayoutInflater mInflater;

// I called this method  to change data
        public void setEditText(int position, final String item) {
            mData[position] = item;
            notifyDataSetChanged();
        }



}

I change data at some dialog like this

       builder = new AlertDialog.Builder(ct);
                            builder.setTitle(R.string.pickStatus)
                                    .setView(edBuffer)
                                    .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog, int id) {
                                                    // TODO Auto-generated method stub
                                                    canPop = true;
                                                    final String tmp = edBuffer.getText().toString();
                                                    KbonezLog.e(String.format( "set into key %d", key)); 
//use mData key to set value
setEditText(key, tmp);
dialog.dismiss();
}})

回答1:


It's hard to tell what's going on without seeing the full source code for your adapter, but this sounds like an issue in your implementation of getView(). Every time getView() is invoked you must rebind all the data.




回答2:


I've encountered a similiar problem here

I solved it by always creating a new ViewHolder in getView() implemantation.

So I think Romain Guy provide the correct way to solving such kinds of problems.

Hope it works.




回答3:


It looks to me that in setEditText you are actually changing the list contents, so that might be a good place to start looking for issues.




回答4:


i think instead of using private String[] mData;, You should use following,

private ArrayList mData = new ArrayList();
mData.add(item);

In my case, it works and does not change data, i hope you will solve your problem from this.



来源:https://stackoverflow.com/questions/5049613/android-listview-order-changed-when-called-notifydatasetchanged

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