gpt4 book ai didi

android - 将 Firebase 数据库中的值加载到 Android 中的下拉 TextView 中

转载 作者:行者123 更新时间:2023-11-30 00:02:41 25 4
gpt4 key购买 nike

我需要帮助将辅助 Firebase 数据库中的数据实现到我的 Android 应用程序中的自动完成 TextView 中。我有一个包含汽车数据(汽车制造商、汽车型号、发动机尺寸)的数据库。

我附上了一张图片,显示了我的数据是如何在数据库中设置的。

数据库图片

enter image description here

我想要一个 TextView 只显示所有“品牌”属性,另一个 TextView 只显示“型号”属性,具体取决于用户选择的“汽车品牌”。 (例如,如果用户选择奥迪,则显示的车型将仅为 A4、A6、A3。如果用户选择大众,则显示的车型将仅为高尔夫、捷达、帕萨特)然后最后的 TextView 仅显示“引擎尺寸”这对应于所选的“品牌”和“型号”,因为每种型号的汽车都有不同的发动机尺寸。

任何帮助将不胜感激,因为我已经在我的应用程序的这个元素上停留了几个星期,并尝试了无数不同的方法。

下面是我的类和代码。该应用程序运行时没有错误,但不会从数据库中检索数据。

public class VehicleSpec extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Sets the layout according to the XML file
setContentView(R.layout.activity_vehicle_spec);

//FirebaseDatabase database = FirebaseDatabase.getInstance();

//
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("com.example.user.vel")
.setApiKey("AIzaSyBvBtL81H7aiUK90c3QfVccoU1CowKrmAA")
.setDatabaseUrl("https://finalyearproject-vel1-aac42.firebaseio.com/")
.build();

//
FirebaseApp.initializeApp(this, options, "secondary");

//
FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseDatabase database2 = FirebaseDatabase.getInstance(app);
DatabaseReference dbref = database2.getReference();
DatabaseReference dbref2 = database2.getReference();
DatabaseReference dbref3 = database2.getReference();

//
dbref.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();

for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
{
String suggestion = suggestionSnap.child("Make").getValue(String.class);
Cars.add(suggestion);

}//End For()

//XML TextView variable
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv.setAdapter(adapter);

}//End onDataChange()

public void onCancelled(DatabaseError databaseError)
{

}//End onCancelled()

});//End dbref ValueEventListener()

//
dbref2.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();

for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
{
String suggestion = suggestionSnap.child("Model").getValue(String.class);
Cars.add(suggestion);
}//End for()

//XML TextView variable
AutoCompleteTextView actv1 = (AutoCompleteTextView) findViewById(R.id.auto1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv1.setAdapter(adapter1);

}//End onDataChange()

public void onCancelled(DatabaseError databaseError)
{

}//End onCancelled()

});//End dbref2 ValueEventListener()

//
dbref3.child("Cars").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<String> Cars = new ArrayList<String>();

for (DataSnapshot suggestionSnap : dataSnapshot.getChildren())
{
String suggestion = suggestionSnap.child("Engine Size").getValue(String.class);
Cars.add(suggestion);
}//End for()

//XML TextView variable
AutoCompleteTextView actv2 = (AutoCompleteTextView) findViewById(R.id.auto2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
actv2.setAdapter(adapter2);

}//End onDataChange()

public void onCancelled(DatabaseError databaseError)
{

}//End onCancelled()

});//End dbref3 ValueEventListener()

}//End onCreate()

最佳答案

我不太明白你到底想达到什么目的,但我们假设你想要以下之一:

  • 显示汽车列表,列表中包含 3 个部分、制造商、型号和发动机尺寸下拉列表:我建议将 RecyclerView 与 2 个 TextViews(1 个用于 Make,第二个用于 Model)和一个 AppCompatSpinner 一起使用,该 AppCompatSpinner 将从引擎大小填充节点。您可以将这些库用作适配器:
  • FirebaseUI
  • Infinite-Fire我个人喜欢这个,因为当你到达最后一个项目时,它有一个内置的 loadMore。

  • 显示汽车品牌列表(在本例中为 Make),当用户选择一个项目时,会出现另一个包含型号和引擎尺寸的列表:我建议使用 RecyclerView 带有用于 Make 的 TextViews,当用户单击一个项目时,您将项目名称保存在 SharedPrefrances 中,并打开一个仅显示来自选择使用 Firebase databaseRef.child("Cars").orderByChild("Make").equalesTo(getSelectedMake());或者只是使用相同的方法将它们显示在 AlertDialog 中而不是服装布局...用于显示 DATA 您也可以使用上述库。

如果这不是您想要的,请详细说明您的问题,我会更新我的答案。

编辑 :使用SharedPreferences 来保存项目的引用是胡说八道,这是我对android 的经验不足的结果,相反你可以使用intent。 putExtra()

示例

//Start your activity like this
intent.putExtra("itemId", itemId)
startService(intent)

//On your new activity's onCreate() get the itemId as the following
String itemId = intent.getStringExtra("itemId")
Item item = firebaseRef.child("cars")....getValue(Item:class)

关于android - 将 Firebase 数据库中的值加载到 Android 中的下拉 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49628132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com