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

时间:2016-09-20 08:23:22

标签: android onclick textview android-pageradapter

嗨我的寻呼机适配器有问题,根据位置点击按钮时我无法更新textview。代码在下面

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

<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的第三页,所有页面随机出现此问题。任何帮助都非常感谢。

这是我完整的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;
    }

2 个答案:

答案 0 :(得分:1)

产品

中添加一个参数
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();

相应地更改您的班级名称。

答案 1 :(得分:0)

首先,您需要使用

为充气视图设置标记
itemView.setTag(position);

现在点击按钮事件,您需要使用findViewWithTag

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

现在,您可以通过

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

现在更新你的用户界面。另外不要忘记在那之后刷新适配器