3

I mean how to make the adapter work not with the model class, but with the already created ArrayList (randomPlaceList)? I take 20 records from my Firestore and put 3 random of them to the ArrayList(randomPlaceList). And now, I want that my adapter connect ViewHolder object not with the model class(Places.class), but with this early created ArrayList(randomPlaceList), where I have already 3 random records..

RecycleView class:

    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();
        }
    }
}

And that model class, that used in OnCreate method, for query, but I need to use in Adapter, randomPlaceList to take 3 records, that already put earlier in it. In other words, use 2 model class, if I can say so..:

My Model class:

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; }   
}

EDITED WITH 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);
                    }
                }
            });
        }
    }
}

Error:

error: constructor MyListAdapter in class MyListAdapter cannot be applied to given types; required: Context,int,List found: >,List reason: actual and formal argument lists differ in length

Spike Johnson
  • 365
  • 4
  • 12
  • If you are trying to use that list in order to display only those 3 elements, I recommend you to use a ListView. Will this work for you? – Alex Mamo May 07 '18 at 07:51
  • Later@AlexMamo, you helped me with creating `RecycleView` more than 4 hours, and now refuse all this? I just prepared the design for `RecycleView` and worked only with it. And this is the only thing that has remained in my project, everything else is ready. Do you have any idea how to do this? Does my logic in this question is the same as yours in solving this problem? – Spike Johnson May 07 '18 at 08:11
  • The logic is the same but using a `ListView` and an `ArrayAdapter` where you can pass that list as an argument in order to display only those 3 items. Take a look [here](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview). – Alex Mamo May 07 '18 at 08:14
  • Maybe there is no difference in principle@Alex Mamo, listview it will be or recycleview, it is important for me that it would perform the same functions and look exactly like the recycle view. Just before, I imagined the solution to this problem and the result displayed only in recycleview.. – Spike Johnson May 07 '18 at 08:15
  • Have you tried to implement my answer from that post? – Alex Mamo May 07 '18 at 08:25
  • I'm in process@AlexMamo.. – Spike Johnson May 07 '18 at 08:26
  • Ok, keep me posted. – Alex Mamo May 07 '18 at 08:41
  • I'm edit question@AlexMamo, but I'm completely confused. – Spike Johnson May 07 '18 at 10:47
  • Edit again@AlexMamo – Spike Johnson May 07 '18 at 10:52
  • You didn't follow all steps. You didn't set the adapter. – Alex Mamo May 07 '18 at 10:54
  • Edited @AlexMamo, but null object reference. – Spike Johnson May 07 '18 at 11:01
  • `mListView.setAdapter(myListAdapter);` get it outside the 4 loop. – Alex Mamo May 07 '18 at 11:03
  • I get it out of `for` loop, outside `onComlete`, outside `AddOnCompleteListener` - nothing@AlexMamo – Spike Johnson May 07 '18 at 11:10
  • In this case, just take that example, starting from zero to see how it works. – Alex Mamo May 07 '18 at 11:12
  • https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview This example? But how it work on me? You peace of code for take random 3 records works, in the example, the same piece of code, but with diferent task, and nothing else. I put `.setAdapter` on `.addOnCompleteListener` there are no errors, but ListView empty@AlexMamo – Spike Johnson May 07 '18 at 11:28
  • @AlexMamo I edited the question. Could are you look at that? – Spike Johnson May 07 '18 at 22:59

0 Answers0