listview适配器在删除项目后不更新

时间:2015-11-27 07:30:26

标签: android listview android-listview android-adapter

你好我试图从我的列表视图中删除一个项目能够从我的数据库中删除项目,但我的适配器没有得到更新我已经附上了下面的代码。

public class SavedPolygonFragment extends android.support.v4.app.Fragment implements RefreshListener {
private static String LOG_TAG = SavedPolygonFragment.class.getName();

private String message = "No message";
private ListView          shapeList;
private TextView          textView;
private Button            startMainBtn;
private SavedShapeAdapter savedShapeAdapter;
private long              shapeId;
private float             scaleFactor;
private List<Shape> savedShapes = new ArrayList<>();
private StartDrawingListener startListener;
private DeleteShapesDialog   deleteDialog;

public SavedPolygonFragment() {

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    startListener = (StartDrawingListener) activity;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    savedShapes = getArguments().getParcelableArrayList("shapes");
    AssetLog.verbose(LOG_TAG, "savedshapes.size " + savedShapes.size());
    shapeList = (ListView) view.findViewById(R.id.shapeList);
    textView = (TextView) view.findViewById(R.id.polygonTitle);
    startMainBtn = (Button) view.findViewById(R.id.gotoMain);

    startMainBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startListener.startMainReceived();
        }
    });

    if (!savedShapes.isEmpty() && !savedShapes.equals(null) && savedShapes.size() != 0) {
        textView.setText("Polygons");
        shapeList.setVisibility(View.VISIBLE);
        startMainBtn.setVisibility(View.GONE);


        if (savedShapeAdapter != null) {
            savedShapeAdapter.notifyDataSetChanged();
        } else {

            savedShapeAdapter = new SavedShapeAdapter(savedShapes, getActivity());
            shapeList.setAdapter(savedShapeAdapter);

            shapeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    MainActivity_.intent(getActivity())
                            .savedProjectId(ProjectShapesActivity.projId)
                            .scaleFactor(ProjectShapesActivity.scaleFactor)
                            .shapeId(((Shape) view.getTag()).getShapeId())
                            .lineId(-1)
                            .pointId(-1)
                            .start();
                }
            });


        }
    }
    else{
        shapeList.setVisibility(View.GONE);
        startMainBtn.setVisibility(View.VISIBLE);

        textView.setText("Sorry no shapes are saved");
    }

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    registerForContextMenu(shapeList);
}

@Override
public void onRefreshPressed() {
}

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    shapeId = info.id;
    menu.add(Menu.NONE, R.id.deleteShapes, Menu.NONE, "Delete");
    menu.add(Menu.NONE, R.id.editShapes, Menu.NONE, "Edit");

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.refresh:
            return false;

        case R.id.deleteShapes:
            if(deleteDialog == null){
                Bundle bundle = new Bundle();
                bundle.putString("SHAPE", "SHAPE");
                bundle.putLong("shapeId", shapeId);
                deleteDialog = new DeleteShapesDialog();
                deleteDialog.setArguments(bundle);
                deleteDialog.show(getActivity().getFragmentManager().beginTransaction(), "showDeleteDialog");
            }
            return true;
        case  R.id.editShapes:
            return true;

    }
    return super.onContextItemSelected(item);
}


 public void onDataChanged(long shapeid) {

    Shape shape;
    for (int i = 0; i <savedShapes.size() ; i++) {
        shape = savedShapes.get(i);
        if(shape.getShapeId() == shapeid){
            savedShapes.remove(i);
            shapeList.removeViewAt(i);
            shapeList.invalidate();
            savedShapeAdapter.notifyDataSetChanged();
            break;
        }
    }
}

} `

现在,当用户从 deleteDialog 中选择“是”时,我写了一个回调给我的活动,并在相应的方法中这就是我的工作

SavedPolygonFragment polygonFragment = new SavedPolygonFragment();
 polygonFragment.onDataChanged(shapeId);

有人可以告诉我什么是错的

/////编辑1

 public void onDataChanged(long shapeid) {

     savedShapeAdapter.removeItemById(shapeid);

}

和我的适配器

public void removeItemById(long itemId){
    for(int i =0; i< shapeList.size() ; i++){
        if(shapeList.get(i).getShapeId() == itemId){
            shapeList.remove(i);
        }
    }
    notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:1)

  

能够从我的数据库中删除项目,但我的适配器没有获取   更新

问题是由:

引起的
savedShapes.remove(i);

线。

目前正在从SavedPolygonFragment类中的ArrayList中删除项目,而不是从适配器用来填充ListView的ArrayList中删除项目。

使用当前的方法尝试这种方式将通过每次设置新的适配器来工作:

       if(shape.getShapeId() == shapeid){
            savedShapes.remove(i);
            savedShapeAdapter = new SavedShapeAdapter(savedShapes, getActivity());
            shapeList.setAdapter(savedShapeAdapter);
            savedShapeAdapter.notifyDataSetChanged();
            break;
        }

但是,当想要更新ListView时,这不是每次创建和设置适配器新对象的优化解决方案。

正确的方法是在SavedShapeAdapter类中创建一个方法,用于删除适配器使用的ArrayList项,然后在onDataChanged方法中调用它:

public void deleteRowItem(int index){
  this.savedShapes.remove(index);
  this..notifyDataSetChanged();
}

现在想要从ListView中删除行项目时调用deleteRowItem

if(shape.getShapeId() == shapeid){
    savedShapeAdapter.deleteRowItem(i);
    break;
 }