На самом деле проблема в том, что в окне поиска мои предложения правильно отображаются, которые извлекаются из API, поэтому вот ss поисковой активности:
при щелчке по представлению таблицы подсказок, я перехожу к активности просмотра списка
Я понял, это потому, что я использовал getadadpterposition () в адаптере
, посмотрите это ->
intent.putExtra("idSLnext", movieList.get(getAdapterPosition()).getId());
`SuggestionAdapter:
public class SuggestionAdapter extends RecyclerView.Adapter<SuggestionAdapter.ViewHolder> implements Filterable
{
private Context context;
private List<StartLearning.SlModel> movieList;
private List<StartLearning.SlModel> movieListAgain;
public SuggestionAdapter(Context context, List<StartLearning.SlModel> movieList) {
this.context = context;
this.movieList = movieList;
this.movieListAgain=movieList;
}
@NonNull
@Override
public SuggestionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item_chips,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SuggestionAdapter.ViewHolder holder, final int position) {
holder.tvSuggestion.setText(movieListAgain.get(position).getTitle());
/* holder.ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
movieListAgain.remove(position);
notifyDataSetChanged();
}
});*/
}
@Override
public int getItemCount() {
if(movieList != null){
return movieListAgain.size();
} else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView tvSuggestion;
ImageView ivDelete;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvSuggestion=itemView.findViewById(R.id.tvSuggestion);
// ivDelete=itemView.findViewById(R.id.ivDelete);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NextSLactivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("title", movieList.get(getAdapterPosition()).getTitle());
intent.putExtra("idSLnext", movieList.get(getAdapterPosition()).getId());
//Log.e("ashwini",WAmdel.get(getAdapterPosition()).getId());
context.startActivity(intent);
}
});
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final List<StartLearning.SlModel> results = new ArrayList<StartLearning.SlModel>();
if (movieList == null)
movieListAgain = movieList;
if (constraint != null) {
if (movieListAgain != null & movieListAgain.size() > 0) {
for (final StartLearning.SlModel g : movieListAgain) {
if (g.getTitle().toLowerCase().contains(constraint.toString().trim()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
movieListAgain = (ArrayList<StartLearning.SlModel>) results.values;
notifyDataSetChanged();
}
};
}
public void setMovieList(Context context, final List<StartLearning.SlModel> movieList){
this.context = context;
if(this.movieList == null){
this.movieList = movieList;
this.movieListAgain = movieList;
notifyItemChanged(0, movieListAgain.size());
} else {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return SuggestionAdapter.this.movieList.size();
}
@Override
public int getNewListSize() {
// return movieList.size();
return (movieList == null) ? 0 : movieList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return SuggestionAdapter.this.movieList.get(oldItemPosition).getTitle() == movieList.get(newItemPosition).getTitle();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
StartLearning.SlModel newMovie = SuggestionAdapter.this.movieList.get(oldItemPosition);
StartLearning.SlModel oldMovie = movieList.get(newItemPosition);
return newMovie.getTitle() == oldMovie.getTitle() ;
}
});
this.movieList = movieList;
this.movieListAgain = movieList;
result.dispatchUpdatesTo(this);
}
}
}
поисковое действие:
public class Search extends AppCompatActivity {
SearchView searchView;
RecyclerView recyclerView,recyclerViewSuggestions;
TextView tvSuggestions;
SuggestionAdapter suggestionAdapter;
SuggestionAdapter1 suggestionAdapter1;
List<StartLearning.SlModel> movieList;
List<GetAhead.GetaheadModel> modelList1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchView=findViewById(R.id.searchView);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
tvSuggestions=findViewById(R.id.tvSuggestions);
recyclerViewSuggestions=findViewById(R.id.recyclerviewSuggestions);
recyclerViewSuggestions.setLayoutManager(new GridLayoutManager(this,3));
searchView.onActionViewExpanded();
searchView.setIconified(true);
SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
retrofit2.Call<List<StartLearning.SlModel>> call = service.getMySlmodel();
call.enqueue(new Callback<List<StartLearning.SlModel>>() {
@Override
public void onResponse(retrofit2.Call<List<StartLearning.SlModel>> call, Response<List<StartLearning.SlModel>> response) {
movieList = response.body();
if(movieList.size()!=0){
tvSuggestions.setVisibility(View.VISIBLE);
suggestionAdapter=new SuggestionAdapter(Search.this,movieList);
recyclerViewSuggestions.setAdapter(suggestionAdapter);
Log.e("TAG", "onResponse: size of movielist "+movieList);
suggestionAdapter.notifyDataSetChanged();
suggestionAdapter.setMovieList(Search.this,movieList);
}
else{
tvSuggestions.setVisibility(View.VISIBLE);
tvSuggestions.setText("No Suggestions Found");
}
}
@Override
public void onFailure(retrofit2.Call<List<StartLearning.SlModel>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});
GetAheadApiService service1 = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
retrofit2.Call<List<GetAhead.GetaheadModel>> call1 = service1.getaheadapi();
call1.enqueue(new Callback<List<GetAhead.GetaheadModel>>() {
@Override
public void onResponse(retrofit2.Call<List<GetAhead.GetaheadModel>> call, Response<List<GetAhead.GetaheadModel>> response) {
modelList1 = response.body();
if(modelList1.size()!=0){
tvSuggestions.setVisibility(View.VISIBLE);
suggestionAdapter1=new SuggestionAdapter1(Search.this,modelList1);
recyclerViewSuggestions.setAdapter(suggestionAdapter1);
Log.e("TAG", "onResponse: size of movielist "+movieList);
// suggestionAdapter.getFilter().filter(query);
suggestionAdapter1.notifyDataSetChanged();
suggestionAdapter1.setMovieList(Search.this,modelList1);
}
else{
tvSuggestions.setVisibility(View.VISIBLE);
tvSuggestions.setText("No Suggestions Found");
}
}
@Override
public void onFailure(retrofit2.Call<List<GetAhead.GetaheadModel>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(final String query) {
SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
retrofit2.Call<List<StartLearning.SlModel>> call = service.getMySlmodel();
call.enqueue(new Callback<List<StartLearning.SlModel>>() {
@Override
public void onResponse(retrofit2.Call<List<StartLearning.SlModel>> call, Response<List<StartLearning.SlModel>> response) {
movieList = response.body();
if(movieList.size()!=0){
tvSuggestions.setVisibility(View.VISIBLE);
suggestionAdapter=new SuggestionAdapter(Search.this,movieList);
recyclerViewSuggestions.setAdapter(suggestionAdapter);
Log.e("TAG", "onResponse: size of movielist "+movieList);
suggestionAdapter.getFilter().filter(query);
suggestionAdapter.notifyDataSetChanged();
suggestionAdapter.setMovieList(Search.this,movieList);
}
else{
tvSuggestions.setVisibility(View.VISIBLE);
tvSuggestions.setText("No Suggestions Found");
}
}
@Override
public void onFailure(retrofit2.Call<List<StartLearning.SlModel>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});
GetAheadApiService service1 = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
retrofit2.Call<List<GetAhead.GetaheadModel>> call1 = service1.getaheadapi();
call1.enqueue(new Callback<List<GetAhead.GetaheadModel>>() {
@Override
public void onResponse(retrofit2.Call<List<GetAhead.GetaheadModel>> call, Response<List<GetAhead.GetaheadModel>> response) {
modelList1 = response.body();
if(modelList1.size()!=0){
tvSuggestions.setVisibility(View.VISIBLE);
suggestionAdapter1=new SuggestionAdapter1(Search.this,modelList1);
recyclerViewSuggestions.setAdapter(suggestionAdapter1);
Log.e("TAG", "onResponse: size of movielist "+movieList);
suggestionAdapter1.getFilter().filter(query);
suggestionAdapter1.notifyDataSetChanged();
suggestionAdapter1.setMovieList(Search.this,modelList1);
}
else{
tvSuggestions.setVisibility(View.VISIBLE);
tvSuggestions.setText("No Suggestions Found");
}
}
@Override
public void onFailure(retrofit2.Call<List<GetAhead.GetaheadModel>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});
return false;
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() ==android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}}
следующее действие для получения намерения:
public class NextSLactivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NextSLJavaAdapter adapter;
private NextSLModel DescriptList;
private BottomNavigationView bottomNavigation;
private Fragment fragment;
private FragmentManager fragmentManager;
Button button1;
Button button2;
Button button3;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextslearning);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
Toolbar toolbar = (Toolbar) findViewById(R.id. toolbar );
setSupportActionBar( toolbar );
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
Intent intent = getIntent();
String title = intent.getStringExtra("title");
getSupportActionBar().setTitle(title);
String id = intent.getStringExtra("idSLnext");
Log.e("ashwini", String.valueOf(id));
setFragment(new JavaFragment());
button1.setBackgroundResource(R.drawable.javabutton);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(new JavaFragment());
button1.setBackgroundResource(R.drawable.javabutton);
button2.setBackgroundResource(R.drawable.demobutton);
button3.setBackgroundResource(R.drawable.demobutton);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(new XMLFragmet());
button2.setBackgroundResource(R.drawable.javabutton);
button1.setBackgroundResource(R.drawable.demobutton);
button3.setBackgroundResource(R.drawable.demobutton);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(new DemoFragment());
button3.setBackgroundResource(R.drawable.javabutton);
button1.setBackgroundResource(R.drawable.demobutton);
button2.setBackgroundResource(R.drawable.demobutton);
}
});
}
public void setFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
fragmentTransaction.replace(R.id.main_container,fragment);
fragmentTransaction.commit();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() ==android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}