android - Recycler View 正在加载错误的数据或从错误的位置加载数据

标签 android android-recyclerview android-volley

我正在使用 Recycler View 显示倒数计时器列表,每个计时器都显示在卡片 View 中。但是,在向下滚动 Recycler View 并向上滚动后,卡片的某些内容发生了变化,如下面的两个屏幕截图所示。滚动后红色内容发生变化。

最初,当第一次加载回收器 View 时显示正确的信息如下: Correct Output

但在向下和向上滚动后,相同的卡片显示错误信息如下: Wrong output

我在主要 Activity 中使用 JSON 对象请求加载数据,如下所示:

public class MainActivity extends AppCompatActivity
{
String showUrl = "http://192.168.56.1/GetData.php";
private List<information> info;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    info = new ArrayList<>();


    setContentView(R.layout.activity_main);     //Cardlayout code
    createJSON();
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layout = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layout);
    myadapter adapter = new myadapter(info);
    recyclerView.setAdapter(adapter);


}

private void createJSON()
{
    RequestQueue requestQueue = Singelton.instantinate().getRequestqueue();                     

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,
            new Response.Listener<JSONObject>()                                                 
            {
                @Override
                public void onResponse(JSONObject response)
                {
                   parseJSONResponse(response);
                }
            },
            new Response.ErrorListener()                                                         
            {....});
    requestQueue.add(jsonObjectRequest);
}



private void parseJSONResponse(JSONObject response)
{

    try
    {
        JSONArray jsonArray = response.getJSONArray("students");
        for (int i = 0; i <= jsonArray.length(); i++)
        {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            int a = jsonObject.getInt("Year");
            int b = jsonObject.getInt("Month");
            int c = jsonObject.getInt("Day");
            int d = jsonObject.getInt("Hours");
            int e = jsonObject.getInt("Minutes");
            String mid = jsonObject.getString("Movieid");           
            int sesa=jsonObject.getInt("ses");             
            info.add(new information(a, b - 1, c, d, e,mid,sesa));
        }
    } catch (JSONException e)
    {
        e.printStackTrace();
    }
}

我的recycler View 适配器如下

public class myadapter extends RecyclerView.Adapter<myadapter.myviewholder>
{
public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
List<information> info;

Singelton singelton = Singelton.instantinate();
ImageLoader imageLoader = singelton.getImageLoader();
String x;


myadapter(List<information> info)
{
    this.info = info;

}

@Override
public myviewholder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cardcontent, parent, false);
    myviewholder pvh = new myviewholder(v);
    return pvh;
}

@Override
public void onBindViewHolder(final myviewholder holder, final int position)
{
    information in = info.get(position);
    holder.to = in;
    Calendar targetdate = Calendar.getInstance();                     //start of timer code

    targetdate.set(in.a, in.b, in.c, in.d, in.e);
    Calendar currentDate = Calendar.getInstance();
    final long diff = targetdate.getTimeInMillis() - currentDate.getTimeInMillis();
    new CountDownTimer(diff, 1)
    {        //will change every 1ms i.e 0.001 seconds

        public void onTick(long millisUntilFinished)
        {
            long diffSec = millisUntilFinished / 1000;
            long days = diffSec / SECONDS_IN_A_DAY;
            long secondsDay = diffSec % SECONDS_IN_A_DAY;
            long seconds = secondsDay % 60;
            long minutes = (secondsDay / 60) % 60;
            long hours = (secondsDay / 3600);
            long countmilliforsec = millisUntilFinished % 1000;
            long milliseconds = countmilliforsec % (SECONDS_IN_A_DAY);
            holder.textView1.setText(days + " days " + hours + " hours " + minutes + " minutes \n" + seconds + " secs " + String.format("%03d", milliseconds) + " millisconds " + "remaining ");
            holder.textView3.setText("Position" + position);    //Dispalying the position here
        }

        public void onFinish()
        {

            holder.textView1.setText("done!");
        }
    }.start();             //end of timer code



}
 public class myviewholder extends RecyclerView.ViewHolder{...}

我做错了什么。我知道每次向上滚动时都会调用 onBindViewHolder,但为什么我收到错误的位置?

最佳答案

与其为每个 View 创建计时器,不如尝试创建一个计时器,通过调用 notifyDataSetChanged() 刷新整个 RecyclerView。这样您就可以保持同步。

关于android - Recycler View 正在加载错误的数据或从错误的位置加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692819/

相关文章:

android - 如何在 Android Volley 中使用 setEntity?

android - 在 android activity 生命周期中应该在哪里关闭数据库?

Android 多项目选择 RecyclerView

多种语言的Android应用程序

java - 如何在 recyclerview 中左右两个方向进行无限滚动?

android - 如何在 ViewPager2 中以编程方式设置当前项目?

android - 为什么 Android-Volley 更快

android - 截击返回错误响应

android - 如何处理 AOSP 存储库同步上的 curl clone.bundle 错误

android - 如何在 ImageView 中显示图像?