在工具栏searchview中过滤时适配器空指针

时间:2016-07-15 18:08:30

标签: android listview filter android-recyclerview searchview

所以我试图按照此http://www.tutorialsbuzz.com/2015/11/Android-Filter-RecyclerView-Using-SearchView-In-ToolBar.html

中所示过滤我的列表

现在,当我点击搜索图标进行星型输入以进行过滤时,我会在

上获得空指针异常

adapter.setFilter(filteredModelList);

活动

public class CreateMyTeamActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

List<MessageData> messageDataList = new ArrayList<MessageData>();

private RecyclerView mRecyclerView;
private CustomRecyclerAdapterCreateTeam adapter;


ListView usersListView;
Context context;

private static final String TAG = "MyActivity";

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

    context = this;

    mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);

    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);




    MessageData msg1 = new MessageData();
    msg1.setName("Anant Kharod");
    msg1.setImage(R.drawable.msgone);

    MessageData msg2 = new MessageData();
    msg2.setName("Adil");
    msg2.setImage(R.drawable.msgthree);

    messageDataList.add(msg1);
    messageDataList.add(msg2);

    Log.i(TAG, "Message: " + msg1);

    //arrMessageData.add(msg1);

    CustomRecyclerAdapterCreateTeam adapter = new CustomRecyclerAdapterCreateTeam(this,messageDataList);
    mRecyclerView.setAdapter(adapter);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_team, menu);

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

    MenuItemCompat.setOnActionExpandListener(item,
            new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // Do something when collapsed


                     ///HERE IS THE PROBLEM
                    adapter.setFilter(messageDataList);




                    return true; // Return true to collapse action view
                }

                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    // Do something when expanded
                    return true; // Return true to expand action view
                }
            });
    return super.onCreateOptionsMenu(menu);


}

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

@Override
public boolean onQueryTextChange(String newText) {
    final List<MessageData> filteredModelList = filter(messageDataList, newText);
    adapter.setFilter(filteredModelList);
    return true;
}

private List<MessageData> filter(List<MessageData> models, String query) {
    query = query.toLowerCase();

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

适配器:

public class CustomRecyclerAdapterCreateTeam extends RecyclerView.Adapter<ListViewRowHolder>{


private List<MessageData> messageDataList;
private Context context;

public CustomRecyclerAdapterCreateTeam(Context context, List<MessageData> messageDataList){
    this.messageDataList = messageDataList;
    this.context = context;
}


@Override
public ListViewRowHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.create_team_list_item, null);

    ListViewRowHolder holder = new ListViewRowHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(ListViewRowHolder holder, int position) {

    MessageData messageData = messageDataList.get(position);

    holder.person.setText(messageData.getName());
    holder.pic.setImageResource(messageData.getImage());

}

@Override
public int getItemCount() {
    return messageDataList.size();
}

public void setFilter(List<MessageData> messageModels) {
    messageDataList = new ArrayList<>();
    messageDataList.addAll(messageModels);
    notifyDataSetChanged();
}

}

ListviewRowholder

public class ListViewRowHolder extends RecyclerView.ViewHolder {

public ImageView pic;
public TextView person;

public ListViewRowHolder(View itemView) {
    super(itemView);

    this.pic = (ImageView) itemView.findViewById(R.id.teamImage);
    this.person = (TextView) itemView.findViewById(R.id.teamName);
}

public void bind(MessageData messageModel) {
    this.pic.setImageResource(messageModel.getImage());
    this.person.setText(messageModel.getName());
}

}

模型

public class MessageData {


    String message = "";
    boolean isRead = false;
    int images;
    String name = "";
    String subject = "";
    String type = "";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean isRead) {
        this.isRead = isRead;
    }

    public void setImage (int images){
        this.images = images;
    }

    public int getImage(){
        return images;
    }

    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }

    public String getSubject(){
        return subject;
    }
    public void setSubject(String subject){
        this.subject = subject;
    }

    public String getType(){
        return type;
    }

    public void setType(String type){
        this.type = type;
    }

}

1 个答案:

答案 0 :(得分:1)

您已声明全局变量

private CustomRecyclerAdapterCreateTeam adapter;

但是你正在初始化适配器的局部变量:

CustomRecyclerAdapterCreateTeam adapter = new CustomRecyclerAdapterCreateTeam(this,messageDataList);

因此当您尝试访问全局adapter实例时,它会返回null

更改初始化,如下所示:

adapter = new CustomRecyclerAdapterCreateTeam(this,messageDataList);