在Android中的Dialog中将Text设置为ListView中的TextViews

时间:2014-11-04 10:50:50

标签: android listview android-listview getview

我的ListView动态充斥着自定义布局,其中包含不同的文字视图按钮。在按钮单击时,用户指定对话框窗口中的选项,并将数据返回到列表视图中的选定项目。

我希望数据插入列表视图中相应项的文本视图中。但是,当列表具有较少的不可滚动的项目时,在关闭对话框时没有数据插入,并且当有足够的项目可滚动时,数据将插入到列表中错误项目的文本视图中。我不知道我做错了什么,这是我的代码:

public double COST_EM = 0.00, COST = 0.00, NUM_CO = 0.00;
private Spinner mPSpinner, mMediSpinner;
private ArrayAdapter<String> pmedi, numCo;
private TextView mPTv, mCoTv, mSubMain, mSubTv;

@Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View view = null;
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_custom_layout, parent, false);

            // Display image in the ImageView widget
            TempP tphoto = tempPList.get(position);


            mPTv = (TextView) view.findViewById(R.id.p_tv_po);
            mCoTv = (TextView) view.findViewById(R.id.co_tv_po);
            mSubMain = (TextView) view.findViewById(R.id.sub_list);

            mModify = (Button) view.findViewById(R.id.modify_btn_po);
            mModify.setOnClickListener(new View.OnClickListener() {

                // calculate the order cost
                protected void calculateCost() {

                    mSub = ((COST_EM + COST) * NUM_CO);

                    mSubtv.setText("" + Double.toString(mSub));
                }

                @Override
                public void onClick(View v) {

                    mDialog = new Dialog(PCustom.this);
                    mDialog.setContentView(R.layout.item_custom_dialog);
                    mDialog.setTitle("Modify specifications");

                    mPSpinner = (Spinner) mDialog.findViewById(R.id.ces_sp_dialog);
                    mSubTv = (TextView) mDialog.findViewById(R.id.sub_der_dialog);

                    // set spinner adapters (code truncated for brevity)
                    pmedi = new ArrayAdapter<String>(MyApplication
                            .getContext(), R.layout.spinner_style, pmedi);
                    mPSpinner.setAdapter(pmedi);

                    mPSpinner
                            .setOnItemSelectedListener(new OnItemSelectedListener() {
                                public void onItemSelected(AdapterView<?> arg0,
                                        View arg1, int pos, long arg3) {

                                    if (mPSpinner.getSelectedItem() == "Glossy") {
                                        COST = 2000.00;
                                    } else if (mPSpinner
                                            .getSelectedItem() == "Standard") {
                                        COST = 1500.00;
                                    } else if (mPSpinner
                                            .getSelectedItem() == "Other") {
                                        COST = 1000.00;
                                    }

                                    // calculate the cost
                                    calculateCost();
                                }

                                public void onNothingSelected(
                                        AdapterView<?> arg0) {
                                }
                            });

                    Button save = (Button) mDialog
                            .findViewById(R.id.save_btn_po_dialog);
                    Button cancel = (Button) mDialog
                            .findViewById(R.id.cancel_btn_po_dialog);

                    save.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            // String newnumber =
                            // mobileNumber.getText().toString();

                            mPTv.setText("" + mPSpinner
                                    .getSelectedItem());
                            mCoTv.setText((String) mNCoSpinner
                                    .getSelectedItem());
                            mSubMain.setText(Double.toString(mSub));

                            mDialog.dismiss();
                        }
                    });

                    cancel.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                        }
                    });

                    mDialog.show();

                }
            });

            return view;
        }

1 个答案:

答案 0 :(得分:1)

方法getView()返回一个项目ViewView每次使用特定的&#34;位置&#34;参数,因此调用它的次数不少于它所拥有的项目数。因此,你的成员变量mPTv,mCoTv和mSubMain将被重新分配多次,并最终在最终调用getView()时得到它们的值。这就是你遇到这个问题的原因。

要解决您的问题,您需要在&#34;保存按钮&#34;时使用正确的TextView。单击。这是如何找到正确的TextViews:

在mModify按钮的OnClickListener中,&#34; v&#34; onClick(View v)方法中的参数与mModify本身的实例相同。因此,您可以使用它来查找正确的TextView。也许您可以将项目根视图设置为其标记,并在onClick(View v)中获取根视图,然后使用它查找所有正确的TextView。以下是更改代码的方法:

    mModify = (Button) view.findViewById(R.id.modify_btn_po);
    // set the root view as the tag of "modify button".
    mModify.setTag(view);
    mModify.setOnClickListener(new View.OnClickListener() {

        // calculate the order cost
        protected void calculateCost() {

            mSub = ((COST_EM + COST) * NUM_CO);

            mSubtv.setText("" + Double.toString(mSub));
        }

        @Override
        public void onClick(View v) {
            // retrieve the root view here, it's the root view of the item on which you    
            // clicked the "modify button".
            View view = (View)v.getTag();
            // find the correct TextViews here.
            mPTv = (TextView) view.findViewById(R.id.p_tv_po);
            mCoTv = (TextView) view.findViewById(R.id.co_tv_po);
            mSubMain = (TextView) view.findViewById(R.id.sub_list);
顺便说一下,你没有优化你的ListView,所以它可能无法顺利滚动,但这不是导致你的问题的原因。你可以参考这个: How to optimize Android ListView?