过滤后两个数组的错误位置

时间:2017-09-09 09:35:12

标签: android android-recyclerview

我创建了一个从Firebase获取数据的Recyclerview。我有两个数组发送到适配器以在项目中使用。我已经实现了一个搜索过滤器。这适用于第一个数组,但第二个数组始终处于起始位置。如何将第二个数组放在与第一个过滤器相同的位置?谢谢

MainActivity

public class FriendActivity3 extends AppCompatActivity
    //implements FriendHoldAdapter3.ItemClickListener{

FriendHoldAdapter3 adapter;
RecyclerView recyclerView;
ArrayList<FriendItem> username;
ArrayList<String> uid, url;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friend2);

    SearchView search = (SearchView) findViewById(R.id.ciao);

     recyclerView = (RecyclerView) findViewById(R.id.friend_list2);
    //recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

    final DatabaseReference mPostReference = FirebaseDatabase.getInstance().getReference().child("user-profile");
    mPostReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {



            username = new ArrayList<>();
            uid = new ArrayList<String>();
            url = new ArrayList<String>();

            for (DataSnapshot data : dataSnapshot.getChildren()){
                final FriendItem friendItem = data.getValue(FriendItem.class);
                final String friendItem2 = data.getKey();



                username.add(friendItem);
                uid.add(friendItem2);

                adapter = new FriendHoldAdapter3(getApplicationContext(), username, uid, url);
                //adapter.setClickListener(FriendActivity3.this);
                recyclerView.setAdapter(adapter);

                /**
                adapter = new FriendHoldAdapter2(FriendActivity2.this, username, uid);
                adapter.setClickListener(FriendActivity2.this);
                recyclerView.setAdapter(adapter);
                 **/
                final List<FriendItem> filteredModelList = filter(username, "        ");
                adapter.setFilter(filteredModelList);


            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }});


    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText.equals("")){
                newText = "             ";
            }
            if (newText.equals("View All")){
                newText = "";
            }
            final List<FriendItem> filteredModelList = filter(username, newText);
            adapter.setFilter(filteredModelList);
            return true;
        }

    });
private List<FriendItem> filter(List<FriendItem> models, String query) {

    query = query.toLowerCase();final List<FriendItem> filteredModelList = new ArrayList<>();
    for (FriendItem model : models) {
        final String text = model.username.toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}}

适配器

public class FriendHoldAdapter3 extends RecyclerView.Adapter<FriendHoldAdapter3.ViewHolder> {

private List<FriendItem> mData_username = Collections.emptyList();
private List<String> mData_uid = Collections.emptyList();
private List<String> mData_url = Collections.emptyList();

private LayoutInflater mInflater;
private ItemClickListener mClickListener;




// data is passed into the constructor
public FriendHoldAdapter3(Context context, List<FriendItem> data_username, List<String> data_uid, List<String> data_url) {
    this.mInflater = LayoutInflater.from(context);
    this.mData_username = data_username;
    this.mData_uid = data_uid;
    this.mData_url = data_url;


}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.friend_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);


    return viewHolder;
}

// binds the data to the textview in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    FriendItem username = mData_username.get(position);
    String uid = mData_uid.get(position);
    holder.myTextView.setText(username.username);
    holder.uid_txt.setText(uid);

}

// total number of rows
@Override
public int getItemCount() {
    return mData_username.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView myTextView, uid_txt;

    public ViewHolder(final View itemView) {
        super(itemView);
        myTextView = (TextView) itemView.findViewById(R.id.friend_item_name);
        uid_txt = (TextView) itemView.findViewById(R.id.friend_item_uid);


        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(itemView.getContext(),mData_uid.get(getAdapterPosition()), Toast.LENGTH_SHORT).show();

                if (uid_txt.getText().toString()!=null | !uid_txt.getText().toString().equals("") ){
                    Intent intent = new Intent(itemView.getContext(), AccountPubblic.class);

                    //MANDARE TESTO A AccountPubblic
                    intent.putExtra(AccountPubblic.EXTRA_UID, uid_txt.getText().toString());

                    itemView.getContext().startActivity(intent);
                }


            }
        });


    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
    }
}

// convenience method for getting data at click position
public FriendItem getItem(int id) {
    return mData_username.get(id);
}

@Override
public long getItemId(int position) {

    return super.getItemId(position);
}

// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
    this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
    void onItemClick(View view, int position);
}


public void setFilter(List<FriendItem> countryModels) {
    mData_username = new ArrayList<>();
    mData_username.addAll(countryModels);
    notifyDataSetChanged();
}}

0 个答案:

没有答案