需要从列表视图中获取项目,使用自定义适配器和上下文菜单

时间:2018-06-06 17:00:29

标签: android listview contextmenu onitemclicklistener

我需要例如列表中的第二个病人,它始终是第一个位置,我的int总是0,当我点击任何位置时,没有办法获得真正的位置,这是我的代码:

public void setupGUI() {
    listView = findViewById(R.id.listView);
    if (getIntent().getParcelableExtra(Constants.PARCELABLE_TAG) != null) {
        user = getIntent().getParcelableExtra(Constants.PARCELABLE_TAG);
        GetDoctor gd = new GetDoctor(DoctorMenu.this, user.getId(), user.getToken());
        gd.execute();


        if (user != null) {
            userToken = user.getToken();
            FillPatientsTask fpt = new FillPatientsTask(DoctorMenu.this, this, userToken, null);
            fpt.execute();

        } else {
            userToken = "";
            Toast.makeText(this, "User was null", Toast.LENGTH_SHORT).show();
        }
    }
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            numberPatient = position;
        }
    });
    registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) menuInfo;

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_patient_menu, menu);
    patient = items.get(numberPatient);
}

1 个答案:

答案 0 :(得分:0)

为什么收藏从第一个位置开始为zero,这将是0 position。对于您的情况,请尝试如下:

numberPatient = position + 1;