如何从firebase中的autoComplete textview hashmap的选定项加载微调器

时间:2018-03-04 12:48:45

标签: java android firebase arraylist firebase-realtime-database

我制作了来自HashMap的钥匙(汽车品牌)的AutoCompleteTextView,我想将所选品牌(汽车模型)的值加载到微调器,微调器是最后一个项目的装载车型而不是选定的一个。

here is response

AutoCompleteTextView actBrand;
Spinner spinnerModel;
ArrayList<String> keyList = new ArrayList<>();
ArrayList<String> valueList = new ArrayList<>();
ArrayAdapter<String> arrayAdapterBrands;
ArrayAdapter<String> arrayAdapterModel;
ArrayList<HashMap<String, ArrayList>> mapArrayList;
ArrayList<HashMap<String, ArrayList>> mapValueArrayList;
String keys;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actBrand = (AutoCompleteTextView) findViewById(R.id.spinnerBrand);
    spinnerModel = (Spinner) findViewById(R.id.spinnerModel);

    getdata();
}

 public void getdata() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference myRef = database.getReference("carBrands");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated
            mapArrayList = (ArrayList<HashMap<String, ArrayList>>) dataSnapshot.getValue();


            //get data from Firebase 

            for (HashMap<String, ArrayList> hash : mapArrayList) {
                // For each hashmap, iterate over it
                for (Map.Entry<String, ArrayList> entry : hash.entrySet()) {

                    keys = entry.getKey();
                    keyList.add(keys);
                    hash.keySet();
                    valueList = entry.getValue();
                    valueList = hash.get(keys);

                }
            }
        }
         @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("tag", "Failed to read value.", error.toException());
        }
    });

    arrayAdapterBrands =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, keyList);
    actBrand.setAdapter(arrayAdapterBrands);

    actBrand.setOnItemClickListener(new AdapterView.OnItemClickListener() {

自动完成文本视图中的所选键项

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
            Object item = parent.getItemAtPosition(position);
            Log.v("getListSelection", String.valueOf(item));

这里我想根据所选键加载特定列表

            arrayAdapterModel =
                    new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, valueList);
            spinnerModel.setAdapter(arrayAdapterModel);

        }
    });
}

0 个答案:

没有答案