You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio搜索结果选中后传递用户ID至User_Profile活动求助

Hey there! Let's fix that issue where you can't pass the user ID to your User_Profile activity when tapping a search result. I’ll break down the steps across your Search, SearchAdapter, and User_Profile classes clearly.


Step 1: Add Click Listener to SearchAdapter

First, we need to set up a click callback in your adapter to pass the selected user's ID back to the Search component. Here's how to implement it:

  1. Define a click listener interface inside your SearchAdapter class:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder> {
    // Your existing member variables (like a list of user data)
    private List<UserModel> mUserList;
    private OnUserClickListener mClickListener;

    // Add this interface for click callbacks
    public interface OnUserClickListener {
        void onUserSelected(String userId); // Use int if your user ID is numeric
    }

    // Setter for the click listener
    public void setOnUserClickListener(OnUserClickListener listener) {
        mClickListener = listener;
    }

    // Your existing ViewHolder class
    public class UserViewHolder extends RecyclerView.ViewHolder {
        // Bind your view elements here (e.g., userNameTextView)

        public UserViewHolder(View itemView) {
            super(itemView);
            // Initialize your views...

            // Set click listener on the entire list item
            itemView.setOnClickListener(v -> {
                if (mClickListener != null) {
                    int position = getAdapterPosition();
                    // Make sure we're not dealing with an invalid position
                    if (position != RecyclerView.NO_POSITION) {
                        UserModel selectedUser = mUserList.get(position);
                        // Pass the user ID to the callback
                        mClickListener.onUserSelected(selectedUser.getUserId());
                    }
                }
            });
        }
    }

    // Rest of your adapter code (onCreateViewHolder, onBindViewHolder, getItemCount)...
}

Note: Replace UserModel with your actual data class that holds user details, and getUserId() with the correct method to retrieve the user ID from your model.


Step 2: Handle the Click in Your Search Class

Next, in your Search activity/fragment, attach the click listener to the adapter and trigger the navigation to User_Profile when a user is selected:

// Inside your Search activity's onCreate (or fragment's onViewCreated)
SearchAdapter searchAdapter = new SearchAdapter(yourUserDataset);
searchAdapter.setOnUserClickListener(userId -> {
    // Create an intent to launch User_Profile
    Intent profileIntent = new Intent(SearchActivity.this, User_Profile.class);
    // Put the user ID into the intent as an extra
    profileIntent.putExtra("KEY_USER_ID", userId); // Use a consistent key here
    startActivity(profileIntent);
});

// Attach the adapter to your RecyclerView
recyclerView.setAdapter(searchAdapter);

If you're working with a Fragment, replace SearchActivity.this with getActivity().


Step 3: Retrieve the User ID in User_Profile

Finally, fetch the user ID from the intent in your User_Profile activity and use it to load the user's profile data:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    // Retrieve the user ID from the intent
    String userId = getIntent().getStringExtra("KEY_USER_ID");
    // If your user ID is an integer, use this instead:
    // int userId = getIntent().getIntExtra("KEY_USER_ID", -1); // -1 is a default invalid value

    // Validate the ID before proceeding
    if (userId != null) { // For int: if (userId != -1)
        // Call your method to load profile data using the ID
        loadUserProfileDetails(userId);
    } else {
        // Handle the case where no ID was passed (e.g., show error toast)
        Toast.makeText(this, "Failed to load user profile", Toast.LENGTH_SHORT).show();
        finish(); // Close the activity if we can't get the ID
    }
}

// Example method to load profile data
private void loadUserProfileDetails(String userId) {
    // Add your logic here: fetch from API, query local database, etc.
}

Quick Troubleshooting Tips
  • Double-check that User_Profile is registered in your AndroidManifest.xml (missing registration will cause a crash when launching the activity).
  • Ensure the key used in putExtra() and getStringExtra() matches exactly (typos are a common culprit!).
  • Verify that your UserModel's getUserId() method returns the correct value (log it if needed to confirm).

内容的提问来源于stack exchange,提问作者user5670198

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:34:07