Мне нужна ваша помощь. Во-первых, могу ли я использовать два списка в одном окне просмотра?
** Это моя структура просмотра Recyclerview.
| (User's name) [ PBMS ]|
| (User's PBMS) [ Matching rate ]|
*PBMS means comparing my prefer to others and return matching-rate.
*Also I have two model(User, PBMS). They have getter/setter/empty constructor, full constructor.
*If I search users on specific condition, it will show users with matching result in the recyclerview.
** Это моя база данных. база данных.
tkitaka
ㄴUser
ㄴ50IPmMNzTMQm0heWc7BLRoMDGbs2(User 1)
- userName: "aaa"
- ...
ㄴshngJIzT8ieI1LiQ012peMHH5p13(User 2)
- userName: "aaa"
- ...
ㄴPBMS
ㄴ50IPmMNzTMQm0heWc7BLRoMDGbs2(User 1)
- accommondation : "A"
- expense: "B"
- flight: "C"
- guide: "C"
ㄴshngJIzT8ieI1LiQ012peMHH5p13(User 2)
- accommondation : "A"
- expense: "C"
- flight: "C"
- guide: "B"
В моем поиске есть два пользователя (я - Пользователь 1), моя PBMS - "ABCC", а PBMS User2 - "ACCB". В это время коэффициент совпадения составляет 50%. Я хочу показать эту скорость сопоставления в держателе представления переработчика. Т.Т.
. Поэтому я пытаюсь сослаться на две модели для позиции представления переработчика, я получил ошибку IndexOutOfBoundsException.
TripSearchListFragment.java
.
.
List<PBMS> PBMSList;
List<User> UserList;
TripSearchAdapter tripSearchAdapter;
.
.
onCreateView(){
UserList = new ArrayList<>();
PBMSList = new ArrayList<>();
// Call Listener
Query query2 = database.getReference().child("TripInfo").child(uCodeID);
query2.addValueEventListener(valueEventListener2);
tripSearchAdapter = new TripSearchAdapter(getContext(), UserList, PBMSList);
}
ValueEventListener valueEventListener2 = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot snapshot: dataSnapshot.getChildren()){
TripInfo tripInfo = snapshot.getValue(TripInfo.class);
userID = tripInfo.getUserID();
Log.d(TAG, "1 userID "+userID);
DatabaseReference UserRef = database.getReference().child("User").child(userID);
UserRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
User user = dataSnapshot.getValue(User.class);
String a = user.getUserID();
UserList.add(user);
Log.d(TAG, "2 userID "+a);
DatabaseReference PBMSRef = database.getReference().child("PBMS").child(a);
PBMSRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
PBMS pbms = dataSnapshot.getValue(PBMS.class);
PBMSList.add(pbms);
String b = pbms.getUserID();
Log.d(TAG, "3 userID "+b);
}
tripSearchAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
};
TripSearchAdapter.java
public class TripSearchAdapter extends RecyclerView.Adapter<TripSearchAdapter.ViewHolder> {
public TripSearchAdapter() {
}
// 아이템 리스트
private List<User> UserList;
private List<PBMS> PBMSList;
private Context context;
int score = 0;
private FirebaseAuth auth;
private FirebaseUser user;
private DatabaseReference UserRef, PBMSRef;
private String currentUser;
private FirebaseDatabase database;
String accommondation = "";
String meal = "";
String sTransportation = "";
String lTransportation = "";
String expense = "";
String preplan = "";
String spending = "";
String flight = "";
String guide = "";
String smoking = "";
String myAccommondation = "";
String myMeal = "";
String mySTransportation = "";
String myLTransportation = "";
String myExpense = "";
String myPreplan = "";
String mySpending = "";
String myFlight = "";
String myGuide = "";
String mySmoking = "";
private static final String TAG = "어댑터";
// 생성자에서 데이터 리스트 객체를 전달받음
public TripSearchAdapter(Context context, List<User> UserList, List<PBMS> PBMSList) {
this.context = context;
this.UserList = UserList;
this.PBMSList = PBMSList;
}
// 아이템 뷰를 저장하는 뷰홀더 클래스
public class ViewHolder extends RecyclerView.ViewHolder {
...
}
// 아이템 뷰를 위한 뷰홀더 객체 생성하여 리턴
@Override
public TripSearchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_search, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
getMyPBMS(); // my PBMS
return viewHolder;
}
// position에 해당하는 데이터를 뷰홀더의 아이템뷰에 표시
@Override
public void onBindViewHolder(TripSearchAdapter.ViewHolder holder, int position) {
final User user = UserList.get(position);
String a = user.getUserID();
Log.d(TAG, "user from adapter "+a);
// final PBMS pbms = PBMSList.get(position);
// String b = pbms.getAccommondation();
// Log.d(TAG, "어댑터 PBMS "+b);
# I can't construct PBMS model. If I try this 3 lines, I got error 'IndexOutOfBoundsException'
// // position에 해당하는 전체 유저 PBMS 값
// if (!PBMSList.isEmpty()){
// final PBMS pbms = PBMSList.get(position);
// accommondation = pbms.getAccommondation();
// meal = pbms.getMeal();
// sTransportation = pbms.getsTransportation();
// lTransportation = pbms.getlTransportation();
// expense = pbms.getExpense();
// preplan = pbms.getPreplan();
// spending = pbms.getSpending();
// flight = pbms.getFlight();
// guide = pbms.getGuide();
// smoking = pbms.getSmoking();
//
// String allPBMS2 = accommondation+meal+sTransportation+lTransportation+expense+preplan+spending+flight+guide+smoking;
// Log.d(TAG, "user PBMS★ "+allPBMS2);
// }
// getMatching(); // This is my matching method.
holder.tvUserName.setText(user.getUserName());
// holder.tvPBMS.setText(pbms.getAccommondation());
holder.tvMatchingRate.setText(String.valueOf(score * 10) + "%"); // Matching-rate in this part.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ChattingActivity.class);
intent.putExtra("userID", user.getUserID());
// intent.putExtra("userName", user.getUserName());
String a = user.getUserID();
context.startActivity(intent);
Log.d(TAG, "어댑터에서 보냄 "+a);
}
});
score = 0; // Make var score 0 before pass next recycler view.
}
# Get my PBMS to compare others.
public void getMyPBMS(){
auth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
user = auth.getCurrentUser();
currentUser = user.getUid();
PBMSRef = database.getReference().child("PBMS").child(currentUser);
PBMSRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
myAccommondation = dataSnapshot.child("accommondation").getValue(String.class);
myMeal = dataSnapshot.child("meal").getValue(String.class);
mySTransportation = dataSnapshot.child("sTransportation").getValue(String.class);
myLTransportation = dataSnapshot.child("lTransportation").getValue(String.class);
myExpense = dataSnapshot.child("expense").getValue(String.class);
myPreplan = dataSnapshot.child("preplan").getValue(String.class);
mySpending = dataSnapshot.child("spending").getValue(String.class);
myFlight = dataSnapshot.child("flight").getValue(String.class);
myGuide = dataSnapshot.child("guide").getValue(String.class);
mySmoking = dataSnapshot.child("smoking").getValue(String.class);
String myPBMS = myAccommondation+myMeal+mySTransportation+myLTransportation+myExpense+myPreplan+mySpending+myFlight+myGuide+mySmoking;
Log.d(TAG, "my PBMS★ "+myPBMS);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
// Compare to my PBMS and other user
private void getMatching() {
if (accommondation.matches(myAccommondation)) {
score = score + 1;
}
if (meal.matches(myMeal)) {
score = score + 1;
}
if (sTransportation.matches(mySTransportation)) {
score = score + 1;
}
if (lTransportation.matches(myLTransportation)) {
score = score + 1;
}
if (expense.matches(myExpense)) {
score = score + 1;
}
if (preplan.matches(myPreplan)) {
score = score + 1;
}
if (spending.matches(mySpending)) {
score = score + 1;
}
if (flight.matches(myFlight)) {
score = score + 1;
}
if (guide.matches(myGuide)) {
score = score + 1;
}
if (smoking.matches(mySmoking)) {
score = score + 1;
}
}
// 전체 데이터 갯수 리턴
@Override
public int getItemCount() {
try {
int size = UserList.size();
return size;
} catch(NullPointerException ex) {
return 0;
}
}
}
Это то, что я хочу получить ...
-------------------
| user1 [ 100 ]|
| ABCC [ % ]|
# Actually I want to exclude my name in recycler view..haha
-------------------
| user2 [ 50 ]|
| ACCB [ % ]|
-------------------
Какя могу решить эту проблему? Спасибо за чтение.