0

I have a ListView dynamically inflated with custom layout that has different text views and a button. On button click, a user specifies options in a dialog window and the data returned to the selected item in the list view.

I expect the data to get inserted in text views of the corresponding item in the list view. However, when the list has fewer item that are not scrollable, no data gets inserted at all when dialog is closed and when it has enough items to be scrollable, the data gets inserted into text views of wrong item in the list. I don't know what I am doing wrong, here is my code:

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;
        }
Dut A.
  • 1,029
  • 11
  • 22

1 Answers1

1

The method getView() return one item View each time ListView call it with a specific "position" parameter, so the times it is called is no less than the number of items it has. So, your member variables mPTv, mCoTv and mSubMain will be reassigned many times and finally have their values in the final calling of getView().That's why you meet that problem.

To solve your problem, you need to have the correct TextViews when the "save Button" is clicked.Here is how to find those correct TextViews:

In the OnClickListener of mModify Button, the "v" parameter in the method onClick(View v) is the same instance of mModify itself. So, you can use it to find those correct TextViews. Perhaps you can set the item root View as its tag and get the root View in onClick(View v) and then use it to find all those correct TextViews. Here is how to change your code:

    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);

BTW, you didn't optimize your ListView, so it may not scroll smoothly, but this is not what caused your problem.You can refer to this: How to optimize Android ListView?

Community
  • 1
  • 1
Lei Guo
  • 2,550
  • 1
  • 17
  • 22