0

我有列表视图,其中包括四个文本视图。我的看法如下 目前看来

我想在对话框值的帮助下根据特定项目手动设置数量我的代码如下

public class OrderFragment extends Fragment {

    Spinner mSpinnerMeal;

    OrderAdapter mOrderAdpter;
    ArrayList<ParserOrder>mArrayList;
    ListView mListView;
    TextView mTextViewSubmit;
    Dialog mDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View mView=inflater.inflate(R.layout.order, container, false);
        mSpinnerMeal=(Spinner)mView.findViewById(R.id.order_spn_meal_type);
        mArrayList=new ArrayList<ParserOrder>();
        StaticValue.noOfFragment=2;
        mDialog=new Dialog(getActivity(),android.R.style.Theme_NoTitleBar|android.R.style.Theme_Holo|android.R.style.Theme_Translucent);
        mDialog.setCancelable(false);
        mListView=(ListView)mView.findViewById(R.id.order_listview);
        mTextViewSubmit=(TextView)mView.findViewById(R.id.order_txt_submit);
        mTextViewSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

        mOrderAdpter=new OrderAdapter();
        mListView.setAdapter(mOrderAdpter);
        return mView;
    }

    public class OrderAdapter extends BaseAdapter
    {
        ArrayList<ParserOrder >mList;
        FragmentActivity mActivity;
        ViewHolder mViewHolder;
        LayoutInflater inflater;

        public OrderAdapter() {
            // TODO Auto-generated constructor stub
            inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return RestaurantApplication.mParserOrder.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            mViewHolder=new ViewHolder();
            convertView = inflater.inflate(R.layout.row_order, parent, false);
            mViewHolder.mTextViewItenam=(TextView)convertView.findViewById(R.id.row_order_iteamname);
            mViewHolder.mTextViewPrice=(TextView)convertView.findViewById(R.id.row_order_price);
            mViewHolder.mTextViewTotal=(TextView)convertView.findViewById(R.id.row_order_txt_total);
            mViewHolder.mTextViewQty=(TextView)convertView.findViewById(R.id.row_order_edt_qty);
            mViewHolder.mTextViewItenam.setText(RestaurantApplication.mParserOrder.get(position).getIteamname());
            mViewHolder.mTextViewPrice.setText(RestaurantApplication.mParserOrder.get(position).getIteamprice());
            mViewHolder.mTextViewQty.setId(position);

            mViewHolder.mTextViewQty.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mDialog.setContentView(R.layout.dialog_qty);
                    final EditText  mEditTextQty=(EditText)mDialog.findViewById(R.id.dialog_edt);
                    TextView  mTextViewOk=(TextView)mDialog.findViewById(R.id.dialog_txt_ok);
                    TextView mTextViewCancel=(TextView)mDialog.findViewById(R.id.dialog_txt_cancle);
                    mTextViewOk.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            mViewHolder.mTextViewQty.setText(mEditTextQty.getText().toString().trim());
                            mDialog.dismiss();
                        }
                    });
                    mTextViewCancel.setOnClickListener(new View.OnClickListener() {

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

            mViewHolder.mTextViewQty.setTag(position);
            if (convertView==null) {
                mViewHolder.mTextViewQty.getTag();
            }
            return convertView;
        }
    }
    public class ViewHolder
    {
        TextView mTextViewItenam;
        TextView mTextViewPrice;
        TextView mTextViewQty;
        TextView mTextViewTotal;
    }
}

现在的问题是当我运行此代码并单击任何数量的文本视图时,它只会在文本视图的最后一次设置值,当我再次单击任何文本视图并尝试在文本中设置值时,它不允许在任何文本框中设置该值想法我该如何解决这个问题?

编辑代码

根据您的建议,我将代码更改如下

public class OrderFragment extends Fragment {

    Spinner mSpinnerMeal;

    OrderAdapter mOrderAdpter;
    ArrayList<ParserOrder>mArrayList;
    ListView mListView;
    TextView mTextViewSubmit;
     Dialog mDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View mView=inflater.inflate(R.layout.order, container, false);
        mSpinnerMeal=(Spinner)mView.findViewById(R.id.order_spn_meal_type);
        mArrayList=new ArrayList<ParserOrder>();
        StaticValue.noOfFragment=2;
        mDialog=new Dialog(getActivity(),android.R.style.Theme_NoTitleBar|android.R.style.Theme_Holo|android.R.style.Theme_Translucent);
        mDialog.setCancelable(false);
        mListView=(ListView)mView.findViewById(R.id.order_listview);
        mTextViewSubmit=(TextView)mView.findViewById(R.id.order_txt_submit);
        mTextViewSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });

        mOrderAdpter=new OrderAdapter();
        mListView.setAdapter(mOrderAdpter);
        return mView;
    }

    public class OrderAdapter extends BaseAdapter
    {

        ArrayList<ParserOrder >mList;
        FragmentActivity mActivity;
        ViewHolder mViewHolder;
        LayoutInflater inflater;

        public OrderAdapter() {
            // TODO Auto-generated constructor stub
            inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return RestaurantApplication.mParserOrder.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            mViewHolder=new ViewHolder();
            convertView = inflater.inflate(R.layout.row_order, parent, false);
            mViewHolder.mTextViewItenam=(TextView)convertView.findViewById(R.id.row_order_iteamname);
            mViewHolder.mTextViewPrice=(TextView)convertView.findViewById(R.id.row_order_price);
            mViewHolder.mTextViewTotal=(TextView)convertView.findViewById(R.id.row_order_txt_total);
            mViewHolder.mTextViewQty=(TextView)convertView.findViewById(R.id.row_order_edt_qty);
            mViewHolder.mTextViewItenam.setText(RestaurantApplication.mParserOrder.get(position).getIteamname());
            mViewHolder.mTextViewPrice.setText(RestaurantApplication.mParserOrder.get(position).getIteamprice());
            mViewHolder.mTextViewQty.setId(position);

            mViewHolder.mTextViewQty.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mDialog.setContentView(R.layout.dialog_qty);
                    final EditText  mEditTextQty=(EditText)mDialog.findViewById(R.id.dialog_edt);
                    final TextView  mTextViewOk=(TextView)mDialog.findViewById(R.id.dialog_txt_ok);
                    mTextViewOk.setTag(position);
                    TextView mTextViewCancel=(TextView)mDialog.findViewById(R.id.dialog_txt_cancle);
                    mTextViewOk.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            int pos=Integer.parseInt(mTextViewOk.getTag().toString());
                            System.out.println("Position  "+pos);

                            mOrder.setIt_id(RestaurantApplication.mParserOrder.get(position).getIt_id());
                            mOrder.setCid(RestaurantApplication.mParserOrder.get(position).getCid());
                            mOrder.setIteamname(RestaurantApplication.mParserOrder.get(position).getIteamname());
                            mOrder.setIteamprice(RestaurantApplication.mParserOrder.get(position).getIteamprice());
                            mOrder.setSid(RestaurantApplication.mParserOrder.get(position).getSid());
                            mOrder.setQty(mEditTextQty.getText().toString().trim());
                            RestaurantApplication.mParserOrder.remove(position);
                            RestaurantApplication.mParserOrder.add(position,mOrder);
                            mViewHolder.mTextViewQty.setText(RestaurantApplication.mParserOrder.get(position).getQty());
                            mDialog.dismiss();
                            mOrderAdpter.notifyDataSetChanged();
                        }
                    });
                    mTextViewCancel.setOnClickListener(new View.OnClickListener() {

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

            mViewHolder.mTextViewQty.setTag(position);
            if (convertView==null) {
                mViewHolder.mTextViewQty.getTag();
            }
            return convertView;
        }

    }
    public class ViewHolder
    {
        TextView mTextViewItenam;
        TextView mTextViewPrice;
        TextView mTextViewQty;
        TextView mTextViewTotal;
    }
}

在编辑的代码中,当我第一次更改数量时,我会正常工作,但是当我更改任何其他项目数量时,它会更改所有其他值,它知道它是如何发生的吗?以及如何计算每一行的总值以及如何获得所有项目的总值?

4

1 回答 1

0

我建议您将 datasetChanged 用于列表视图适配器。

  • 首先将该文本视图的位置标记为 mTextViewOk.setTag(position);
  • 获取onClick中的位置 int pos=Integer.parseInt(mTextViewOk.getTag().toString());
  • 从数组列表中获取值,即对象值。
  • 更改您需要更改的值。
  • 最后使用 adapter.notifyDataSetChanged();

我正在更新我的ans

“现在的问题是,当我运行此代码并单击任何数量的 textview 时,它只会在 textview 的最后一次设置值,当我再次单击任何 textview 并尝试在文本中设置值时,它不允许在任何文本框中设置该值知道如何解决这个问题吗?”

  • 这背后的原因是列表视图行为,它将保留单个页面的元素,即:) 如果您有 50 个项目并且由于列表视图项目的大小而只能在单个页面中显示 5 个项目。适配器将保留最后一个元素数据。如果你更改为内部 getView 意味着它将更新最后一个项目。所以
  • 如上所述,您必须将 Tag () 的位置设置为该文本视图。(前两点)
于 2013-10-25T05:17:35.717 回答