简体   繁体   English

在android中滚动listview时,listview中的textview值被删除了吗?

[英]Textview values is deleted in listview while scrolling the listview in android?

i have a listview with a row that row contains one imageview and 3 layouts and each layout contains again 3 textviews 我有一个带有行的listview,该行包含一个imageview和3个布局,每个布局又包含3个textviews

the xml is like this XML是这样的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/llRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="3dp"
            android:layout_weight="0.2"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal"
                android:layout_width="20dip"
                android:layout_height="20dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="5dip"
                android:background="@drawable/circel"
                android:gravity="center"
                android:text="0"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Laundry"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal1"
                android:layout_width="20dip"
                android:layout_height="20dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="5dip"
                android:background="@drawable/circel"
                android:gravity="center"
                android:text="0"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Dry Clean"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal2"
                android:layout_width="20dip"
                android:layout_height="20dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="5dip"
                android:background="@drawable/circel"
                android:gravity="center"
                android:text="0"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Stream Iron"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

and i take on Custom Adapter like this i get the some string values from server and that values is setted in listview 我像这样使用Custom Adapter,我从服务器获取了一些字符串值,并且这些值在listview中设置

here the code is 这里的代码是

public class CustomAdapter extends BaseAdapter {

    ArrayList<String> p_id;
    ArrayList<String> p_name;
    Context context;
    ArrayList<String> imageId;
    ArrayList<String> wash_v;
    ArrayList<String> dry_v;
    ArrayList<String> iron_v;
    Typeface tf;
    ProgressDialog dialog;
    Intent i;
    float washItemCount = 0.0f;
    String iconName;
    Handler handler;
    Runnable myRunnable;
    Vector<BasketDO> vecBasket = new Vector<BasketDO>();
    int countVal1, countVal2, countVal3;
    public ImageLoader imageLoader;
    ArrayList<String> laundry = null;
    ArrayList<String> streamIron = null;
    ArrayList<String> dryClean = null;
    int KEY;

    static int value1 = 0;
    static int value2 = 0;
    static int value3 = 0;

    DbaseHandler dbaseHandler;
    HashMap<Integer, Integer> laundryHashmap;
    HashMap<Integer, Integer> ironHashmap ;
    HashMap<Integer, Integer> dryCleanHashmap ;

    public CustomAdapter(Context con, ArrayList<String> pid,
            ArrayList<String> pname, ArrayList<String> pimg,
            ArrayList<String> wash, ArrayList<String> dry,
            ArrayList<String> iron, int KEY) {
        p_id = pid;
        context = con;
        p_name = pname;
        imageId = pimg;
        wash_v = wash;
        dry_v = dry;
        iron_v = iron;
        this.KEY = KEY;
        imageLoader = new ImageLoader(context);
        laundryHashmap = new HashMap<Integer, Integer>();
        ironHashmap = new HashMap<Integer, Integer>();
        dryCleanHashmap = new HashMap<Integer, Integer>();
    }

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

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

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

    static class ViewHolder {
        TextView tv1, tv2, tv3;
        ImageView img;
        LinearLayout ll1, ll2, ll3, llRow;
    }

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

        ViewHolder vh = null;
        if (convertView == null) {



            vh = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.dynamic, parent, false);
            vh.ll1 = (LinearLayout) convertView.findViewById(R.id.ll1);
            vh.ll2 = (LinearLayout) convertView.findViewById(R.id.ll2);
            vh.ll3 = (LinearLayout) convertView.findViewById(R.id.ll3);
            vh.llRow = (LinearLayout) convertView.findViewById(R.id.llRow);
            vh.tv1 = (TextView) convertView.findViewById(R.id.tv1);
            vh.tv2 = (TextView) convertView.findViewById(R.id.tv3);
            vh.tv3 = (TextView) convertView.findViewById(R.id.tv5);
            vh.img = (ImageView) convertView.findViewById(R.id.ivImage);

            convertView.setTag(vh);
        }

        else
            vh = (ViewHolder) convertView.getTag();
        vh.tv1.setText(wash_v.get(position));
        vh.tv2.setText(iron_v.get(position));
        vh.tv3.setText(dry_v.get(position));
        ImageView image = vh.img;
        imageLoader.DisplayImage(imageId.get(position), image);

        final TextView tvVal = (TextView) convertView.findViewById(R.id.tvVal);
        final TextView tvVal1 = (TextView) convertView
                .findViewById(R.id.tvVal1);
        final TextView tvVal2 = (TextView) convertView
                .findViewById(R.id.tvVal2);
        if (Appconstants.laundryHashmap != null
                && Appconstants.laundryHashmap.size() > 0) {
            for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {
                tvVal.setText(""+Appconstants.laundryHashmap.get(i));
            }

        }
        if (Appconstants.ironHashmap != null
                && Appconstants.ironHashmap.size() > 0) {
            for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {
                tvVal1.setText(""+Appconstants.ironHashmap.get(i));
            }

        }
        if (Appconstants.dryCleanHashmap != null
                && Appconstants.dryCleanHashmap.size() > 0) {
            for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {
                tvVal2.setText(""+Appconstants.dryCleanHashmap.get(i));
            }

        }

        i = new Intent("com.example.safewash");

        vh.ll1.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {
                int count = 0;

                String washItem = wash_v.get(position);
                int washItems = Integer.parseInt(washItem);

                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;

                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();

                    washItemCount = Float.parseFloat(washItem);

                    tvVal.setVisibility(View.VISIBLE);

                    count = Integer.parseInt(tvVal.getText().toString());
                    countVal1 = ++count;

                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal1", countVal1);
                    context.sendBroadcast(i);

                    laundryHashmap.put(position, countVal1);
                    Appconstants.laundryHashmap = laundryHashmap;

                    if (Appconstants.laundryHashmap != null
                            && Appconstants.laundryHashmap.size() > 0) {
                        for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {

                            value1 = Appconstants.laundryHashmap.get(position);
                            tvVal.setText("" + value1);
                        }
                    }

                }
            }
        });

        vh.ll2.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {
                int count = 0;
                String washItem = iron_v.get(position);

                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;
                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();
                    washItemCount = Float.parseFloat(washItem);

                    tvVal1.setVisibility(View.VISIBLE);
                    count = Integer.parseInt(tvVal1.getText().toString());
                    countVal2 = ++count;
                    // tvVal1.setText("" + countVal2);
                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal2", countVal2);
                    context.sendBroadcast(i);
                    tvVal1.setText("" + countVal2);

                    ironHashmap.put(position, countVal2);
                    // int val = ironHashmap.get(position);

                    Appconstants.ironHashmap = ironHashmap;

                    if (Appconstants.ironHashmap != null
                            && Appconstants.ironHashmap.size() > 0) {
                        for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {

                            value2 = Appconstants.ironHashmap.get(position);

                            tvVal1.setText("" + value2);

                        }
                    }

                }
            }
        });
        vh.ll3.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {

                int count = 0;
                String washItem = dry_v.get(position);
                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;

                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();

                    washItemCount = Float.parseFloat(washItem);

                    tvVal2.setVisibility(View.VISIBLE);
                    count = Integer.parseInt(tvVal2.getText().toString());
                    countVal3 = ++count;
                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal3", countVal3);
                    context.sendBroadcast(i);
                    dryCleanHashmap.put(position, countVal3);
                    Appconstants.dryCleanHashmap = dryCleanHashmap;

                    if (Appconstants.dryCleanHashmap != null
                            && Appconstants.dryCleanHashmap.size() > 0) {
                        for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {

                            value2 = Appconstants.dryCleanHashmap.get(position);
                            tvVal2.setText("" + value3);
                        }
                    }

                }
            }
        });


        return convertView;
    }

}

But here my problem is when click on the linearlayout the value will be incremented by one it is working fine and that value is also stored in hashmap based on position,but when i scroll the listview up and down the values is clearing how we can issue the problem 但是这里我的问题是当单击linearlayout时,该值将增加一,这很好,并且该值也根据位置存储在哈希图中,但是当我上下滚动listview时,这些值正在清除我们可以发出的值问题

help me guys 帮帮我

thank in Advance.... 预先感谢....

can you place this below code in if block 您可以将以下代码放置在if块中吗

     tvVal = (TextView) convertView.findViewById(R.id.tvVal);
     tvVal1 = (TextView) convertView.findViewById(R.id.tvVal1);
     tvVal2 = (TextView) convertView.findViewById(R.id.tvVal2);

and declare the textview in viewholder class 并在viewholder类中声明textview

TextView tvVal,tvVal1,tvVal2;

your ViewHolder should looks like this: 您的ViewHolder应该看起来像这样:

static class ViewHolder {
        TextView tv1, tv2, tv3,tvVal1,tvVal2,tvVal3;
        ImageView image;
        LinearLayout ll1, ll2, ll3, llRow;
    }

Now update the following code as: 现在将以下代码更新为:

  if (convertView == null) {
       //other viewholder reference
        vh.tvVal = (TextView) convertView.findViewById(R.id.tvVal);
        vh.tvVal1 = (TextView) convertView.findViewById(R.id.tvVal1);
        vh.tvVal2 = (TextView) convertView.findViewById(R.id.tvVal2);
        vh.image=(ImageView) convertView.findViewById(R.id.ivImage)
    }

Now update your else part as: 现在将其他部分更新为:

imageLoader.DisplayImage(imageId.get(position), vh.image);

if (Appconstants.laundryHashmap != null
        && Appconstants.laundryHashmap.size() > 0) {
    for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {
        vh.tvVal.setText(""+Appconstants.laundryHashmap.get(i));
    }

}
if (Appconstants.ironHashmap != null
        && Appconstants.ironHashmap.size() > 0) {
    for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {
        vh.tvVal1.setText(""+Appconstants.ironHashmap.get(i));
    }

}
if (Appconstants.dryCleanHashmap != null
        && Appconstants.dryCleanHashmap.size() > 0) {
    for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {
        vh.tvVal2.setText(""+Appconstants.dryCleanHashmap.get(i));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM