filter recyclerview使用改进从mysql获取数据

时间:2018-04-08 07:32:50

标签: android android-recyclerview android-toolbar recycler-adapter android-search

我正在开发一个Android应用程序,我们在其中有一个搜索栏,如果我们在搜索栏中输入任何内容,它会根据用户的输入使用改造从过滤器获取数据并过滤recyclelerview。这是代码

Manage_Schedule.java(MainActivity) 在这个类中我们有搜索视图的功能,但我有一些错误 在每个循环中 for(String nameee: subjects)它给出错误,如果我们解决它,它将String类转换为模型类(主题:它包含你想要显示的内容的getter和setter)但是如果我们将它转​​换为 for (Subjects nameee: subjects)发生另一个错误,说namee不是String,所以我们不能使用toLowercase()并在其中包含方法 if (nameee.toLowerCase().contains(userInput)) { newList.add(nameee); }

这是代码

public class Manage_Schedule extends AppCompatActivity implements SearchView.OnQueryTextListener{
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclearAdapterSubjects adapter;
private List<Subjects> subjects;
private ApiInterface apiInterface;
AlertDialog dialog;
private Button viewSchedule;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule_subjects);
    recyclerView = (RecyclerView)findViewById(R.id.subjectsDetail);
    viewSchedule = (Button)findViewById(R.id.viewSubjectsSchedule);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    dialog= new SpotsDialog(this,R.style.Custom);
    dialog.show();
    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<List<Subjects>> call= apiInterface.getSubjects();

    call.enqueue(new Callback<List<Subjects>>() {
        @Override
        public void onResponse(Call<List<Subjects>> call, Response<List<Subjects>> response) {
            subjects = response.body();
            adapter =new RecyclearAdapterSubjects(subjects,getApplicationContext());
            recyclerView.setAdapter(adapter);
            dialog.dismiss();
        }

        @Override
        public void onFailure(Call<List<Subjects>> call, Throwable t) {
            dialog.dismiss();
        }
    });


    //View Schedule On Schedule button clicked
    viewSchedule.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        startActivity(new Intent(Manage_Schedule.this,scheduled_Subjects.class));
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu,menu);

    // We have to register SearchView to this listenear
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView  = (SearchView)menuItem.getActionView();
    searchView.setOnQueryTextListener(this);

    return true;
}

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

@Override
public boolean onQueryTextChange(String newText) {
    String userInput = newText.toLowerCase();
    List<Subjects> newList = new ArrayList<>();
    for (String nameee: subjects)
    {
        if (nameee.toLowerCase().contains(userInput))
        {
            newList.add(nameee);
        }
    }
    adapter.updateList(newList);
    return true;

}
}

这是Adapter类

public class RecyclearAdapterSubjects extends 
       RecyclerView.Adapter<RecyclearAdapterSubjects.MyViewHolder> {

          List<Subjects> subjects;
         public Context context;
          public DatabaseHelper mDatabaseHelper;

          public RecyclearAdapterSubjects(List<Subjects> subjects,Context                                   
          context)
{
    this.subjects = subjects;
    this.context =context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
    return new MyViewHolder(view,context,subjects);
}

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

    holder.sName.setText(subjects.get(position).getsName());
    holder.sCode.setText(subjects.get(position).getsCode());
}

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


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView sName, sCode;
    Context context;
    LinearLayout layout;
    List<Subjects> subjects;

    public MyViewHolder(View itemView, Context context, List<Subjects> subjects) {
        super(itemView);
        this.context = context;
        this.subjects = subjects;
        itemView.setOnClickListener(this);
        layout = (LinearLayout) itemView.findViewById(R.id.linearTopMost);
        sName = (TextView) itemView.findViewById(R.id.subjectName);
        sCode = (TextView) itemView.findViewById(R.id.subjectCode);

    }

    @Override
    public void onClick(View view) {
        mDatabaseHelper = new DatabaseHelper(context);
        int position = getAdapterPosition();
        Subjects obj = this.subjects.get(position);
        Toast.makeText(context, obj.getsName() + "", 
   Toast.LENGTH_SHORT).show();

        System.out.println("In OnClick Upper Side");

        try {
            String Subject_name = obj.getsName();
            //mDatabaseHelper.getWritableDatabase();
            boolean insertData = mDatabaseHelper.addData(Subject_name + "");


            if (insertData) {
                Toast.makeText(context, "Data inserted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            System.out.println("Exception handling here");
        }
        System.out.println("In OnClick Lower Side");

    }


}

//Call when filter RecyclearView

public void updateList(List<Subjects> newList)
{
    subjects = newList;
    subjects.addAll(newList);
    notifyDataSetChanged();
}
}

只需检查我如何解决搜索功能,recyclelerview工作正常。 为什么有人不回复

0 个答案:

没有答案