使用微调器填充RecyclerView

时间:2018-03-14 05:13:05

标签: android firebase android-recyclerview google-cloud-firestore recycler-adapter

我正在从Firebase的Firestore DB加载一个列表以填充微调器。这样可行。我想在微调器中的ItemSelected上填充RecyclerView。

我有这个代码来填充RecyclerView:

private void populateRecycler(final String RootCollection, final String UserID) {
    final String UserID = getIntent().getExtras().getString(UserIDKey);
    mQuery = mFirestore
            .collection(RootCollection)
            .whereEqualTo("UID", UserID)
            .orderBy("DateAdded", Query.Direction.DESCENDING);

    mAdapter = new OrdersAdapter(mQuery, HomeActivity.this);

    mOrdersRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mOrdersRecycler.setAdapter(mAdapter);
}

如果从onCreate放置或调用此代码,则此代码可以正常工作。但是我想稍后修改代码,以便稍后在spinner中填充String。 (可能只是为了添加另一个.whereEqualto)

当我从这里打电话给populateRecycler()时,它不起作用(我正在使用ButterKnife,fyi):

@OnItemSelected(R.id.storespinner)
public void spinnerItemSelected(Spinner spinner, int position) {
    String spinnerValue = mStoreSpinner.getItemAtPosition(position).toString();
    Toast.makeText(getBaseContext(), "Selected item " + spinnerValue, Toast.LENGTH_SHORT).show();
populateRecycler("test", UserID);
}

或者,如果我从另一个onSuccess侦听器(Firebase Firestore数据库调用)调用populateRecycler,它也不起作用:

    public void onSuccess(DocumentSnapshot documentSnapshot) {
                    UIDOrg uidOrg = documentSnapshot.toObject(UIDOrg.class);
                    mOrgTextView.setText(uidOrg.getOrg()); 
                    RootCollection = uidOrg.getRootCollection();
                    populateRecycler("test", UserID);

有人可以指出我正确的方向吗?

为什么populateRecycler仅在从onCreate调用时才有效?

编辑: 这就是我填充微调器数组的方式。

                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    UIDOrg uidOrg = documentSnapshot.toObject(UIDOrg.class);
                    mOrgTextView.setText(uidOrg.getOrg());
                    RootCollection = uidOrg.getRootCollection();

                    //Populate Spinner here
                    readData(new FirestoreCallBack() {
                        @Override
                        public void onCallback(List<String> list) {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            mStoreSpinner.setAdapter(adapter);
                        }
                    });
                    //End of Spinner
                }

在readData()中,它是:

                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                           spinnerArray.add(String.valueOf(document.getId()));
                        }
                        firestoreCallBack.onCallback(spinnerArray);
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }

3 个答案:

答案 0 :(得分:0)

首先请检查此方法,onItemSelected正常工作:

   spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         @Override
         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             Toast.makeText(MainActivity.this, "test"+ position, Toast.LENGTH_SHORT).show();

         }

         @Override
         public void onNothingSelected(AdapterView<?> parent) {

         }
     });

如果工作正常,则问题可能出在ButterKnife的声明中。看看我的例子它是否正常工作。 首先在Activity中声明微调器,如下所示:

  @InjectView(R.id.spinner)
    Spinner spinner;

然后在oncreate方法中:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        ArrayList<String> list = new ArrayList<>();
        list.add("test");
        list.add("test1");
        list.add("test2");
        list.add("test3");
        list.add("test4");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,list);
        spinner.setAdapter(adapter);

    }

然后在此方法之外我在OnItemSelected

中声明activity
   @OnItemSelected(R.id.spinner)
    public void spinnerItemSelected(Spinner spinner, int position) {
        Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
    }

试试这个并检查代码出了什么问题。

答案 1 :(得分:0)

如果你在下面代码中使用的微调器值选择时调用populateRecycler方法,我不提供黄油kinfe库中的代码,但提供简单的java代码,你将转换成黄油kinfe代码......

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String spinnerValue = spinner.getSelectedItem().toString();
            Toast.makeText(getBaseContext(), "Selected item " + spinnerValue, Toast.LENGTH_SHORT).show();
            populateRecycler("test", UserID);        
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

答案 2 :(得分:0)

我已经解决了。我想它毕竟是Firestore解决方案。

当从onCreate以外的其他地方调用populateRecycler时,我真的只需要这一行。

   mAdapter.setQuery(query);

Firestore已经动态更新了Recycler,我需要做的就是更新一个新查询,然后更新.setQuery(查询),以便更新Recycler视图。