大家好,你能帮我解决一个问题,当从 json 删除、添加或更新数据时,我的 recyclerview 应该自动更新

Posted

技术标签:

【中文标题】大家好,你能帮我解决一个问题,当从 json 删除、添加或更新数据时,我的 recyclerview 应该自动更新【英文标题】:Hi Everyone, can you help me in solving a problem where my recyclerview should update automatically when data is deleted, added or updated from json 【发布时间】:2021-02-15 15:25:20 【问题描述】:

我有一个问题,我的代码无法根据 json 更改自动更新每个数据, 更改应该在这里自动发生,我正在使用 volley 库来处理它们

MainActivity

public class Gaji extends AppCompatActivity 
    private RecyclerView mRecyclerView;
    private static final String TAG = MainActivity.class.getSimpleName();
    private ExampleAdapter mExampleAdapter;
    private ArrayList<ExampleItem> mExampleList;
    private RequestQueue mRequestQueue;
    private RecyclerView.Adapter adapter;
    SessionManager sessionManager;
    String getId;
    String tag_json_obj = "json_obj_req";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gaji);
        mRecyclerView = findViewById(R.id.recycler_view);
        sessionManager = new SessionManager(this);
        HashMap<String, String> user = sessionManager.getUserDetail();
        getId = user.get(sessionManager.ID);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mExampleList = new ArrayList<>();
        mRequestQueue = Volley.newRequestQueue(this);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom);
        bottomNavigationView.setSelectedItemId(R.id.gaji);
        bottomNavigationView.setOnNavigationItemSelectedListener(new 
        BottomNavigationView.OnNavigationItemSelectedListener() 
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) 
                switch (item.getItemId())
                    case R.id.gaji:
                        break;
                    case R.id.home:
                        //Beranda
                        startActivity(new Intent(getApplicationContext(), MainActivity.class ));
                        overridePendingTransition(0,0);
                        break;
                    case R.id.absen:
                        //Presensi harian
                        startActivity(new Intent(getApplicationContext(), Izin.class ));
                        overridePendingTransition(0,0);
                        break;
                    case R.id.scan:
                        //Scan
                        startActivity(new Intent(getApplicationContext(), ScanQrBarcode.class ));
                        overridePendingTransition(0,0);
                        break;
                    case R.id.profile:
                        //Profile
                        startActivity(new Intent(getApplicationContext(), Profile.class ));
                        overridePendingTransition(0,0);
                        break;
                
                return false;
            
        );
        parseJSON();
    
    private void parseJSON() 
        String url = Constans.BaseUrl +"gaji.php";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() 
                    @Override
                    public void onResponse(String response) 
                        Log.i(TAG, response.toString());
                        try 
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("hasil");
                            for (int i = 0; i < jsonArray.length(); i++) 
                                JSONObject hit     = jsonArray.getJSONObject(i);
                                String creatorName = hit.getString("gaji_pokok");
                                String asuransi    = hit.getString("asuransi");
                                String potongan    = hit.getString("potongan");
                                mExampleList.add(new ExampleItem(creatorName, asuransi, potongan));
                            
                            mExampleAdapter = new ExampleAdapter(Gaji.this, mExampleList);
                            mRecyclerView.setAdapter(mExampleAdapter);
                            mExampleAdapter.notifyDataSetChanged();
                         catch (JSONException e) 
                            e.printStackTrace();
                         
                    
                , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 
                error.printStackTrace();
            
        )
            @Override
            protected Map<String, String> getParams() 
                // Posting parameters ke post url
                Map<String, String> params = new HashMap<>();
                params.put("id_pegawai", getId);
                return params;
            ;
        AppController.getInstance().addToRequestQueue(stringRequest, tag_json_obj);
    

Adapter

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> 
    private Context mContext;
    private ArrayList<ExampleItem> mExampleList;
    public ExampleAdapter(Context context, ArrayList<ExampleItem> exampleList) 
        mContext = context;
        mExampleList = exampleList;
    
    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View v = LayoutInflater.from(mContext).inflate(R.layout.example_item, parent, false);
        return new ExampleViewHolder(v);
    
    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) 
        ExampleItem currentItem = mExampleList.get(position);
        String creatorName = currentItem.getCreator();
        String likeCount   = currentItem.getLikeCount();
        String potongan    = currentItem.getPotongan();
        holder.mTextViewCreator.setText("gaji pokok: " +creatorName);
        holder.mTextViewLikes.setText("asuransi: " + likeCount);
        holder.mPotongan.setText("potongan: " + potongan);
    
    @Override
    public int getItemCount() 
        return mExampleList.size();
    
    public class ExampleViewHolder extends RecyclerView.ViewHolder 
        public TextView mTextViewCreator;
        public TextView mTextViewLikes;
        public TextView mPotongan;
        public ExampleViewHolder(View itemView) 
            super(itemView);
            mTextViewCreator = itemView.findViewById(R.id.text_view_creator);
            mTextViewLikes = itemView.findViewById(R.id.text_view_likes);
            mPotongan = itemView.findViewById(R.id.text_view_potongan);
        
    

Model

public class ExampleItem 
    private String mCreator;
    private String mLikes;
    private String mPotongan;
    public ExampleItem(String creator, String likes, String potongan) 
        mCreator = creator;
        mLikes = likes;
        mPotongan = potongan;
    

    public String getCreator() 
        return mCreator;
    
    public String getLikeCount() 
        return mLikes;
    

    public String getPotongan() 
        return mPotongan;
    

【问题讨论】:

您正在重新初始化 parseJSON 方法中的适配器。还要清除您的基本概念。请参阅下面的链接了解基础知识:medium.com/androiddevelopers/… 【参考方案1】:

您应该使用 Recyclerview.Adapter 类中的 notifyDataSetChanged() 或 notifyItemChanged() 方法

【讨论】:

你能帮我写代码吗!!我不同意你的回答

以上是关于大家好,你能帮我解决一个问题,当从 json 删除、添加或更新数据时,我的 recyclerview 应该自动更新的主要内容,如果未能解决你的问题,请参考以下文章

你能帮我解决keras的尺寸问题吗?

你能帮我理解指针和地址吗?

我想从控制器获取数据以查看你能帮我吗

你能帮我实现克拉克和赖特算法吗?

你能帮我解决 Unity3d 项目中数组或枚举中的错误吗?

使用rest创建一个webservice来与json通信 - 教程