如何获取活动中列表项的单击位置,但其适配器不在同一活动中

时间:2015-05-21 05:31:59

标签: android

btnUpdateMedicine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final MedicineModel medicine = new MedicineModel();

                adapterMedicine.remove(adapterMedicine.getItem(1));
                adapterMedicine.insert(medicine, 1);

}

适配器:         公共类MedicineAdapter扩展ArrayAdapter {         上下文背景;         布尔启用;

public MedicineAdapter(Context context, int resourceId,
        List<MedicineModel> item, boolean enabled) {
    super(context, resourceId, item);
    this.enabled = enabled;
    this.context = context;
}

/* private view holder class */
private static class ViewHolder {
    private TextView txtName;
    private TextView txtStrength;

}

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

    final MedicineModel rowItem = getItem(position);

    View row = null;
    if (convertView == null) {
        ViewHolder holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.row_medicine, null);
        holder.txtName = (TextView) row.findViewById(R.id.txt_drug_name);
        holder.txtStrength = (TextView) row.findViewById(R.id.txt_strength);

        row.setTag(holder);
    } else {
        row = convertView;
    }
    final ViewHolder holder = (ViewHolder) row.getTag();
    holder.txtName.setText(rowItem.name);
    holder.txtStrength.setText(rowItem.strength);

    if (enabled) {
        holder.btnDeleteMedicine.setVisibility(View.VISIBLE);
    } else {
        holder.btnDeleteMedicine.setVisibility(View.GONE);
    }
    holder.btnDeleteMedicine.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            remove(rowItem);
            notifyDataSetChanged();
        }
    });

    return row;
}

}

的活动:

     adapterMedicine = new MedicineAdapter(this, R.layout.row_medicine,
                prescription.arrMedicine, true);
        listPrescription.setAdapter(adapterMedicine);

        final TextView btnUpdateMedicine = (TextView) findViewById(R.id.btn_update_medicine);

        listPrescription.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                MedicineModel med = adapterMedicine.getItem(arg2 - 1);
                editName.setText(med.name);
                editStrength.setText(med.strength);

                btnUpdateMedicine.setVisibility(View.VISIBLE);
            }
        });

        btnUpdateMedicine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final MedicineModel medicine = new MedicineModel();

                adapterMedicine.remove(adapterMedicine.getItem(1));
                adapterMedicine.insert(medicine, 1);
}

适配器药物在不同的java文件中但在同一个包中。

如何在这种情况下找到位置(我用过1)?

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position, long id){

    // DO STUFF HERE

    }
});

position正是您所需要的 - 来自listVIew的点击项目

或只是覆盖onListItemClick方法

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);  

    Log.i("the Item clicked is at position : ",  position);
}

答案 1 :(得分:0)

无需为列表项设置onClickListener即可将onItemClickListener用于listview

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        //TODO handle event
    }
});

答案 2 :(得分:0)

 adapterMedicine = new MedicineAdapter(this, R.layout.row_medicine,
                prescription.arrMedicine, true);
        listPrescription.setAdapter(adapterMedicine);
        int selected_pos = -1;
        final TextView btnUpdateMedicine = (TextView) findViewById(R.id.btn_update_medicine);

        listPrescription.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                selected_pos = arg2-1;
                MedicineModel med = adapterMedicine.getItem(arg2 - 1);
                editName.setText(med.name);
                editStrength.setText(med.strength);

                btnUpdateMedicine.setVisibility(View.VISIBLE);
            }
        });

        btnUpdateMedicine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final MedicineModel medicine = new MedicineModel();

                adapterMedicine.remove(adapterMedicine.getItem(selected_pos));
                adapterMedicine.insert(medicine, selected_pos);
}

检查 selected_pos 值的使用情况。