从我的gridView中删除所选元素

时间:2016-10-17 06:43:33

标签: java android gridview

如何删除gridView列表中的所选项? 现在我删除了我最后添加的元素。

 @Override
        public void onClick(View view) {
            remove(personItems);
            notifyDataSetChanged();
        }

我尝试这样做

public View getView(final int position, View convertView, ViewGroup parent) {
    personItems = getItem(position);
...
public void onClick(View view) {
            personItems.remove(position);
            notifyDataSetChanged();
        }

但是这样我就无法从网格视图中删除元素

我的适配器的完整代码

public class CustomAdapter extends ArrayAdapter<PersonItems> {

PersonItems personItems;

static class ViewHolder {
    TextView member_name;
    ImageView profile_pic;
}

public CustomAdapter(Context context, ArrayList<PersonItems> users) {
    super(context, R.layout.list_item, users);
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    personItems = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.list_item, parent, false);
        viewHolder.member_name = (TextView) convertView.findViewById(R.id.member_name);
        viewHolder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.member_name.setText(personItems.name);
    viewHolder.profile_pic.setImageResource(setImage());

    Button buttonDelete = (Button) convertView.findViewById(R.id.buttonDelete);
    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            remove(personItems);
            notifyDataSetChanged();
        }
    });
    return convertView;
}

public int setImage() {
    switch (personItems.getName()) {
        case Names.AII:
            return R.drawable.profile;
        case Names.VdV:
            return R.drawable.vdm;
    }
    return 0;
}

}

2 个答案:

答案 0 :(得分:0)

在适配器的构造函数中,从Activity:

接收列表视图
 console.error node_modules/core-js/modules/es6.promise.js:117
      Unhandled promise rejection [Error: expect(jest.fn())[.not].toHaveBeenCalledWith()

      jest.fn() value must be a mock function or spy.
      Received:
        object: {"addMatchers": [Function anonymous], "autoMockOff": [Function anonymous], "autoMockOn": [Function anonymous], "clearAllMocks": [Function anonymous], "clearAllTimers": [Function anonymous], "deepUnmock": [Function anonymous], "disableAutomock": [Function anonymous], "doMock": [Function anonymous], "dontMock": [Function anonymous], "enableAutomock": [Function anonymous], "fn": [Function anonymous], "genMockFn": [Function bound getMockFunction], "genMockFromModule": [Function anonymous], "genMockFunction": [Function bound getMockFunction], "isMockFunction": [Function isMockFunction], "mock": [Function anonymous], "resetModuleRegistry": [Function anonymous], "resetModules": [Function anonymous], "runAllImmediates": [Function anonymous], "runAllTicks": [Function anonymous], "runAllTimers": [Function anonymous], "runOnlyPendingTimers": [Function anonymous], "runTimersToTime": [Function anonymous], "setMock": [Function anonymous], "unmock": [Function anonymous], "useFakeTimers": [Function anonymous], "useRealTimers": [Function anonymous]}]

您的适配器的构造函数应如下所示:

MyCustomAdapter adapter = new MyCustomAdapter(context, mylist);

现在在你的getView()方法中:

public MyCustomAdapter (Context context, List<Object> objectsList){
    this.objectsList = objectsList;
    this.context = context;    
}

希望这有帮助。

答案 1 :(得分:0)

获取当前网格视图的位置并执行删除方法