如何使FirestoreRecyclerAdapter不与模型类一起工作?

时间:2018-05-07 04:20:49

标签: java android firebase android-recyclerview google-cloud-firestore

我的意思是如何让adapter不是使用model类,而是使用已创建的ArrayListrandomPlaceList)?我从我的Firestore中取出了20条记录,并将其中的3条随机放入ArrayListrandomPlaceList)。现在,我希望我的适配器连接ViewHolder对象而不是model类(Places.class),但是这个早期创建的ArrayListrandomPlaceList),我已经有3个随机记录..

RecycleView类:

    public class Myactivity extends AppCompatActivity {

    public RecyclerView mResultList;
    public FirebaseFirestore mFirestore;
    public com.google.firebase.firestore.Query query;
    public FirestoreRecyclerAdapter<Places, PlaceViewHolder> firestoreRecyclerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_activity);
        mResultList = findViewById(R.id.list_result);

        Boolean l_check1 = getIntent().getExtras().getBoolean("1");
        Boolean l_check2 = getIntent().getExtras().getBoolean("2");
        Boolean l_check3 = getIntent().getExtras().getBoolean("3");
        mFirestore = FirebaseFirestore.getInstance();  
        if (l_check1) {
            query = mFirestore.collection("Places").whereEqualTo("colour", "1").limit(20);

            //QUESTION START HERE   

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    List<Places> placesList = new ArrayList<>();
                    for (DocumentSnapshot document : task.getResult()) {
                        Places place = document.toObject(Places.class);
                        placesList.add(place);
                    }
                    int placeCount = placesList.size();
                    int randomNumber = new Random().nextInt(placeCount);

                    //THIS ARRAYLIST I WANT TO USE INSTEAD THE PLACES.CLASS

                    List<Places> randomPlaceList = new ArrayList<>();
                    for (int i=1; i<=3; i++) {
                        randomPlaceList.add(placesList.get(randomNumber));
                    }
                }
            }
        });
        } else if (l_check2) {
            query = mFirestore.collection("Places").whereEqualTo("colour", "2").limit(3);
        } else if (l_check3) {
            query = mFirestore.collection("Places").whereEqualTo("colour", "3").limit(3);
        }
        mResultList.setLayoutManager(new LinearLayoutManager(this));
        FirestoreRecyclerOptions<Places> options = new FirestoreRecyclerOptions.Builder<Places>()
                .setQuery(query, Places.class)
                .build();
        firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<Places, PlaceViewHolder>(options) {
            @Override
            protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {

                holder.setDetails(getApplicationContext(), model.getName(), model.getImage());
            }

            @Override
            public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_layout, parent, false);
                return new PlaceViewHolder(view);
            }

        };
        mResultList.setAdapter(firestoreRecyclerAdapter);
    }
    @Override
    protected void onStart() {
        super.onStart();
        firestoreRecyclerAdapter.startListening();
    }
    class PlaceViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public PlaceViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setDetails(Context context, String placeName, String placeImage) {

            final TextView place_Name = mView.findViewById(R.id.text_image_id);
            ImageView place_Image = mView.findViewById(R.id.image_id);

            place_Name.setText(placeName);
            Glide.with(context).load(placeImage).into(place_Image);

            place_Name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), Card_activity.class);
                    intent.putExtra("qwerty", place_Name.getText());
                    startActivity(intent);
                }
            });
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        if (firestoreRecyclerAdapter != null) {
            firestoreRecyclerAdapter.stopListening();
        }
    }
}

model方法中使用的OnCreate类,用于query,但我需要在Adapter中使用randomPlaceList来获取3条记录,已经提到了它。换句话说,使用2个模型类,如果我可以这样说..:

我的模特课:

public class Places {
private String image, name;
public Places() { }

public Places(String image, String name) {
    this.image = image;
    this.name = name; 
}
public String getImage() { return image; }
public String getName() { return name; }   
}

使用LISTVIEW编辑

MyListAdaper:

    class MyListAdapter extends ArrayAdapter {


    public MyListAdapter(Context context, int resource,List<Places> randomPLaceList) {
        super(context, 0, randomPLaceList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_list, parent, false);
        }
        TextView place_Name = convertView.findViewById(R.id.text_image_id);
        ImageView place_Image = convertView.findViewById(R.id.image_id);

        Places places = (Places) getItem(position);

        place_Name.setText(places.getName());
        Glide.with(getContext()).load(places.getImage()).into(place_Image);

        return convertView;
    }
}

ListViewPlaces.class:

    public class ListViewPlaces extends AppCompatActivity {

    public ListView mListView;
    public MyListAdapter myListAdapter;
    public FirebaseFirestore mFirestore;
    public Query query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_places);

        Boolean l_check1 = getIntent().getExtras().getBoolean("1");

        mFirestore = FirebaseFirestore.getInstance();

        if (l_check1) {
            query = mFirestore.collection("Places").whereEqualTo("meet", "1").whereEqualTo("cash", "1").limit(7);
            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        List<Places> placesList = new ArrayList<>();
                        for (DocumentSnapshot document : task.getResult()) {
                            Places place = document.toObject(Places.class);
                            placesList.add(place);
                        }
                        //mListView = findViewById(R.id.place_list);
                        //mListView.setAdapter(myListAdapter);
                        final int placeCount = placesList.size();
                        final Random randomGenerator = new Random();
                        List<Places> randomPlaceList = new ArrayList<>();
                        for (int i = 1; i <= 3; i++) {
                            randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
                        }
                        ListView mListView = (ListView) findViewById(R.id.place_list);
                        MyListAdapter mListAdapter = new MyListAdapter(this, randomPlaceList);
                        mListView.setAdapter(mListAdapter);
                    }
                }
            });
        }
    }
}

错误:

  

错误:类MyListAdapter中的构造函数MyListAdapter不能   适用于给定类型; required:Context,int,找到的列表:   &gt ;,列出原因:   实际和正式的参数列表长度不同

0 个答案:

没有答案