android 单击按钮时从循环视图获取选定项目文本

wgx48brx  于 2022-12-16  发布在  Android
关注(0)|答案(2)|浏览(113)

我只是想创建一个列表,当我点击“Selanjutnya”按钮时,它将显示当前选定的项目文本,但我不知道如何做到这一点。

所以当我点击“Selanjutnya”时,它会显示22,23。目前,我使用大书呆子牧场多选来进行多选工作。
实现“com.bignerdranch.android:回收视图-多选:+”
这是我的密码。
适配器

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),

6jjcrrmo

6jjcrrmo1#

您可以创建一个添加到适配器中的接口以及它的侦听器。

public interface OnAdapterItemClicked {
  // you can name it what ever you need add pass more parameters
  void itemClicked(String text); 
}

然后在适配器中,初始化它并在单击事件上添加侦听器,

private OnAdapterItemClicked mListener = null;
...
@Override
public void onBindViewHolder(@NonNull YourAdapter.ViewHolder holder, 
int position) {
  ...
holder.YourBtn.setOnClickListener(....{
  mListener.itemClicked("string you need to pass")
 });
 ...
}

...
public void setOnItemClickListener(OnAdapterItemClicked listener) {
    this.mListener = listener;
}

最后,在活动/片段中,您在recyclerview上添加了一个侦听器

rv.setOnItemClickListener(new OnAdapterItemClicked () {
                @Override
                public void itemClicked(String text) {
                    Log.d(TAG, "itemClicked: "+text);
                }
            });
ao218c7q

ao218c7q2#

在适配器中,可以按如下方式添加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);
    }

相关问题