android - 退格后搜索不会重新填充结果/列表

Posted

技术标签:

【中文标题】android - 退格后搜索不会重新填充结果/列表【英文标题】:android - Search isn't repopulating results / list after backspacing 【发布时间】:2016-09-02 13:27:01 【问题描述】:

我正在尝试使用 Searchview 功能: github.com/Wrdlbrnft/Searchable-RecyclerView-Demo(不能发布超过2个链接,抱歉)

.GIF of original

我已尝试将其添加到项目中。但是,当我这样做时,我的结果会被过滤,但不会在用户修改或退格时被添加回来。

.GIF of mine not functioning correctly

代码几乎相同。我没有对适配器进行任何更改。

CustomAdapter.java 公共类 CustomAdapter 扩展 RecyclerView.Adapter

// Dataset = list of players and their attributes
private final ArrayList<PlayerData> dataSet;
private final List<PlayerData> filteredList;
private ArrayList<PlayerData> originalItems;
public static class MyViewHolder extends RecyclerView.ViewHolder 

    TextView textViewName;
    TextView textViewPos;
    ImageView imageViewFace;
    ImageView imageTeamLogo;
    TextView textViewTeam;

    public MyViewHolder(View itemView) 
        super(itemView);
        this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
        this.textViewPos = (TextView) itemView.findViewById(R.id.textViewPos);
        this.imageViewFace = (ImageView) itemView.findViewById(R.id.imageViewFace);
        this.imageTeamLogo = (ImageView) itemView.findViewById(R.id.ImgTeamLogo);
        this.textViewTeam = (TextView) itemView.findViewById(R.id.textViewTeam);

    

//dataset = data from Main Activity
public CustomAdapter(ArrayList<PlayerData> data) 
    this.dataSet = data;
    this.filteredList = data;



@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) 
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cards_layout, parent, false);

    MyViewHolder myViewHolder = new MyViewHolder(view);

    return myViewHolder;


@Override
public void onBindViewHolder(final MyViewHolder holder, final int Position) 
    // called everytime user scrolls up or down
    TextView textViewName = holder.textViewName;
    TextView textViewPos = holder.textViewPos;
    ImageView imageViewFace = holder.imageViewFace;
    ImageView imageViewLogo = holder.imageTeamLogo;
    TextView textViewTeam = holder.textViewTeam;


    textViewName.setText(dataSet.get(Position).getName());

    textViewPos.setText(dataSet.get(Position).getPos());

    textViewTeam.setText(dataSet.get(Position).getTeam());




    Context context = imageViewFace.getContext();
    Context context2 = imageViewLogo.getContext();

    Picasso.with(context)
            .load(dataSet.get(Position).getFace())
            .placeholder(R.drawable.ic_launcher)
            .error(R.drawable.ic_3d_rotation)
            .into(holder.imageViewFace);

    Glide.with(context2).load(dataSet.get(Position).getTeamLogo()).into(holder.imageTeamLogo);


@Override
public int getItemCount() 
    return dataSet.size();

public void animateTo(ArrayList<PlayerData> players) 
    //checks what query found and adds / removes results
    // problem is, it is not adding back results
    applyAndAnimateRemovals(players);
    applyAndAnimateAdditions(players);
    applyAndAnimateMovedItems(players);



private void applyAndAnimateRemovals(ArrayList<PlayerData> newPlayers) 
    for (int i = dataSet.size() - 1; i >= 0; i--) 
        final PlayerData player = dataSet.get(i);
        if (!newPlayers.contains(player)) 
            removeItem(i);
        
    


private void applyAndAnimateAdditions(ArrayList<PlayerData> newPlayers) 
    for (int i = 0, count = newPlayers.size(); i < count; i++) 
        final PlayerData player = newPlayers.get(i);
        if (!dataSet.contains(player)) 
            addItem(i, player);
        
    



private void applyAndAnimateMovedItems(ArrayList<PlayerData> newPlayers) 
    for (int toPosition = newPlayers.size() - 1; toPosition >= 0; toPosition--) 
        final PlayerData player = newPlayers.get(toPosition);
        final int fromPosition = dataSet.indexOf(player);
        if (fromPosition >= 0 && fromPosition != toPosition) 
            moveItem(fromPosition, toPosition);
        
    


public PlayerData removeItem(int position) 
        final PlayerData player = dataSet.remove(position);
        notifyItemRemoved(position);

        return player;
    
    public void addItem(int position, PlayerData player) 
        dataSet.add(position, player);
        // this code is not working for whatever reason)
        notifyItemInserted(position);


    

    public void moveItem(int fromPosition, int toPosition) 
        final PlayerData player = dataSet.remove(fromPosition);
        dataSet.add(toPosition, player);
        notifyItemMoved(fromPosition, toPosition);
    

MainActivity.java

public class MainActivity extends AppCompatActivity  implements SearchView.OnQueryTextListener

private static RecyclerView recyclerView;
private static CustomAdapter adapter;
private static ArrayList<PlayerData> data;





private RecyclerView.LayoutManager layoutManager;

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


    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    data = new ArrayList<PlayerData>();


    addPlayers();
    adapter = new CustomAdapter(data);

    recyclerView.setAdapter(adapter);





public interface Constants 
    String LOG = "com.vogella.testapp";

private void addPlayers() 

    PlayerData player = new PlayerData("Chris Smith", "#", R.drawable.auser, R.drawable.aquestion, "First", 0);
    data.add(0,player);
    player = new PlayerData("Bobby Richars","#", R.drawable.auser, R.drawable.aquestion, "Second", 9);
            data.add(player);
    player = new PlayerData("Steven Williams", "#", R.drawable.auser, R.drawable.aquestion, "Third", 1);
    data.add(player);


    player = new PlayerData("Tony Lloyd", "#", R.drawable.auser, R.drawable.aquestion, "Fourth", 2);
    data.add(player);

    player = new PlayerData("Henry Gates", "#", R.drawable.auser,R.drawable.aquestion, "Fifth", 3);
    data.add(player);

    player = new PlayerData("Robert Marks", "#", R.drawable.auser, R.drawable.aquestion, "Sixth", 4);
    data.add(player);


    player = new PlayerData("Edward Jackson", "#", R.drawable.auser, R.drawable.aquestion, "SEVENTH", 5);
    data.add(player);

    player = new PlayerData("Zack Jordan", "#", R.drawable.auser, R.drawable.aquestion, "Eighth", 6);
    data.add(player);

    player = new PlayerData("Christopher Thomas", "#", R.drawable.auser, R.drawable.aquestion, "Ninth", 7);
    data.add(player);

    player = new PlayerData("Bruce Allen", "#", R.drawable.auser, R.drawable.aquestion, "Tenth", 8);
    data.add(player);

@Override
public boolean onCreateOptionsMenu(Menu menu) 
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_main, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
    return true;


@Override
public boolean onQueryTextChange(String query) 
    final ArrayList<PlayerData> filteredModelList = filter(data, query);
    adapter.animateTo(filteredModelList);
    recyclerView.scrollToPosition(0);
    return false;


@Override
public boolean onQueryTextSubmit(String query) 
    return false;


private ArrayList<PlayerData> filter(ArrayList<PlayerData> players, String query) 
    query = query.toLowerCase();

    final ArrayList<PlayerData> filteredModelList = new ArrayList<>();
    for (PlayerData player : players) 
        final String text = player.getName().toLowerCase() + player.getPos().toLowerCase()+ player.getTeam().toLowerCase();
        if (text.contains(query)) 
            filteredModelList.add(player);
        
    
    return filteredModelList;



@Override
public boolean onOptionsItemSelected(MenuItem item) 
    super.onOptionsItemSelected(item);
    return true;

【问题讨论】:

【参考方案1】:

原来这是一个小错误。

自定义适配器

public CustomAdapter(ArrayList<PlayerData> data) 
this.dataSet = data;
this.filteredList = data;

进入

public CustomAdapter(ArrayList<PlayerData> data) 
    this.dataSet = new ArrayList<>(data);

刚刚删除它,它的工作原理。

【讨论】:

以上是关于android - 退格后搜索不会重新填充结果/列表的主要内容,如果未能解决你的问题,请参考以下文章

在 TextBox.Text 中退格后 Regex.IsMatch 不起作用

删除单元格后刷新tableView

重新排序单元格后如何更改获取结果控制器数组中对象的顺序

根据输入中的每个字母过滤数组

EXCEL如何从单元格选中到它所在的整行

填充矩阵/数据帧的整合解决方案