下载程序数据添加到searchview sugguest列表

yyyllmsg  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(211)

我试着想出了第一个主意,但却犯了很多错误。我可以显示数据,但它会导致其他项移动并破坏顺序。结果,我想的这个主意不起作用。如果我的工作是;将我下载的电影排序到一个看不见的回收视图中。
我从jsonarray获取数据。我想在searchview建议列表中显示此数据。把它想象成在谷歌上搜索。我该怎么办?这是我的主要活动

  1. `
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. initializeComponent();
  7. LinearLayoutManager linearLayoutManagerParentRecyclerView = new LinearLayoutManager(this);
  8. linearLayoutManagerParentRecyclerView.setOrientation(LinearLayoutManager.VERTICAL);
  9. MovieInitializeDownLoader movieInitializeDownLoader =new MovieInitializeDownLoader(MainActivity.this,recyclerViewParent
  10. , linearLayoutManagerParentRecyclerView,totalCategory,popular,topRate,upComing,nowPlaying);
  11. movieInitializeDownLoader.download(JSON_POPULAR_LIST_URL,getString(R.string.popular));
  12. movieInitializeDownLoader.download(JSON_TOP_RATE_LIST_URl,getString(R.string.Top_Rate));
  13. movieInitializeDownLoader.download(JSON_NOW_PLAYING_LIST_URL,getString(R.string.Up_coming));
  14. movieInitializeDownLoader.download(JSON_UP_COMING_LIST_URL,getString(R.string.now_playing));
  15. sliderImageDownload(JSON_UP_COMING_LIST_URL);
  16. if (getIntent().getBooleanExtra("LOGOUT", false))
  17. {
  18. finish();
  19. }
  20. }
  21. @Override
  22. public boolean onCreateOptionsMenu(Menu menu) {
  23. getMenuInflater().inflate(R.menu.bottom_nav_menu,menu);
  24. MenuItem searchItem = menu.findItem(R.id.action_search);
  25. SearchView searchView = (SearchView) searchItem.getActionView();
  26. searchView.setOnQueryTextListener(this);
  27. return true;
  28. }
  29. @Override
  30. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  31. switch (item.getItemId()){
  32. case R.id.navigation_home:
  33. TxtFileReader txtFileReader=new TxtFileReader();
  34. Intent intent=new Intent(MainActivity.this,UserMovieTopListActivity.class);
  35. intent.putStringArrayListExtra(ON_CLICKED_TOP_LIST_BUTTON_MOVIE_ID_INTENT_KEY,txtFileReader.read());
  36. startActivity( intent);
  37. return true;
  38. case R.id.action_search:
  39. return true;
  40. }
  41. return super.onOptionsItemSelected(item);
  42. }
  43. public void sliderImageDownload(String imageDataURL){
  44. JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET
  45. , imageDataURL
  46. , null
  47. , new Response.Listener<JSONObject>() {
  48. @Override
  49. public void onResponse(JSONObject response) {
  50. try {
  51. JSONArray jsonArray=response.getJSONArray(JSON_OBJECT_KEYWORD_RESULT);
  52. for (int i=0;i<13;i++){
  53. movie=new Movie();
  54. movie.setId(jsonArray.getJSONObject(i).getInt(JSON_OBJECT_KEYWORD_ID));
  55. movie.setMovieName(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_MOVIE_TITLE));
  56. movie.setMoviePosterImageURL(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_POSTER_PATH));
  57. movie.setRank(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_VOTE_AVERAGE));
  58. movie.setReleaseDate(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_RELEASE_DATE));
  59. movie.setMovieBackdropPathImageUrl(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_BACKDROP_PATH));
  60. sliderViewImageArrayList.add(movie);
  61. }
  62. Log.d(TAG, "onResponse: "+sliderViewImageArrayList);
  63. SliderImageAdapter sliderImageAdapter=new SliderImageAdapter(sliderViewImageArrayList,MainActivity.this);
  64. sliderView.setSliderAdapter(sliderImageAdapter);
  65. sliderView.startAutoCycle();
  66. sliderView.setSliderTransformAnimation(SliderAnimations.CUBEINROTATIONTRANSFORMATION);
  67. } catch (JSONException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }, new Response.ErrorListener() {
  72. @Override
  73. public void onErrorResponse(VolleyError error) {
  74. Log.d(TAG, "onErrorResponse: "+error);
  75. }
  76. });
  77. HttpConnector.getInstance(MainActivity.this).addRequestQue(jsonObjectRequest);
  78. }
  79. public void initializeComponent(){
  80. sliderView=findViewById(R.id.imageSlider);
  81. recyclerViewParent=findViewById(R.id.recyclerViewParent);
  82. searchRecyclerView=findViewById(R.id.searchRecyclerView);
  83. }
  84. @Override
  85. public boolean onQueryTextSubmit(String query) {
  86. searchRecyclerView.setVisibility(View.INVISIBLE);
  87. return false;
  88. }
  89. @Override
  90. public boolean onQueryTextChange(String newText) {
  91. JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET
  92. ,String.format(JSON_SEARCH_MOVIE_URL,newText)
  93. , null
  94. , new Response.Listener<JSONObject>() {
  95. @Override
  96. public void onResponse(JSONObject response) {
  97. ArrayList<Movie> searchMovies=new ArrayList<>();
  98. try {
  99. JSONArray jsonArray=response.getJSONArray(JSON_OBJECT_KEYWORD_RESULT);
  100. for (int i = 0; i < jsonArray.length(); i++) {
  101. movie=new Movie();
  102. movie.setId(jsonArray.getJSONObject(i).getInt(JSON_OBJECT_KEYWORD_ID));
  103. movie.setMovieName(jsonArray.getJSONObject(i).getString(JSON_OBJECT_KEYWORD_NAME));
  104. searchMovies.add(movie);
  105. }
  106. if (!newText.isEmpty()){
  107. RecyclerViewSearchViewAdapter searchViewAdapter=new RecyclerViewSearchViewAdapter(searchMovies);
  108. LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
  109. searchRecyclerView.setAdapter(searchViewAdapter);
  110. searchRecyclerView.setLayoutManager(layoutManager);
  111. searchRecyclerView.setVisibility(View.VISIBLE);
  112. }
  113. } catch (JSONException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118. , new Response.ErrorListener() {
  119. @Override
  120. public void onErrorResponse(VolleyError error) {
  121. Log.d(TAG, "onErrorResponse: "+error);
  122. }
  123. });
  124. HttpConnector.getInstance(MainActivity.this).addRequestQue(jsonObjectRequest);
  125. return false;
  126. }`

这是我的主要布局:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="wrap_content"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.MainActivity"
  8. android:background="@color/darkBlue"
  9. android:orientation="vertical"
  10. >
  11. <androidx.recyclerview.widget.RecyclerView
  12. android:id="@+id/searchRecyclerView"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:visibility="invisible"
  16. android:background="@color/white"
  17. />
  18. <com.smarteist.autoimageslider.SliderView
  19. android:id="@+id/imageSlider"
  20. android:layout_width="match_parent"
  21. android:layout_height="250dp"
  22. android:layout_marginTop="5dp"
  23. android:layout_marginStart="5dp"
  24. android:layout_marginEnd="5dp"
  25. android:layout_marginBottom="5dp"
  26. app:layout_constraintEnd_toEndOf="parent"
  27. app:layout_constraintHorizontal_bias="0.0"
  28. app:layout_constraintStart_toStartOf="parent"
  29. app:layout_constraintTop_toTopOf="parent"
  30. app:sliderAnimationDuration="600"
  31. app:sliderAutoCycleDirection="back_and_forth"
  32. app:sliderAutoCycleEnabled="true"
  33. app:sliderIndicatorAnimationDuration="600"
  34. app:sliderIndicatorGravity="center_horizontal|bottom"
  35. app:sliderIndicatorMargin="15dp"
  36. app:sliderIndicatorOrientation="horizontal"
  37. app:sliderIndicatorPadding="3dp"
  38. app:sliderIndicatorRadius="2dp"
  39. app:sliderIndicatorSelectedColor="#5A5A5A"
  40. app:sliderIndicatorUnselectedColor="#FFF"
  41. app:sliderScrollTimeInSec="3"
  42. app:sliderStartAutoCycle="true">
  43. </com.smarteist.autoimageslider.SliderView>
  44. <androidx.recyclerview.widget.RecyclerView
  45. android:layout_width="match_parent"
  46. android:layout_height="match_parent"
  47. android:id="@+id/recyclerViewParent"

/>
这是我的菜单xml:

  1. <item
  2. android:id="@+id/action_search"
  3. android:icon="@drawable/search_icon"
  4. android:title="@string/search"
  5. app:showAsAction="always"
  6. app:actionViewClass="androidx.appcompat.widget.SearchView"
  7. />
  8. <item
  9. android:id="@+id/navigation_home"
  10. android:icon="@drawable/ic_home_black_24dp"
  11. android:title="@string/title_home"
  12. app:showAsAction="always"
  13. />

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题