本文介绍了单击android中的适配器类中存在的按钮时,如何更新片段内textview的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮:+和-.我希望当我单击按钮+时,片段类(位于列表视图之外)中存在的textview的值被更改.我该怎么办?

I have two buttons: + and -. I want that when I click on the button +, the value of the textview present in the fragment class (outside the listview) is changed. How can I do this ?

这是我的适配器类:

public class CartBaseAdapter extends BaseAdapter  {

    private Context mContext;
    private ArrayList<PojoCart> mList;
    private ViewHolder viewHolder;
    private HashMap<String, Integer> mHashMap = new HashMap<String, Integer>();
    private Integer total;
    private DataBaseHandler dbh;
    private int Id = 1;
    private String value1, value2;
    private int z;
    private FragmentTransactionListener fragmentTransactionListener = (FragmentTransactionListener) new Cart();


    public CartBaseAdapter(Context mContext, ArrayList<PojoCart> mList) {
        this.mContext = mContext;
        this.mList = mList;
        dbh = new DataBaseHandler(mContext);


    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


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


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.cart_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.mImgItem = (ImageView) convertView.findViewById(R.id.cart_image);
            viewHolder.mTvItemName = (TextView) convertView.findViewById(R.id.tv_item_name);
            viewHolder.mTvItemPrice = (TextView) convertView.findViewById(R.id.tv_item_price);
            viewHolder.mTvNumber = (TextView) convertView.findViewById(R.id.tv_number);
            viewHolder.mBtnAdd = (Button) convertView.findViewById(R.id.btn_add);
            viewHolder.mBtnMinus = (Button) convertView.findViewById(R.id.btn_sub);
            viewHolder.mImgDelete = (ImageView) convertView.findViewById(R.id.img_del);


        } else {
            viewHolder = (ViewHolder) convertView.getTag();


        }


        convertView.setTag(viewHolder);


        final PojoCart pojoCart = (PojoCart) getItem(position);
        viewHolder.mTvItemName.setText(pojoCart.getmItemName());
        viewHolder.mTvItemPrice.setText(pojoCart.getmItemPrice());
        //  viewHolder.mImgDelete.setTag(pojoCart.getmCategoryId());

      /*  try {
            URL url = new URL(pojoCart.getmItemImage());
            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            viewHolder.mImgItem.setImageBitmap(bmp);
        } catch (Exception e) {
            e.printStackTrace();

            //   Log.e("exception", "" + e.getMessage());
        }*/


        viewHolder.mImgItem.setImageBitmap(Utility.StringToBitMap(pojoCart.getmItemImage()));
        viewHolder.mBtnAdd.setTag(pojoCart);
        viewHolder.mBtnMinus.setTag(pojoCart);
        viewHolder.mTvItemPrice.setTag(pojoCart);
        viewHolder.mTvNumber.setTag(pojoCart);
        viewHolder.mImgDelete.setTag(position);

        if (pojoCart.getmQuantity() > 0) {

            viewHolder.mTvNumber.setText("" + pojoCart.getmQuantity());

        } else {

            viewHolder.mTvNumber.setText("" + 0);
        }


        viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PojoCart pojoCart = (PojoCart) v.getTag();
                int mValue = pojoCart.getmQuantity();
                mValue++;
                viewHolder.mTvNumber.setText("" + mValue);
                pojoCart.setmQuantity(mValue);
                notifyDataSetChanged();

                value1 = viewHolder.mTvNumber.getText().toString();
                value2 = pojoCart.getmItemPrice();
                int x = Integer.parseInt(value1);
                int y = Integer.parseInt(value2);


                //  viewHolder.Dish_rate.setVisibility(View.GONE);


                Log.e("value1", value1);
                Log.e("value2", value2);
                z = x * y;

                pojoCart.setmItemPrice(String.valueOf(z));


                Log.e("z", "" + z);

                if (x > 2) {
                    int n = x - 1;
                    int k = z / n;
                    Log.e("k", "" + k);

                    pojoCart.setmItemPrice(String.valueOf(k));

                } else {

                    pojoCart.setmItemPrice(String.valueOf(z));
                }


                dbh.updateSingleRow(pojoCart.getmCategoryId(), pojoCart.getmItemPrice(), pojoCart.getmQuantity());
                int total = dbh.getTotalOfAmount();
                pojoCart.setmTotalPrice(total);




            }


        });


        viewHolder.mBtnMinus.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        PojoCart pojoCart = (PojoCart) v.getTag();
                                                        int mValue = pojoCart.getmQuantity();


                                                        if (mValue > 0) {
                                                            mValue--;
                                                            viewHolder.mTvNumber.setText("" + mValue);

                                                            value1 = viewHolder.mTvNumber.getText().toString();
                                                            value2 = pojoCart.getmItemPrice();
                                                            int x = Integer.parseInt(value1);
                                                            int y = Integer.parseInt(value2);

                                                            if (x >= 1) {

                                                                Log.e("value11", value1);
                                                                Log.e("value22", value2);

                                                                int n = x + 1;

                                                                Log.e("n", "" + n);
                                                                int k = y / n;
                                                                Log.e("k", "" + k);

                                                                z = k * x;

                                                                Log.e("z", "" + z);


                                                                pojoCart.setmItemPrice(String.valueOf(z));

                                                            } else {

                                                                pojoCart.setmItemPrice(pojoCart.getmItemPrice());

                                                            }


                                                        }

                                                        pojoCart.setmQuantity(mValue);


                                                        notifyDataSetChanged();

                                                        dbh.updateSingleRow(pojoCart.getmCategoryId(), pojoCart.getmItemPrice(), pojoCart.getmQuantity());


                                                        pojoCart.setmTotalPrice(dbh.getTotalOfAmount());

                                                    }
                                                }

        );

        viewHolder.mImgDelete.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        int categoryId = pojoCart.getmCategoryId();

                        //  int id = (Integer) view.getTag();
                        //   id++;

                        Log.e("removeIdFromTheTable", "" + categoryId);


                        dbh.delete_byID(categoryId);
                        mList.remove(position);
                        notifyDataSetChanged();


                        pojoCart.setmTotalPrice(dbh.getTotalOfAmount());


                    }
                }

        );


        return convertView;
    }




    private class ViewHolder {

        TextView mTvItemName, mTvItemPrice, mTvNumber;
        ImageView mImgItem, mImgDelete;
        Button mBtnAdd, mBtnMinus;


    }


}

这是我的片段类:

public class Cart extends Fragment implements View.OnClickListener {

    private ArrayList<PojoCart> mCartList;
    private ListView mListView;
    private CartBaseAdapter mCartBaseAdapter;
    private DataBaseHandler dbh;
    private List<PojoCartDataBase> pojoCartDataBase;
    private TextView mTvProcesscheck, mTvTotalPrice;
    private String ItemName, ItemPrice;
    private String ItemImage;
    private ArrayList<String> mTotalPrice;
    private Toolbar toolbar;
    private int ItemQuantity;
    int id = 1;
    private String categoryId;
    private int sumOfPrice;
    private PojoCart pojoCart;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_cart, container, false);


    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initialize();
        //  addData();
        displayTotalAmount();


        try {
            getDataFromDatabase();
        } catch (IOException e) {
            e.printStackTrace();

        }


    }

    private void initialize() {
        mTotalPrice = new ArrayList<String>();
        mCartList = new ArrayList<PojoCart>();
        mListView = (ListView) getActivity().findViewById(R.id.listview_cart);
        mCartBaseAdapter = new CartBaseAdapter(getContext(), mCartList);
        Parcelable state = mListView.onSaveInstanceState();
        mListView.setAdapter(mCartBaseAdapter);
        mListView.onRestoreInstanceState(state);

        mTvProcesscheck = (TextView) getActivity().findViewById(R.id.tv_checkout);
        mTvTotalPrice = (TextView) getActivity().findViewById(R.id.tv_total_price);
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        dbh = new DataBaseHandler(getContext());
        mTvProcesscheck.setOnClickListener(this);
        toolbar.setTitle("Cart");


        mCartBaseAdapter.notifyDataSetChanged();


        final RippleView rippleView = (RippleView) getActivity().findViewById(R.id.ripple_view_cart);


        rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {

            @Override
            public void onComplete(RippleView rippleView) {
                Log.d("Sample", "Ripple completed");

                Fragment fragment = new LogIn();
                getFragmentManager().beginTransaction().replace(R.id.frame, fragment).addToBackStack(null).commit();
                toolbar.setTitle("Restaurant List");
            }

        });


    }


   /* private void addData() {


        for (int i = 0; i < mItemName.length; i++) {

            PojoCart pojoCart = new PojoCart();
            pojoCart.setmItemName(mItemName[i]);
            pojoCart.setmItemPrice(mItemPrice[i]);
            pojoCart.setmItemImage(mItemImage[i]);


            mCartList.add(pojoCart);

        }
        //   mCartList.add(pojoCartDataBase);

    }
*/


    private void getDataFromDatabase() throws IOException {

        Cursor c = dbh.getAllRows();


        if (c.moveToFirst()) {
            while (c.isAfterLast() == false) {

                //  int id = c.getInt(0);
                int id = c.getInt(1);
                Log.e("id.....", "" + id);
                ItemName = c.getString(2);
                ItemPrice = c.getString(3);
                Log.e("itemname", ItemName);

                Log.e("itemprice", ItemPrice);

                ItemQuantity = c.getInt(4);

                Log.e("itemquantity", "" + ItemQuantity);
                ItemImage = c.getString(5);


                Log.e("itemimage.........", ItemImage);

                pojoCart = new PojoCart();
                pojoCart.setmItemName(ItemName);
                pojoCart.setmItemPrice(ItemPrice);
                pojoCart.setmItemImage(ItemImage);
                pojoCart.setmQuantity(ItemQuantity);

                pojoCart.setmCategoryId(id);


                mCartList.add(pojoCart);
                mCartBaseAdapter.notifyDataSetChanged();
                c.moveToNext();


            }


        }


    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.tv_checkout:
              /*  Fragment fragment = new LogIn();
                getFragmentManager().beginTransaction().replace(R.id.frame, fragment).addToBackStack(null).commit();*/

                // toolbar.setTitle("Checkout");
        }


    }

    public void displayTotalAmount() {

        int total = dbh.getTotalOfAmount();
        mTvTotalPrice.setText(String.valueOf(total));

    }


}

我想通过单击按钮+和-来更改mTvTotalPric(Textview)的值,该按钮位于列表视图中.我要更改值的textview在listview之外.

I want to change the value of the mTvTotalPric (Textview) on click of the button + and -, which is present at the listview. And the textview which the value I want to change is outside the listview.

推荐答案

在您的Adapter class中创建一个interface

Adapter.class

Adapter.class

public class CartBaseAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<PojoCart> mList;
    private ViewHolder viewHolder;
    private HashMap<String, Integer> mHashMap = new HashMap<String, Integer>();
    private Integer total;
    private DataBaseHandler dbh;
    private int Id = 1;
    private String value1, value2;
    private int z;
    private FragmentTransactionListener fragmentTransactionListener = (FragmentTransactionListener) new Cart();
    private SendDataToFragment sendDataToFragment;

    public CartBaseAdapter(FragmentCart fragmentCart, Context mContext, ArrayList<PojoCart> mList) {
        this.mContext = mContext;
        this.mList = mList;
        dbh = new DataBaseHandler(mContext);
        sendDataToFragment = (SendDataToFragment) fragmentCart;

    }

    //Interface to send data from adapter to fragment
    public interface SendDataToFragment {
        void sendData(String Data);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


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

            viewHolder = new ViewHolder();
            viewHolder.mImgItem = (ImageView) convertView.findViewById(R.id.cart_image);
            viewHolder.mTvItemName = (TextView) convertView.findViewById(R.id.tv_item_name);
            viewHolder.mTvItemPrice = (TextView) convertView.findViewById(R.id.tv_item_price);
            viewHolder.mTvNumber = (TextView) convertView.findViewById(R.id.tv_number);
            viewHolder.mBtnAdd = (Button) convertView.findViewById(R.id.btn_add);
            viewHolder.mBtnMinus = (Button) convertView.findViewById(R.id.btn_sub);
            viewHolder.mImgDelete = (ImageView) convertView.findViewById(R.id.img_del);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        convertView.setTag(viewHolder);


        final PojoCart pojoCart = (PojoCart) getItem(position);
        viewHolder.mTvItemName.setText(pojoCart.getmItemName());
        viewHolder.mTvItemPrice.setText(pojoCart.getmItemPrice());


        viewHolder.mImgItem.setImageBitmap(Utility.StringToBitMap(pojoCart.getmItemImage()));
        viewHolder.mBtnAdd.setTag(pojoCart);
        viewHolder.mBtnMinus.setTag(pojoCart);
        viewHolder.mTvItemPrice.setTag(pojoCart);
        viewHolder.mTvNumber.setTag(pojoCart);
        viewHolder.mImgDelete.setTag(position);

        if (pojoCart.getmQuantity() > 0) {

            viewHolder.mTvNumber.setText("" + pojoCart.getmQuantity());

        } else {

            viewHolder.mTvNumber.setText("" + 0);
        }


        viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Send data via interface to your fragment
                sendDataToFragment.sendData("Your Data");

                //Your existing code
            }
        });

        viewHolder.mBtnMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Send data via interface to your fragment
                sendDataToFragment.sendData("Your Data");

                //Your existing code
            }
        });

        return convertView;
    }


    private class ViewHolder {

        TextView mTvItemName, mTvItemPrice, mTvNumber;
        ImageView mImgItem, mImgDelete;
        Button mBtnAdd, mBtnMinus;


    }


}

fragment内部实现该interface,因此,一旦在adapter中单击了button,就会在片段中获取数据.

Inside your fragment implement that interface so as soon as your button is clicked in your adapter you will get the data inside your fragment.

Fragment.class

Fragment.class

public class FragmentCart extends Fragment implements
        View.OnClickListener, CartBaseAdapter.SendDataToFragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.your_layout, null);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CartBaseAdapter adapter = new CartBaseAdapter(FragmentCart.this, getActivity(), yourList);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void sendData(String Data) {
        //set this data to your textView
    }
}

这篇关于单击android中的适配器类中存在的按钮时,如何更新片段内textview的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:19