【问题标题】:Textview values is deleted in listview while scrolling the listview in android?在android中滚动listview时,listview中的Textview值被删除?
【发布时间】:2015-09-07 15:15:15
【问题描述】:

我有一个列表视图,其中一行包含一个图像视图和 3 个布局,每个布局又包含 3 个文本视图

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>

我像这样使用自定义适配器,我从服务器获取一些字符串值,这些值在列表视图中设置

这里是代码

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;
    }

}

但是这里我的问题是,当单击线性布局时,值将增加一,它工作正常,并且该值也根据位置存储在哈希图中,但是当我上下滚动列表视图时,值正在清除如何我们可以提出问题

帮帮我

在此先感谢....

【问题讨论】:

  • 这是一个常见的问题......开发人员面临......使用持有人来避免......stackoverflow.com/questions/18780180/…
  • 这里我在一个线性布局的一侧多了一个文本视图,当我点击布局时,文本视图是不可见的,文本视图是可见的,并且值增加了 1 个类似
  • 声明 tvVal,tvVal1,tvVal2 image 在你的持有者类中,就像你对其他变量所做的那样并使用它。

标签: java android


【解决方案1】:

您的ViewHolder 应该如下所示:

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

现在将以下代码更新为:

  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)
    }

现在将您的 else 部分更新为:

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));
    }

}

【讨论】:

  • 好的,当我点击线性布局时我想看到文本视图,所以我这样写 tvval.setVisibility(View,VISIBLE);这个 tvVal 在 onClick 方法中是可用的,所以我们如何在这里获取 tvVal
  • 在您的 xml 文件中将可见性属性设置为消失,以便您不希望在视图中首次出现的视图。
  • 当我滚动列表视图时,文本视图将被删除
【解决方案2】:

你能把下面的代码放在 if 块中

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

并在viewholder类中声明textview

TextView tvVal,tvVal1,tvVal2;

【讨论】:

  • 当我将此代码置于 if 条件中时,我们如何设置 tvVal.setText 等值,其显示创建字段
  • 使用 View holder 对象可以为这些视图设置值
  • 当您向上滚动时,会创建新视图,然后如果您再次向下滚动,则会采用已创建的视图并在 Vh 中标记,它将执行 else 条件(对于已创建的视图)。
  • 我在onclick方法中设置了tvval值,vh没有被接管
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多