在列表项内的多个数组中排序其中一个数组

时间:2017-09-20 08:22:04

标签: java sorting arraylist custom-adapter

我有一个自定义列表视图,其中包含3个textview for Name,Unique id和Number of Absent以及1个用于删除每个列表项的图像按钮。我试图对缺席的数量进行排序,但排序却有所不同。我在互联网上看到比较器,我认为这是我的问题的解决方案,但我不知道如何使用它。希望有人在这里帮助我。

这就是我在3 ArrayList中添加值的方法(mUid表示唯一ID,MFullname表示名称,mStatus表示缺少数量)

pagod = FirebaseDatabase.getInstance().getReference().child("users").child("teacher").child(user.getUid().toString().trim()).child("class").child(aba).child(message).child("Listofstudents");
        pagod.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    String userid = child.child("userid").getValue().toString().trim();
                    String name = child.child("name").getValue().toString().trim();
                    mUid.add(userid);
                    mFullname.add(name);
                    //studentlist.invalidateViews();
                    //adapter.notifyDataSetChanged();
                    if (dataSnapshot.child(userid).child("attendance").exists()) {

                        pagodnapagod = FirebaseDatabase.getInstance().getReference();
                        Query query = pagodnapagod.child("users").child("teacher").child(user.getUid().toString().trim()).child("class").child(aba).child(message).child("Listofstudents").child(userid).child("attendance").child("finals").orderByChild("status").equalTo("absent");
                        query.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    String numberOfStudent ="Number of Absent: "+dataSnapshot.getChildrenCount();
                                    //Toast.makeText(MyClassesActivity.this, numberOfStudent, Toast.LENGTH_SHORT).show();
                                    mStatus.add(numberOfStudent);
                                    //Collections.sort(mStatus, Collections.reverseOrder());
                                    adapters.notifyDataSetChanged();
                                    progressDialog.dismiss();
                                } else {
                                    String numberOfStudent = "Number of Absent: 0";
                                    mStatus.add(numberOfStudent);
                                    //Collections.sort(mStatus, Collections.reverseOrder());
                                    adapters.notifyDataSetChanged();
                                    progressDialog.dismiss();
                                }
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                    } else {
                        String numberOfStudent = "Number of Absent: 0";
                        mStatus.add(numberOfStudent);
                        //Collections.sort(mStatus, Collections.reverseOrder());
                        adapters.notifyDataSetChanged();
                        progressDialog.dismiss();
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

这是我的自定义适配器。

class Adapters extends BaseAdapter implements Filterable {

    private Context context;
    private int resource;
    private int ids;
    private ArrayList arrayList;
    private List<String> originalData=null;
    private List<String>filteredData=null;
    private List<String> userId;
    private List<String> status;
    private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    private Adapters.ItemFilter mFilter = new Adapters.ItemFilter();
    private LayoutInflater inflater;

    public Adapters(Context context, int resource, int id, ArrayList arrayList, ArrayList userId, ArrayList status){
        this.context = context;
        this.resource = resource;
        this.ids = id;
        this.arrayList = arrayList;
        this.filteredData = arrayList ;
        this.originalData = arrayList ;
        this.userId = userId;
        this.status = status;
        inflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return filteredData.size();
    }

    public Object getItem(int position) {
        return filteredData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    // Hold views of the ListView to improve its scrolling performance
    private class ViewHolder {
        public TextView type;
        public TextView uid;
        public TextView count;
        public ImageButton removeButton;

    }

    public View getView(final int position, final View convertView, ViewGroup parent) {

        View rowView = convertView;
        // Inflate the list_item.xml file if convertView is null
        if(rowView==null){
            rowView = inflater.inflate(R.layout.listitem6, null);
            rowView= inflater.inflate(resource, parent, false);
            Adapters.ViewHolder viewHolder = new Adapters.ViewHolder();
            viewHolder.type= (TextView) rowView.findViewById(R.id.txt);
            viewHolder.uid= (TextView) rowView.findViewById(R.id.id);
            viewHolder.count= (TextView) rowView.findViewById(R.id.count);
            viewHolder.removeButton= (ImageButton) rowView.findViewById(btn_del);
            rowView.setTag(viewHolder);

        }

        final String x = (String) arrayList.get(position);
        final String y = arrayList.get(position).toString().trim();
        // Set text to each TextView of ListView item
        Adapters.ViewHolder holder = (Adapters.ViewHolder) rowView.getTag();
        holder.type.setText(x);
        holder.uid.setText(x);
        holder.count.setText(x);
        holder.removeButton.setBackgroundResource(R.drawable.deletes);
        holder.removeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                View parentRow = (View) view.getParent();
                ListView listView = (ListView) parentRow.getParent();
                final int position = listView.getPositionForView(parentRow);

                TextView userId=(TextView) parentRow.findViewById(R.id.id);
                TextView userName=(TextView) parentRow.findViewById(R.id.txt);
                final String name = userName.getText().toString().trim();
                final String uid = userId.getText().toString().trim();
                //Toast.makeText(StudentListActivity.this, name +"\n" + uid, Toast.LENGTH_SHORT).show();

                final AlertDialog.Builder builder2=new AlertDialog.Builder(context);
                final View dialogView = inflater.inflate(R.layout.customdialog, null);
                builder2.setView(dialogView);
                final EditText input = (EditText) dialogView.findViewById(R.id.edit1);
                final TextInputLayout inputs = (TextInputLayout) dialogView.findViewById(R.id.text_field);

                TextWatcher watcher = new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        inputs.setErrorEnabled(false);
                        inputs.setError(null);
                    }
                };

                input.addTextChangedListener(watcher);

                builder2.setTitle("Delete Class");
                builder2.setMessage("Do you want to delete "+y+" from this class?");
                builder2.setPositiveButton("ok", null);
                builder2.setNegativeButton("cancel", null);
                builder2.setIcon(R.drawable.deletes);
                builder2.setCancelable(false);
                final AlertDialog mAlertDialog = builder2.create();
                mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(final DialogInterface dialog) {
                        Button a = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                        a.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                // TODO Do something
                                if (input.getText().toString().equalsIgnoreCase("delete")) {
                                    arrayList.remove(position);
                                    notifyDataSetChanged();
                                    Toast.makeText(view.getContext(), y + " has been deleted in this class", Toast.LENGTH_SHORT).show();

                                    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("users").child("teacher").child(user.getUid().toString().trim()).child("class").child(aba).child(message).child("Listofstudents").child(uid);
                                    mDatabase.setValue(null);

                                    ref0 = FirebaseDatabase.getInstance().getReference();
                                    ref0.child("usersclass").orderByChild("userid_classid").equalTo(uid+"-"+message).addListenerForSingleValueEvent(new ValueEventListener() {

                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            for (DataSnapshot child: dataSnapshot.getChildren()) {
                                                String hello = child.getKey();
                                                ref0.child("usersclass").child(hello).setValue(null);
                                            }
                                        }
                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });

                                    dialog.dismiss();
                                    notifyDataSetChanged();
                                    restartThis();
                                } else {
                                    inputs.setError("Enter word DELETE");
                                }
                            }
                        });

                        Button b = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
                        b.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                // TODO Do something
                                dialog.dismiss();
                            }
                        });
                    }
                });

                mAlertDialog.show();

            }
        });
        holder.type.setText(filteredData.get(position));
        holder.uid.setText(userId.get(position));
        holder.count.setText(status.get(position));

        return rowView;
    }

    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<String> list = originalData;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }

}

0 个答案:

没有答案