单击按钮时,从RecycleView中获取选定的项目文本

时间:2019-03-07 06:59:30

标签: android android-recyclerview android-listview android-adapter

我只想创建一个列表,当我单击“ Selanjutnya”按钮时将显示当前选中的项目文本,但是我不知道该怎么做。

Here is the screenshot of the list.

因此,当我单击“ Selanjutnya”时,它将显示22、23。当前,我使用Big Nerd Ranch多项选择功能来使多项选择工作有效。

  

实现'com.bignerdranch.android:recyclerview-multiselect:+'

这是我的密码。

适配器

    public class KetuaAbsenAdapter extends RecyclerView.Adapter<KetuaAbsenAdapter.ViewHolder> {
        private List<KetuaAbsenList> listItems;
        private Context context;
        private MultiSelector mMultiSelector = new MultiSelector();

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

        public KetuaAbsenAdapter(List<KetuaAbsenList> listItems, Context context) {
            this.listItems = listItems;
            this.context = context;
        }



        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            final KetuaAbsenList listItem = listItems.get(position);
            holder.textViewMuridno.setText(listItem.getMuridno());
            holder.textViewMuridnama.setText(listItem.getMuridnama());
        }


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

        public class ViewHolder extends SwappingHolder implements View.OnClickListener {
            public TextView textViewMuridno;
            public TextView textViewMuridnama;
            public TextView textViewMuridkelas;
            public LinearLayout list_item_linear;
            public ViewHolder(View itemView) {
                super(itemView, mMultiSelector);
                textViewMuridno = (TextView) itemView.findViewById(R.id.muridno);
                textViewMuridnama = (TextView) itemView.findViewById(R.id.muridnama);
                textViewMuridkelas = (TextView) itemView.findViewById(R.id.muridkelas);
                list_item_linear = (LinearLayout) itemView.findViewById(R.id.list_item_linear);
                list_item_linear.setOnClickListener(this);
                itemView.setOnClickListener(this);
                mMultiSelector.setSelectable(true);
            }

            @Override
            public void onClick(View v) {
                if (mMultiSelector.tapSelection(this)) {
                    Toast.makeText(context, "terpilih", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(context, "tidak terpilih", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

列表类别

    public class KetuaDaftarAbsen extends AppCompatActivity {
        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;
        private List<KetuaAbsenList> listItems;
        EditText YangTidakHadir;
        Button KetuaLanjut;
        Dialog dialog;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ketua_daftar_absen);
            YangTidakHadir = (EditText)findViewById(R.id.YangTidakHadir);
            recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            listItems = new ArrayList<>();
            loadRecyclerViewData();
            LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                    new IntentFilter("custom-message"));
        }

        public void loadRecyclerViewData(){
            final String url = "https://gameblong.com/raset/daftar_absen.php";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                String status = jsonObject.getString("status");
                                if(status.equals("sukses")){
                                    JSONArray array = jsonObject.getJSONArray("semua");
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject o = array.getJSONObject(i);
                                        KetuaAbsenList item = new KetuaAbsenList(
                                                o.getString("murid_no"),
                                                o.getString("murid_nama"),
                                                o.getString("murid_kelas")
                                        );
                                        listItems.add(item);
                                    }
                                } else {
                                }

                                adapter = new KetuaAbsenAdapter(listItems, getApplicationContext());
                                recyclerView.setAdapter(adapter);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    return params;
                }
            };
            Volley.newRequestQueue(this).add(stringRequest);
        }

        public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, Intent intent) {
                final String muno = intent.getStringExtra("muridnomor");
                KetuaLanjut = (Button)findViewById(R.id.KetuaLanjut);
                KetuaLanjut.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Toast.makeText(context, muno, Toast.LENGTH_LONG).show();
                    }
                });

            }
        };

    }

我可以使用mMultiSelector.getSelectedPositions();在我的适配器上,但是只有这样才能给出当前选定的位置(例如0、1),

Here is the screenshot.

1 个答案:

答案 0 :(得分:0)

在适配器中,您可以像这样添加onClickListener

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.absen_list, parent, false);
          v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //your logic goes here
            }
        });
        return new ViewHolder(v);
    }