Home > database >  DatabaseReference for each listview item
DatabaseReference for each listview item

Time:04-30

so i am working on groupchats for my app, and im trying to display the last message sent. i have got the listview through a fragment and thats why this custom view is made in a separate class. it works good besides for the messaging part. and what is wrong is that the list view only updates the last item on the listview

public class GroupList extends ArrayAdapter<HashMap<String,Object>> {
    private ArrayList<HashMap<String,Object>> lop;
    private Context context;
    private int resource;
    private View view;
    private Holder holder;
    private HashMap<String,Object> hashMap;
    private SharedPreferences UserData;


    public GroupList(Context context, int resource, ArrayList<HashMap<String, Object>> objects)
    {

        super(context, resource, objects);
        this.context=context;
        this.resource=resource;
        this.lop=objects;
        UserData = PreferenceManager.getDefaultSharedPreferences(this.context);
    }




    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
// this is where it gets the reference for the group 
        DatabaseReference  GropDataFinder = FirebaseDatabase.getInstance().getReference().child("Groups").child(lop.get(position).get("name").toString());


//this limits the data to the last mesaage         GropDataFinder.orderByKey().limitToLast(1).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                // tbis switch statment helps the user find out what message it was
                switch (snapshot.child("type").getValue(String.class)) {
                    case "system":
                        String US = "SYSTEM: "   snapshot.child("messages").getValue(String.class);
                        Toast.makeText(context, "system message", Toast.LENGTH_SHORT).show();
                        holder.GroupMessage.setText(US);
                        break;
                    case "user":
                        if (UserData.getString("username", "frugalnormal").equals(snapshot.child("username").getValue(String.class))){
                            String YOU = "YOU: "   snapshot.child("messages").getValue(String.class);
                            holder.GroupMessage.setText(YOU);
                            Toast.makeText(context, "my message", Toast.LENGTH_SHORT).show();
                        }else{
                            String YOU = snapshot.child("username").getValue(String.class) ":"   snapshot.child("messages").getValue(String.class);
                            holder.GroupMessage.setText(YOU);
                            Toast.makeText(context, "person message", Toast.LENGTH_SHORT).show();
                        }

                        break;
                    default:
                        holder.GroupMessage.setText("Somethings broken!");
                        Toast.makeText(context, "broken message", Toast.LENGTH_SHORT).show();
                }


                //for some reason it only works on the last item in my list view to show the group and last message 
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=inflater.inflate(resource, parent,false);

        holder=new Holder();
        holder.GroupName=(TextView)view.findViewById(R.id.Words);
        holder.MainView = (LinearLayout)view.findViewById(R.id.MainView);
        holder.GroupMessage = (TextView)view.findViewById(R.id.GMessages);




        holder.MainView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String currentGroupName = lop.get(position).get("name").toString();

                Intent groupChatIntent = new Intent(getContext(), GroupChatActiviy.class);
                groupChatIntent.putExtra("groupMame", currentGroupName);
                context.startActivity(groupChatIntent);
            }
        });
       // holder.LastText=(TextView)view.findViewById(R.id.text_calllog_date);
        //holder.GroupImageView=(CircleImageView) view.findViewById(R.id.text_calllog_time);

        hashMap=lop.get(position);
        //Toast.makeText(context, hashMap.get("name").toString(), Toast.LENGTH_SHORT).show();

        holder.GroupName.setText(lop.get(position).get("name").toString());


        return view;
    }

    public class Holder
    {
        TextView GroupName;
        TextView GroupMessage;
        LinearLayout MainView;

        //TextView LastText;
       // CircleImageView GroupImageView;
    }

}

it gets the last message for only one item and that item is the last one. so here is the image of my database for reference.

And if I'm missing anything please just tell me, I dont know if I am putting it right

CodePudding user response:

I posted a new reformed question here...

This one is more detailed and explains my problem better and more specifically

  • Related