RecylerView and multi-threaded "love and hatred", how to make your RecyclerView get the value passed by the ssm framework?


Regarding this issue, when I use the ssm framework 想在android端取到ssm框架传过来的数据,然后把数据传给RecylerView显示出来 . Then I ran into a lot of questions. I recently asked the teacher and said that it needs to be used runOnUiThread方法 . In fact, the teacher said before, but I used less, so I forgot. Don't talk nonsense, just move on to the topic.

The first step: the use of RecyclerView.

I won't say much about this. Generally, you can want to implement RecylerView and multi-threaded value transfer. I have a little understanding of the use of RecyclerView.
If you want to consolidate the use of RecyclerView, you can take a look at this blog of our teacher, which is very detailed and explains the most complete use of Android RecyclerView .

The second step: the role of runOnUiThread method.

General explanation : In the process of Android development, the main thread mainly completes UI drawing and responds to user operations. By default, most of our code is executed in the main thread, so we must always consider the main thread Happening. We all know that we need to start a child thread to complete a time-consuming operation to avoid blocking the main thread from affecting the user experience, and even ANR. However 子线程执行完要更新UI的时候,我们又必须回到主线程来更新, the commonly used method to achieve this function is to execute the runOnUiThread() method of Activity.

I think it mainly means using the data of the child thread to update the data of the main thread.

The third step: how to get the value of multithreading.

code show as below:

package com.example.reclycleview_thread;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<String> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView=findViewById(R.id.recycleView);
        Data();
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
    
    
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
            View view = View.inflate(getApplicationContext(), R.layout.layout_thread, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
    
    
            holder.textView.setText(list.get(position));
        }

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

    class MyViewHoder extends RecyclerView.ViewHolder {
    
    
        TextView textView;

        public MyViewHoder(@NonNull View itemView) {
    
    
            super(itemView);
            textView=itemView.findViewById(R.id.textView);
        }
    }

    public void Data(){
    
    
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                List<String> date=new ArrayList<>();
                for(int i=0;i<100;i++){
    
    
                    date.add(String.valueOf(i));
                }
                try {
    
    
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
    
    
                    @Override
                    public void run() {
    
    
                        list = date;
                        mMyAdapter = new MyAdapter();
                        mRecyclerView.setAdapter(mMyAdapter);
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
                        mRecyclerView.setLayoutManager(layoutManager);
                    }
                });
            }
        }).start();
    }

}

Code analysis:
According to all the codes above, the effect is clearer, because I am here to simulate the value passing of the ssm framework, because we generally use ssm框架进行增删改查等操作分为三种情况:
1 只从获取ssm框架传过来的数据.. For example: query all data
2 你传一个数据给ssm框架,然后ssm会传相应的数据给你.. For example: query function.
3 你只传数据给ssm框架.. For example: Add function.
Generally these acquisition methods have delays and will not acquire data immediately, so I useThrea.sleep() method for delay simulation operation
Insert picture description here

Why put the method in the red circle at the bottom of the figure above into runOnUiThread, let me show you, if 没有把上图方法放到runOnUiThread的方法, after all 实践出真知.

Correct form

The correct form is as shown below MyAdapter,LinerLayoutManager方法放到runOnUiThread方法种的形式,把它们放到主线程中的形式.

Insert picture description here
Then we look at the effect,Two seconds later, RecyclerView obtained the simulated data. Because my mobile phone emulator is broken recently, I can only use this device to demonstrate, but I don’t know how to take a gif with this device, so I can only upload a photo. You can also test it yourself.
Insert picture description here

Wrong form

The wrong form 不把MyAdapter,LinerLayoutManager方法放到runOnUiThread方法种的形式. as the picture shows.
Insert picture description here
Insert picture description here
Looking at the effect, we can see that no data is received,I waited almost 10 seconds and still no data,as the picture shows:

Insert picture description here

,
如果你没怎么用过ssm框架传值给RecyclerView,你可能不会这么感到深受,但是这个问题上次确实困扰了一天,把这个经验分享给大家,希望能帮到大家. Because I am also a student who is still studying, if there are mistakes or problems I want to raise, I will correct them.

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/111718486