英文:
Why does transfering data from one Activity to another doesnt work inside a query in firebasefirestore
问题
我正在尝试传递用户选择的经过筛选的芯片的值,以便根据特定条件仅检索他所需的商店。
现在我已经创建了所有内容,并且在完成 FilterActivity 后获得的数据被传递到了 HomeActivity。但是在我的查询中,这些数据没有被“读取”或“接受”,因为它们没有产生正确的输出。当我在查询中使用静态值时,它可以工作。现在我需要它成为一个根据用户选择的内容而变化的值。
这是在打开 Filter Activity 的 HomeActivity 中:
filterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this,FilterActivity.class);
startActivityForResult(intent,101);
}
});
这是我的 FilterActivity,其中包含一些示例:
private Chip rate2,rate3,rate4;
private ArrayList<String> selectedChipData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
rate2 = findViewById(R.id.chip_Rate_2);
selectedChipData = new ArrayList<>();
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
selectedChipData.add(compoundButton.getText().toString().trim());
}else{
selectedChipData.remove(compoundButton.getText().toString().trim());
}
}
};
rate2.setOnCheckedChangeListener(checkedChangeListener);
}
这是用户在想要应用筛选选项之一时单击的按钮:
filter_reset = findViewById(R.id.filter_reset);
filter_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent();
resultIntent.putExtra("data",selectedChipData.toString().trim());
setResult(101,resultIntent);
finish();
}
});
现在在我的 HomeActivity 中,我创建了 onActivityResult:
String Data;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101){
Data = data.getStringExtra("data");
Log.d("TAG", data.getStringExtra("data"));
Query q = firebaseFirestore.collection("Shops").whereEqualTo("location",Data);
}
}
Query q = firebaseFirestore.collection("Shops").whereEqualTo("location",Data);
// 这是我的查询,以及我如何尝试将匹配的用户选择的芯片传递给商店表的位置。
在我的查询中我漏掉了对 q
进行实际操作,你可以添加下面的代码来执行查询:
q.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
// 处理查询结果
}
} else {
Log.d("TAG", "Error getting documents: ", task.getException());
}
}
});
这样可以在查询完成时获取结果并进行处理。希望这对你有所帮助。
英文:
i am trying to pass values of filtered chips that a user has selected in order to retrieve only the required shops he want in that particular criteria.
now i created everything and the data i am getting after finishing from my FilterActivity is being passed into my HomeActivity. but this data is not being 'read' or 'accepted' inside my query since it doesn't produce the correct output. When i use a static value inside the query it would work. now i need it to be a changing value depending on what the use has selected.
This is inside my HomeActivity that opens the Filter Activity:
filterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this,FilterActivity.class);
startActivityForResult(intent,101);
}
});
this is my FilterActivity with a few examples:
private Chip rate2,rate3,rate4;
private ArrayList<String> selectedChipData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
rate2 = findViewById(R.id.chip_Rate_2);
selectedChipData = new ArrayList<>();
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
selectedChipData.add(compoundButton.getText().toString().trim());
}else{
selectedChipData.remove(compoundButton.getText().toString().trim());
}
}
};
rate2.setOnCheckedChangeListener(checkedChangeListener);
}
and this is the button the user clicks when he wants to apply one of the filter options:
filter_reset = findViewById(R.id.filter_reset);
filter_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent();
resultIntent.putExtra("data",selectedChipData.toString().trim());
setResult(101,resultIntent);
finish();
}
});
now in my HomeActivity i created the onActivityResult:
String Data;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101){
Data = data.getStringExtra("data");
Log.d("TAG", data.getStringExtra("data"));
Query q = firebaseFirestore.collection("Shops").whereEqualTo("location",Data);
}
}
Query q = firebaseFirestore.collection("Shops").whereEqualTo("location",Data);
// this is my query and how i am trying to pass the matched users selected chip to the location of the Shops table.
What am i missing or doing wrong inside my query and how to fix it? can someone advise?
答案1
得分: 1
你是否碰巧忘记告诉意图返回首页?
filter_reset = findViewById(R.id.filter_reset);
filter_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent(FilterActivity.this, MainActivity.class);
// 已更改上述意图参数
resultIntent.putExtra("data", selectedChipData.toString().trim());
setResult(101, resultIntent);
finish();
}
});
<details>
<summary>英文:</summary>
Did by any chance you forget to tell intent to go back to Home Page
filter_reset = findViewById(R.id.filter_reset);
filter_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent(FilterActivity.this, MainActivity.class);
// Changed the above Intent Paras
resultIntent.putExtra("data",selectedChipData.toString().trim());
setResult(101,resultIntent);
finish();
}
});
</details>
# 答案2
**得分**: 1
我找到问题了,它与我的数据库不匹配,因为这个值是一个数组,并且在结果中带有[]。如果我在我的数据库中加入[],就可以正常工作。
<details>
<summary>英文:</summary>
i found the problem, it wasn't matching with my database because the value is an array and has [] in the result. if i put the [] in my database it works fine.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论