解决Android时时更新listview数组越界问题

时时更新数据一般出现在金融、股票行业对数据的准确性要求极高情况下使用。

先来看看下面一段代码,

 public class MainActivity extends Activity {
     private RecyclerView listview;
     private boolean isstopThread=false;
     private Thread thread;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         listview=(RecyclerView)findViewById(R.id.listview);
         listview.setHasFixedSize(true);
         RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
         listview.setLayoutManager(layoutManager);
         GoldMarket();
     }

     @Override
     protected void onResume() {
         super.onResume();
         thread = new ThreadInterrupt();
         thread.start();
     }

     public class ThreadInterrupt extends Thread {
         public void run() {
             try {
                 while (!isstopThread) {
                     GoldMarket();
                     sleep(1000); // 延迟2秒
                 }
             } catch (InterruptedException e) {
                 //Logger.show("error",e.getMessage().toString());
             }
         }
     }

     //加入这里是数据是从网络获取的或者数据量大,更新界面慢
     private void GoldMarket(){
         List<String> list=new ArrayList<>();
         for (int i = 0; i < 10; i++) {
             list.add("name"+i);
         }
         MyAdapter myAdapter=new MyAdapter(list);
         listview.setAdapter(myAdapter);
     }
 }
 public class MyAdapter extends RecyclerView.Adapter {
     private List<String> list=new ArrayList<>();
     public MyAdapter(List<String> list) {
         this.list=list;
     }

     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
         View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_list_item, null);
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
         view.setLayoutParams(lp);
         return new MyViewHolder(view);
     }

     @Override
     public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
         MyViewHolder holder = (MyViewHolder) viewHolder;
         String My = list.get(i);
         holder.tv_name.setText(My);
     }

     @Override
     public int getItemCount() {
         return list.size();
     }

     class MyViewHolder extends RecyclerView.ViewHolder
     {
         public TextView tv_name;
         public MyViewHolder(View itemView) {
             super(itemView);
             tv_name = (TextView) itemView.findViewById(R.id.tv_name);

         }
     }

 }

layou_list_item

 <?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">
     <TextView
         android:id="@+id/tv_name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
 </LinearLayout>

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:orientation="vertical"
               android:layout_width="match_parent"
               android:layout_height="match_parent">

     <android.support.v7.widget.RecyclerView
         android:id="@+id/listview"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
 </LinearLayout>

这样用线程时时更新listview会偶现错误说是数组越界,比如

解决Android时时更新listview数组越界问题

当时想了好久,也问了群里很多人,他们只是说说数组越界,但没说出根本,之后再网上百度 http://www.bkjia.com/Androidjc/821451.html 找到可能的原因,但还是不知道如何处理,

之后再Android技术交流群的群员【潜水】上海-安卓-繁华的大神帮忙,大概说了下他自己的理解,

因为在子线程中我们使用了界面相关的list,而界面此时也在占用此list,他们会公用一个内存,当list.size的大小改变,变得比之前小时,就会出现数组越界,比如之前的数组大小为5,当之后list变成3时,list.get(i)中的i的最大值还保留着之前的5的值,但遍历到最大时,就会越界,说是list大小只有3,没有index为5的数据

解决办法:在adapter里面,给adapter的list赋值时用如下代码

this.list.clear();
this.list.addAll(list);

整个Adapter代码如下

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangou on 2016/3/30.
 */
public class MyAdapter extends RecyclerView.Adapter {
    private List<String> list=new ArrayList<>();
    public MyAdapter(List<String> list) {
        this.list.clear();
        this.list.addAll(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_list_item, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        MyViewHolder holder = (MyViewHolder) viewHolder;
        String My = list.get(i);
        holder.tv_name.setText(My);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tv_name;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);

        }
    }
//    public interface OnMySelected {
//        void selectedMy(String itemCode);
//    }
//
//    private OnMySelected mMySelected;
//
//    public void setOnMySelected(OnMySelected mMySelected) {
//        this.mMySelected = mMySelected;
//    }
}

  


欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2877

本人联系方式:

更多精彩分享,可关注我的微信公众号:

解决Android时时更新listview数组越界问题

      若想给予我分享更多知识的动力,请扫描下面的微信打赏二维码,谢谢!:

解决Android时时更新listview数组越界问题

微信号:WeixinJungle

              解决Android时时更新listview数组越界问题

邮箱:oneou6688@163.com

解决Android时时更新listview数组越界问题
上一篇:设计模式 -- 组合模式 (Composite Pattern)


下一篇:CSS清除float浮动