在我在Android上创建上下文菜单时选择的项目之后获取项目

时间:2017-06-14 22:45:56

标签: android listview realm contextmenu

基本上我有一个显示列表视图的活动,它有一个上下文菜单。问题是我在选择项目之后得到了项目的上下文菜单,这意味着如果我长按项目1,我会得到项目2的上下文菜单,如果我长时间点击最后一项,那么应用程序崩溃是因为它试图获取一个甚至不存在的项目。长按单击2项列表中的第二项时,logcat会抛出此错误:

java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 2 > 2

我不认为我做了与我的应用中的任何其他列表视图不同的事情,所以这对我来说真的很奇怪。

以下是相关代码:

    public class DetailsActivity extends AppCompatActivity implements RealmChangeListener<RealmList<Detail>>{

        private ListView listViewDetails;
        private Realm realm;
        private RealmList<Detail> details;
        private Routine routine;
        private DetailsAdapter adapter;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_details);

            Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            int id = getIntent().getExtras().getInt("id");

            realm = Realm.getDefaultInstance();
            routine = realm.where(Routine.class).equalTo("id",id).findFirst();
            details = routine.getDetails();

            getSupportActionBar().setTitle(routine.getName());

            details.addChangeListener(this);

            adapter = new DetailsAdapter(this,details,R.layout.detail_item);
            listViewDetails = (ListView) findViewById(R.id.listViewDetails);

            listViewDetails.setAdapter(adapter);

            registerForContextMenu(listViewDetails);
        }

        @Override
        public void onChange(RealmList<Detail> element) {
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

        menu.setHeaderTitle(details.get(info.position).getExercise());
        getMenuInflater().inflate(R.menu.context_menu,menu);
        MenuItem itemDelete = menu.findItem(R.id.itemDelete);
        MenuItem itemEdit = menu.findItem(R.id.itemEdit);
        itemDelete.setTitle(R.string.delete);
        itemEdit.setTitle(R.string.edit);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()){

            case R.id.itemDelete:
                deleteDialog(details.get(info.position));
                return true;

            case R.id.itemEdit:
                editDialog(details.get(info.position));
        }
        return super.onContextItemSelected(item);
    }

}

DetailsAdapter:

public class DetailsAdapter extends BaseAdapter {

    private List<Detail> list;
    private int layout;
    private Context context;

    public DetailsAdapter(Context context, List<Detail> list, int layout){

        this.context = context;
        this.list = list;
        this.layout = layout;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

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

        ViewHolder vh;

        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(layout,parent,false);
            vh = new ViewHolder();
            vh.textViewExercise = (TextView) convertView.findViewById(R.id.textViewExercise);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }

        Detail detail = list.get(position);

        vh.textViewExercise.setText(detail.getExercise());

        return convertView;
    }

    public class ViewHolder{
        public TextView textViewExercise;

    }

}

0 个答案:

没有答案