私はRecyclerViewを含むアクティビティを持っており、サーバからのデータをいくつか表示します。問題は、私はMYAdapter
クラスのRecyclerViewのonCLickを設定していますが、RecyclerViewを含む私のアクティビティであるいくつかのデータに基づいてRecyclerViewのOnClickを処理する必要があることです。今までにこのint id = getIntent().getIntExtra("IDUSERTYPE", 0);
IDがが含まれている場合アダプタからアクティビティにデータを返す方法
public class ActivityAllUsers extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listitems;
private static final String URL_DATA = "http://shayea.tk/sof/get_allusers.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int id = getIntent().getIntExtra("IDUSERTYPE", 0);// -1 is default value
recyclerView = (RecyclerView) findViewById(R.id.recyvlerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listitems = new ArrayList<>();
loadRecyclerviewData();
}
private void loadRecyclerviewData()
{
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
dialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("result");
for (int i =0;i<array.length();i++)
{
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("usr_id"),
o.getString("usr_name"),
o.getString("usr_phone"),
o.getString("usr_email"),
o.getString("usr_status"),
o.getString("prv_name"),
o.getString("org_name")
);
listitems.add(item);
}
adapter = new MyAdapter(listitems , getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
は、私が利用できるようにRecyclerViewのクリック時にしたいとIDは何が含まれている場合、他のOnClickはアクティブではありません。私はここにActivityAllUsers IHであるIDが必要
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ListItem listItem = listItems.get(position);
holder.txtusername.setText(listItem.getusername());
holder.txtuseremail.setText(listItem.getEmail());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView txtusername;
public TextView txtuseremail;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
txtusername = (TextView) itemView.findViewById(R.id.textviewusername);
txtuseremail = (TextView) itemView.findViewById(R.id.textviewuseremail);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
}
}
}
:
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
int id = getIntent()。getIntExtra( "IDUSERTYPE"、0);このid? – user2025187
@ user2025187 yes 'int id = getIntent()。getIntExtra( "IDUSERTYPE"、0); ' – Shayea