滚动时Listview项目重复/错误的位置

时间:2017-04-21 00:02:26

标签: java android listview

这个问题被多次询问,但每个人都有他对listview适配器(ArrayAdapter或BaseAdapter)的具体用法。 在问这个问题之前,我经常搜索了很多,之前发生过同样的问题,我使用Holder类解决了这个问题,清理代码使其尽可能少。 但是现在,我正在创建另一个包含listview的接口,需要一个BaseAdapter来使用List of Object(List)填充它。

在我的情况下,奇怪的是,如果我的列表视图只包含4个项目(4行),那么一切正常,但如果它包含4个以上,则第一个项目将被复制,如果我快速向下/向上滚动,物品出错了。

这是我的代码:

public class OrdersAdapter extends BaseAdapter {

    List<Order> ordersList;
    FragmentActivity activity;
    private int height;

    public OrdersAdapter(FragmentActivity activity, List<Order> ordersList){
        this.activity   = activity;
        this.ordersList = ordersList;
        height = Constants.getOneThirdScreenHight(activity);
    }

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

    @Override
    public Order getItem(int position) {
        return ordersList.get(position);
    }

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

    private class MyHolder{
        TextView orderNumber, orderDate, menuCount, orderPrice, orderStatus, orderTelephone, orderStoreName;
        ImageView orderDelete, menuPicture;
        Order order, mOrder;
        String numberText = activity.getResources().getString(R.string.order_number);
        HashMap<String, String> map;
        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
    }

    @Override
    public View getView (final int position, View convertView, ViewGroup parent) {
        View vi;
        final MyHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater)activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            holder = new MyHolder();
            vi = layoutInflater.inflate(R.layout.sent_order_item, null);
            holder.order = getItem(position);
            holder.mOrder = new Order();
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.height = height;
            vi.findViewById(R.id.order_history_layout).setLayoutParams(layoutParams);
            holder.orderNumber = (TextView) vi.findViewById(R.id.order_number);
            holder.orderDate = (TextView) vi.findViewById(R.id.order_date);
            holder.orderDelete = (ImageView) vi.findViewById(R.id.order_delete);
            holder.orderPrice = (TextView) vi.findViewById(R.id.order_history_price);
            holder.orderStatus = (TextView) vi.findViewById(R.id.order_history_status);
            holder.orderStoreName = (TextView) vi.findViewById(R.id.order_history_store);
            holder.orderTelephone = (TextView) vi.findViewById(R.id.order_history_phone);
            holder.menuCount = (TextView) vi.findViewById(R.id.order_menu_count1) ;
            holder.menuPicture = (ImageView) vi.findViewById(R.id.order_picture);
            vi.setTag(holder);
        }else{
            vi = convertView;
            holder = (MyHolder) vi.getTag();
        }

        //Getting Restaurants infos ( name and telephone ) from firebase
        holder.mDatabase.child(Constants.STORES_PROFILES + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id())
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(holder.map==null){
                            holder.map = (HashMap<String, String>)dataSnapshot.getValue();
                        }
                        if(dataSnapshot.getValue()!=null) {
                        holder.orderStoreName.setText(holder.map.get(Constants.NAME));
                        holder.orderTelephone.setText(holder.map.get(Constants.TELEPHONE));
                        }
                        //notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

        //checking Order's status (received, validated, in progress, delivered)
        holder.mDatabase.child(Constants.ORDERS + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id() + "/" + holder.order.getOrderId())
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
                        try {
                            holder.mOrder = dataSnapshot.getValue(Order.class);
                            if (!holder.order.getStatus().equals(holder.mOrder.getStatus())) {
                            holder.order.setStatus(holder.mOrder.getStatus());
                                //update status value for the user
                                holder.mDatabase.child(Constants.USERS+"/"+ Authentication.getCurrentUser().getUid()+"/"+Constants.SUBMITTED_ORDERS_KEY
                            +"/"+holder.order.getOrderId()+"/"+Constants.STATUS).setValue(holder.mOrder.getStatus());
                                //notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        displayOrder(holder);
        return vi;
    }

    public void displayOrder(MyHolder holder){
        holder.orderStatus.setText(holder.order.getStatus());
        holder.orderNumber.setText(holder.numberText + holder.order.getOrderNumber());
        holder.orderDate.setText(Utils.parseDateToDisplay((Long)holder.order.getCreated_at()));
        holder.menuCount.setText(holder.order.getMyMealsList().get(0).getQuantity()+"");
        holder.orderPrice.setText(holder.order.getTotal_price_formatted());
        Glide.with(activity)
            .load(holder.order.getMyMealsList().get(0).getMain_image())
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(holder.menuPicture);

        if(OrdersContainerFragment.pDialog.isShowing()){
            OrdersContainerFragment.pDialog.dismiss();
        }
    }
}

这是我的&#34; xml&#34;档案:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/order_history_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/save_order_linear"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="25"
        android:background="#fcf5ef"
        android:orientation="horizontal"
        android:weightSum="100">

        <TextView
            android:id="@+id/order_number"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="@string/order_number"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold"
            android:typeface="serif"/>

        <TextView
            android:id="@+id/order_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="00/00/0000"
            android:textColor="@color/colorPrimary"
            android:typeface="serif"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/save_order_pics"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/save_order_linear"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_weight="74"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:weightSum="100">


        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="33">

            <ImageView
                android:id="@+id/order_picture"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

            <ProgressBar
                android:id="@+id/order_progressBar1"
                style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:indeterminate="true"
                android:indeterminateDrawable="@drawable/my_progressbar"
                android:padding="22dp"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/order_menu_count1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="false"
                android:layout_alignParentEnd="false"
                android:layout_alignParentRight="false"
                android:gravity="bottom|right"
                android:paddingBottom="15dp"
                android:paddingRight="15dp"
                android:text="ABCD"
                android:textColor="@color/gray"
                android:textSize="22dp"
                android:typeface="serif"/>
        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="67"
            android:orientation="vertical"
            android:paddingLeft="15dp"
            android:weightSum="100">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="false"
                    android:layout_centerHorizontal="false"
                    android:layout_weight="1"
                    android:text="@string/order_price"
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>

                <TextView
                    android:id="@+id/order_history_price"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="false"
                    android:layout_centerHorizontal="false"
                    android:layout_weight="1"
                    android:text="..."
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="@string/order_status"
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>

                <TextView
                    android:id="@+id/order_history_status"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="..."
                    android:textColor="@color/colorAccent"
                    android:typeface="monospace"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="@string/Phone"
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>

                <TextView
                    android:id="@+id/order_history_phone"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="..."
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="@string/order_store"
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>

                <TextView
                    android:id="@+id/order_history_store"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="..."
                    android:textColor="@color/colorPrimary"
                    android:typeface="monospace"/>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/save_order_pics"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="#fcf5ef"
        android:orientation="horizontal">
    </LinearLayout>
</LinearLayout>

0 个答案:

没有答案