Android Studio中的Android应用程序存在简单问题。我要创建两个类来在我的应用程序中添加搜索activity
并且非常成功。但是,我有问题。问题是用户想要从结果中选择人员并将用户ID传递给另一个activity
" User_Profile "。但是,我无法做到这一点。任何人都可以帮助我吗?
搜索类:
public class Search extends AppCompatActivity {
EditText se_input;
RecyclerView results_list;
DatabaseReference mUDB;
FirebaseUser mFBU;
SearchAdapter searchAdapter;
ArrayList<String> fullNameList,userNameList, userIdList,profileImageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mUDB = FirebaseDatabase.getInstance().getReference();
mFBU = FirebaseAuth.getInstance().getCurrentUser();
se_input = (EditText) findViewById(R.id.se_input);
results_list = (RecyclerView) findViewById(R.id.results_list);
results_list.setHasFixedSize(true);
results_list.setLayoutManager(new LinearLayoutManager(this));
results_list.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
/*
* Create a array list for each node you want to use
*/
fullNameList = new ArrayList<>();
userNameList = new ArrayList<>();
userIdList = new ArrayList<>();
profileImageList = new ArrayList<>();
se_input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
setAdapter(s.toString());
} else {
/*
* Clear the list when editText is empty
*/
fullNameList.clear();
userNameList.clear();
userIdList.clear();
profileImageList.clear();
results_list.removeAllViews();
}
}
});
}
private void setAdapter(final String searchedString) {
mUDB.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
/*
* Clear the list for every new search
*/
fullNameList.clear();
userNameList.clear();
userIdList.clear();
profileImageList.clear();
results_list.removeAllViews();
int counter = 0;
/*
* Search all users for matching searched string
*/
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String uid = snapshot.getKey();
String full_name = snapshot.child("name").getValue(String.class);
String user_name = snapshot.child("username").getValue(String.class);
String profile_pic = snapshot.child("image").getValue(String.class);
if (full_name.toLowerCase().contains(searchedString.toLowerCase())) {
fullNameList.add(full_name);
userNameList.add(user_name);
userIdList.add(uid);
profileImageList.add(profile_pic);
counter++;
} else if (user_name.toLowerCase().contains(searchedString.toLowerCase())) {
fullNameList.add(full_name);
userNameList.add(user_name);
userIdList.add(uid);
profileImageList.add(profile_pic);
counter++;
}
/*
* Get maximum of 15 searched results only
*/
if (counter == 15)
break;
}
searchAdapter = new SearchAdapter(Search.this, fullNameList, userNameList, userIdList, profileImageList);
results_list.setAdapter(searchAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
SearchAdapter类:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
Context context;
ArrayList<String> fullNameList,userNameList, userIdList,profileImageList;
class SearchViewHolder extends RecyclerView.ViewHolder
{
ImageView profileImage;
TextView full_name, user_name,user_id;
public SearchViewHolder(View itemView) {
super(itemView);
profileImage = (ImageView) itemView.findViewById(R.id.profileImage);
full_name = (TextView) itemView.findViewById(R.id.full_name);
user_name = (TextView) itemView.findViewById(R.id.user_name);
user_id = (TextView) itemView.findViewById(R.id.user_id);
}
}
public SearchAdapter(Context context, ArrayList<String> fullNameList, ArrayList<String> userNameList, ArrayList<String> userIdList, ArrayList<String> profileImageList) {
this.context = context;
this.fullNameList = fullNameList;
this.userNameList = userNameList;
this.userIdList = userIdList;
this.profileImageList = profileImageList;
}
@Override
public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.search_list_item, parent, false);
return new SearchAdapter.SearchViewHolder(view);
}
@Override
public void onBindViewHolder(final SearchViewHolder holder, int position) {
holder.full_name.setText(fullNameList.get(position));
holder.user_name.setText(userNameList.get(position));
holder.user_id.setText(userIdList.get(position));
Glide.with(context).load(profileImageList.get(position)).into(holder.profileImage);
}
@Override
public int getItemCount() {
return fullNameList.size();
}
}
用户个人资料类:
public class UserProfile extends AppCompatActivity {
private CircleImageView u_pro_img;
private TextView u_user_name, u_fullname, u_bio, u_fricou;
private Button add_fri;
private DatabaseReference mUDBR;
private TextView user_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
u_pro_img = (CircleImageView) findViewById(R.id.u_pro_img);
u_user_name = (TextView) findViewById(R.id.u_user_name);
u_fullname = (TextView) findViewById(R.id.u_fullname);
u_bio = (TextView) findViewById(R.id.u_bio);
u_fricou = (TextView) findViewById(R.id.u_fricou);
add_fri = (Button) findViewById(R.id.add_fri);
mUDBR = FirebaseDatabase.getInstance().getReference();
user_id = (TextView) findViewById(R.id.user_id);
}
}
当用户从结果中选择人物时,我只想将user id
密钥传递给用户个人资料活动。我希望你理解我。
答案 0 :(得分:0)
使用以下代码将userId
发送到 ProfileActivity (此处从 SearchAdapter 类发送数据)
@Override
public void onBindViewHolder(final SearchViewHolder holder, int position) {
holder.full_name.setText(fullNameList.get(position));
holder.user_name.setText(userNameList.get(position));
holder.user_id.setText(userIdList.get(position));
Glide.with(context).load(profileImageList.get(position)).into(holder.profileImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//for sending data UserProfile
Intent intent = new Intent(context,UserProfile.class);
intent.putExtra("USER_ID",String.valueOf(userIdList.get(position)));
context.startActivity(intent);
}
});
}
UserProfile.cass 按以下方式获取userId
:
String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
u_pro_img = (CircleImageView) findViewById(R.id.u_pro_img);
u_user_name = (TextView) findViewById(R.id.u_user_name);
u_fullname = (TextView) findViewById(R.id.u_fullname);
u_bio = (TextView) findViewById(R.id.u_bio);
u_fricou = (TextView) findViewById(R.id.u_fricou);
add_fri = (Button) findViewById(R.id.add_fri);
mUDBR = FirebaseDatabase.getInstance().getReference();
user_id = (TextView) findViewById(R.id.user_id);
//Getting data here UserId
userId = getIntent().getStringExtra("USER_ID","");
Toast toast=Toast.makeText(getApplicationContext(),"USER ID "+userId ,Toast.LENGTH_SHORT);
}}