Textview 未根据 pageradapter 内的位置进行更新

Textview is not updating based on position inside pageradapter

您好,我的寻呼机适配器有问题,根据下面的 position.the 代码,单击按钮时我无法更新文本视图

这是我的 pageradapter 单一视图中的两个按钮 class。

<LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:id="@+id/plusminuslayout"
                            android:orientation="horizontal">

                            <Button
                                android:id="@+id/decrease"
                                android:layout_width="0dp"
                                android:layout_weight="0.3"
                                android:background="#e2364e"
                                android:layout_gravity="center"
                                android:layout_height="25dp"
                                android:textColor="#ffffff"
                                android:text="-" />

                            <TextView
                                android:id="@+id/integer_number"
                                android:layout_width="0dp"
                                android:layout_weight="0.4"
                                android:gravity="center"
                                android:layout_gravity="center"
                                android:layout_height="wrap_content"
                                android:text="1"
                                android:textStyle="bold"
                                android:textSize="20sp" />

                            <Button
                                android:id="@+id/increase"
                                android:layout_width="0dp"
                                android:layout_weight="0.3"
                                android:background="#e2364e"
                                android:layout_gravity="center"
                                android:layout_height="25dp"
                                android:textColor="#ffffff"
                                android:text="+" />

                            <TextView
                                android:layout_width="0dp"
                                android:layout_weight="0.2"
                                android:gravity="center"
                                android:layout_gravity="center"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:textSize="20sp" />
                        </LinearLayout>

这是 Instatiateitem() 方法中更新 textview 值的代码

holder.increase = (Button) itemView.findViewById(R.id.increase);
            holder.decrese = (Button) itemView.findViewById(R.id.decrease);
            holder.integerval = (TextView) itemView.findViewById(R.id.integer_number);


            holder.increase.setTag(position);
            holder.decrese.setTag(position);
            holder.integerval.setTag(position);
            holder.decrese.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity = quantity -1;
                    updateView(holder,v,position,quantity);

                }
            });

            holder.increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity = quantity + 1;
                    updateView(holder,v,position,quantity);

                }
            });

这是我的 updateview() 方法

private void updateView( ViewHolder holder,View v, int position,int quantity) {

        holder.integerval.setText("" + quantity);
    }

Textview 正在 pageradapter 的第三页更新,随机出现这个问题是为了所有 pages.Any 帮助真的很感激。

这是我完整的 Insatiateitem() 方法

@SuppressLint("InflateParams")
    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        try {

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.detailspage_new2, container, false);
        } catch (InflateException e) {
            e.printStackTrace();

        }
        try {
            session = SessionManager.getInstance(context);


            holder = new ViewHolder();
            holder.txt_offer = (TextView) itemView
                    .findViewById(R.id.Offer_edittext);
            holder.txt_shop = (TextView) itemView
                    .findViewById(R.id.Shop_edittext);
            holder.Name = (TextView) itemView.findViewById(R.id.name);

            holder.plusminuslayout = (LinearLayout) itemView.findViewById(R.id.plusminuslayout);
            holder.increase = (Button) itemView.findViewById(R.id.increase);
            holder.decrese = (Button) itemView.findViewById(R.id.decrease);
            holder.integerval = (TextView) itemView.findViewById(R.id.integer_number);


            holder.increase.setTag(position);
            holder.decrese.setTag(position);
            holder.integerval.setTag(position);
            holder.decrese.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity = quantity -1;
                    updateView(holder,v,position,quantity);

                }
            });

            holder.increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity = quantity + 1;
                    updateView(holder,v,position,quantity);

                }
            });




            holder.available = (TextView) itemView.findViewById(R.id.available);
            holder.delivery = (TextView) itemView.findViewById(R.id.delivery);
            holder.Address = (TextView) itemView.findViewById(R.id.Address);
            holder.rat_user = (RatingBar) itemView
                    .findViewById(R.id.user_ratingbar);
            holder.txt_posteddate = (TextView) itemView
                    .findViewById(R.id.offerPosted_edittext);
            holder.offerdescription = (TextView) itemView.findViewById(R.id.offerdescription);
            holder.tvphone = (TextView) itemView
                    .findViewById(R.id.tvphone);
            holder.myFab = (FloatingActionButton) itemView.findViewById(R.id.fab);
            holder.myReserveList = (FloatingActionButton) itemView.findViewById(R.id.myReserveList);

            holder.mapsample = (ImageView) itemView.findViewById(R.id.mapsample);
            holder.callimage = (TextView) itemView.findViewById(R.id.callimage);
            holder.VName = (LinearLayout) itemView.findViewById(R.id.VName);
            holder.viewA = (LinearLayout) itemView.findViewById(R.id.viewA);
            holder.listDetails = (ImageView) itemView.findViewById(R.id.listDetails);
            holder.backDetails = (ImageView) itemView.findViewById(R.id.backDetails);
            holder.viewPager = (ImageView) itemView
                    .findViewById(R.id.view_pager);
            holder.textViewNew = (TextView) itemView
                    .findViewById(R.id.textViewNew);

            holder.actualprice = (TextView) itemView
                    .findViewById(R.id.actualprice);
            holder.offerprice = (TextView) itemView
                    .findViewById(R.id.offerprice);
            holder.fromarea = (TextView) itemView
                    .findViewById(R.id.fromarea);
            holder.toarea = (TextView) itemView
                    .findViewById(R.id.toarea);
            holder.price = (TextView) itemView
                    .findViewById(R.id.price);

            holder.type = (TextView) itemView
                    .findViewById(R.id.type);
            holder.propertytype = (TextView) itemView
                    .findViewById(R.id.PropertyType);
            holder.value = (TextView) itemView
                    .findViewById(R.id.value);


            holder.pricelayout = (LinearLayout) itemView.findViewById(R.id.pricelayout);
            holder.fromtolayout = (LinearLayout) itemView.findViewById(R.id.fromtolayout);
            holder.travelpricelayout = (LinearLayout) itemView.findViewById(R.id.travelpricelayout);
            holder.valuelayout = (LinearLayout) itemView.findViewById(R.id.Valuelayout);
            holder.typelayout = (LinearLayout) itemView.findViewById(R.id.typeslayout);


            holder.timer = (TextView) itemView.findViewById(R.id.Offer_timer);
            FloatingActionButton myFabLocal = null;

            FloatingActionButton myReserveLocal;
            TextView tv = null;
            TextView timerTextView = null;
            ImageView viewPagerImage = null;
            try {
                myFabLocal = holder.myFab;
                myReserveLocal = holder.myReserveList;
                tv = holder.timer;
                timerTextView = holder.textViewNew;
                viewPagerImage = holder.viewPager;
            } catch (Exception e) {
                e.printStackTrace();
            }

            final LinearLayout viewA = holder.viewA;
            myFabLocal.setBackgroundTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.details_paly_green)));

            product = products.get(position);
            //blobkedmilliseconds = 300000;
            if (product.getUserRegisterId().equals(session.getUserRegisterID())) {
                holder.myReserveList.setVisibility(View.VISIBLE);
            } else {
                holder.myReserveList.setVisibility(View.GONE);
            }

            final ArrayList<String> ImageArrayList = new ArrayList<>();

            if (product.getUserRegisterId().equals(session.getUserRegisterID())) {
                holder.myFab.setVisibility(View.GONE);
                //milliseconds = product.getOfferDifferenceTime() + blobkedmilliseconds;
                milliseconds = product.getOfferDifferenceTime();
            } else {
                holder.myFab.setVisibility(View.VISIBLE);
                milliseconds = product.getOfferDifferenceTime();
            }
            milliseconds = product.getOfferDifferenceTime();

            to_latitude = product.getLat();
            to_longitude = product.getLng();
            holder.tvphone.setText(product.getMobileNumber());
            holder.txt_offer.setText(product.getDeal());
            holder.Address.setText(product.getAddress());


            String avil = product.getAvailable();

            String[] parts=avil.split(":");

            String available = parts[1];
            holder.available.setText("Qty: "+ available);

            String delvry = product.getDelivery();


            if(delvry.equals("Home Delivery not Available")) {

                holder.delivery.setText("NA");
            }
            else
            {
                holder.delivery.setText("Free");
            }


            holder.offerdescription.setText(product.getDescription());
            if (product.getOfferDifferenceTime() > 0) {
                myFabLocal.setImageDrawable(context.getResources().getDrawable(R.drawable.play));
            } else {
                myFabLocal.setImageDrawable(context.getResources().getDrawable(R.drawable.list_notify));
            }

            String ShopName = product.getShopName(); //+ " , " + product.getAreaName();
            holder.txt_shop.setText(product.getCategoryName());

            holder.Name.setText(ShopName);
            holder.txt_posteddate.setText(product.getPostedDate());




            final TextView finalTv1 = tv;
            final FloatingActionButton finalMyFabLocal = myFabLocal;
            final TextView finalTimerTextView1 = timerTextView;
            final ImageView finalViewPagerImage = viewPagerImage;
            holder.myFab.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    UserListClickEvent(position);
                }

                private void UserListClickEvent(int position) {
                    if (session.getRegUserName().equals("User Name")) {
                        Intent intent = new Intent(context, QuizFragment.class);
                        context.startActivity(intent);
                    } else {
                        //milliseconds = product.getOfferDifferenceTime()+blobkedmilliseconds;
                        milliseconds = product.getOfferDifferenceTime();
                        final ListDetails pro = products.get(position);
                        BlockButtonClick(finalTv1, finalMyFabLocal, finalTimerTextView1, finalViewPagerImage, viewA , pro, "");
                        products.set(position , pro);

                        /*if (product.getOfferDifferenceTime() > 0) {
                            BlockOrNotifyOpportunitiesWS(v, holder, position);
                        } else {
                            final ListDetails pro = products.get(position);
                            DetailsViewpagerIntent(position, pro);
                        }*/
                    }
                }
            });

            holder.myReserveList.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent i = new Intent(context, ReserveUserListTabsActivity.class);
                    final ListDetails pro = products.get(position);
                    i.putExtra("OppId", Integer.parseInt(pro.getOfferID()));
                    context.startActivity(i);
                }
            });


            holder.mapsample.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                 try {
                    Intent i = new Intent(context,
                            MapFragmentActivity.class);
                    MapFragmentActivity.setLat(products.get(position)
                            .getLat());
                    MapFragmentActivity.setLng(products.get(position)
                            .getLng());
                    MapFragmentActivity.setArea(products.get(position)
                            .getAreaName());
                    MapFragmentActivity.setcategory(products.get(position)
                            .getCategoryName());
                    PlacesMapActivity
                            .getNearPlaces(GetAllGooglePlaces.ListItemsData);
                    context.startActivity(i);
                 } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            });

            try {
                ((Activity) itemView.getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        String ImageNameStr = product.getImageName1();
                        final String[] ImageNameArr = ImageNameStr.split(",");
                        Collections.addAll(ImageArrayList, ImageNameArr);
                        //Picasso.with(context).load("http://rtrpservice.azurewebsites.net/Images/"+ImageArrayList.get(0)).into(holder.viewPager);

                        if(!isAirplaneModeOn(context.getApplicationContext())){
                            //Picasso Code
                            Picasso.with(context.getApplicationContext()).load("http://rtrpservice.azurewebsites.net/Images/"+ImageArrayList.get(0)).resize(250, 250)
                                    .into(holder.viewPager/*, new Snackbar.Callback() {
                                        @Override
                                        public void onSuccess() {
                                            Log.e("profilepicsucess", "");
                                        }

                                        @Override
                                        public void onError() {
                                            Log.e("profilepicfalse :3", "");
                                        }
                                    }*/);


                        }



                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

            (container).addView(itemView, 0);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return itemView;
    }

在您的产品中再添加一个参数class

public int quantity;

并在 Instatiateitem() 内添加

holder.integerval.setText("" + product.quantity);

在点击侦听器中这样做

int pos = Integer.parseInt(v.getTag().toString());
Product obj = products.get(pos);
obj.quantity = obj.quantity + 1;  //do -1 in second listener
notifyDataSetChage();

相应地更改您的 class 姓名。

首先您需要使用

为您的膨胀视图设置标签
itemView.setTag(position);

现在在按钮的点击事件上你需要使用findViewWithTag

获取标签
View view = pager.findViewWithTag(position);
if (view == null) {
    return;
}

现在您可以通过

获取您的UI ID
TextView price = (TextView) view.findViewById(R.id.price);

现在更新您的 UI 之后。之后也不要忘记刷新适配器