【问题标题】:while scrolling Text values in Listview are changing滚动 Listview 中的文本值时正在更改
【发布时间】:2018-11-21 17:32:05
【问题描述】:

这是我对适配器的赞美

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fix_container"
>
<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:textSize="15sp"
    android:textStyle="bold"
    android:text="1"
    android:textColor="@color/black"/>
<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="@color/black"
    android:layout_toRightOf="@id/tv1"
    android:layout_marginTop="4dp"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:text="1"
    android:layout_toEndOf="@id/tv1"
    android:layout_marginStart="10dp" />
<TextView
    android:id="@+id/tv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textSize="15sp"
    android:layout_toRightOf="@id/tv2"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="10dp"
    android:layout_toEndOf="@id/tv2"
    android:text="1"
    android:layout_marginStart="10dp" />

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter{
private LayoutInflater layoutInflater;
private Production_Dispatch_Res_DTO rem_res_dto;
List<String> eff_list = new ArrayList<String>();
String[] eff_list_split;

public CustomListAdapter(Context context, Production_Dispatch_Res_DTO res, 
List<String> eff_list) {
    this.rem_res_dto = res;
    layoutInflater = LayoutInflater.from(context);
    this.eff_list = eff_list;
}

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

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

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try{
        ViewHolder holder;
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.lists,null);
            holder = new ViewHolder();
            holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
            holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        eff_list_split= eff_list.get(position).split(",");

        int eff_list_split_size = eff_list_split.length;

        switch (eff_list_split_size){
            case 1:
                if(eff_list_split[0].equals("-1")){
                    holder.tv1.setText("");
                }
                else
                holder.tv1.setText(eff_list_split[0]);
                break;
            case 2:
                if(eff_list_split[0].equals("-1"))
                    holder.tv1.setText("");
                else
                holder.tv1.setText(eff_list_split[0]);

                if(eff_list_split[1].equals("-1"))
                    holder.tv2.setText("");

                else
                holder.tv2.setText(eff_list_split[1]);
                break;
            case 3:
                if(eff_list_split[0].equals("-1"))
                    holder.tv1.setText("");
                else
                    holder.tv1.setText(eff_list_split[0]);

                if(eff_list_split[1].equals("-1"))
                    holder.tv2.setText("");

                else
                    holder.tv2.setText(eff_list_split[1]);

                if(eff_list_split[2].equals("-1"))
                    holder.tv3.setText("");
                else
                holder.tv3.setText(eff_list_split[2]);
                break;
    }
    }catch (Exception e){
        Log.d(VilanConstants.TAG,"/Excp @listAdp"+ e.toString());
    }

    return convertView;
}

public class ViewHolder{
    public TextView tv1;
    public TextView tv2;
    public TextView tv3;
}
}

我在这样的布局中采用了列表视图

 <ListView
    android:id="@+id/product_list_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="3dp"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/card_view_dispatch"/>

一开始它是正确的。每次滚动时,textviews 中的滚动列表视图数据都会发生变化。

我已经尝试过之前回答过的 stackoverflow 中的解决方案。但他们不会为我工作。

任何帮助将不胜感激。

我是安卓新手。

谢谢。

【问题讨论】:

  • 尝试硬编码您的数据并检查

标签: android xml android-layout listview


【解决方案1】:

试试这个,

holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);

之后

if(convertView == null){
     .....
} else {
      ....
}

检查并尝试。当 convertView 为空时,您正在初始化。但是每次都这样做并检查它是否有效?

【讨论】:

    【解决方案2】:

    更新此代码:

     public class ViewHolder{
        public TextView tv1;
        public TextView tv2;
        public TextView tv3;
      }
    

    到此

    public class ViewHolder{
                public TextView tv1;
                public TextView tv2;
                public TextView tv3;
    
            holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
            holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
            }
    

    【讨论】:

      【解决方案3】:

      我看过你的代码。这是正确的。但是,您需要在所有情况 1、2 和 3 中使用所有视图(TextView)。因为当情况 1 为真时,仅使用 tv1(TextView) 而其他情况则不使用。因此,getView() 方法也会自动使用其他视图。因为滚动列表时会一次又一次地调用 getView() 方法。

      尝试在所有情况下使用所有视图或根据需要更改逻辑。但是,当大小写为真时,必须使用所有视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-17
        • 1970-01-01
        相关资源
        最近更新 更多