实现过滤的接口上我CustomListAdapter后无法重装的ListView内容重装、上我、接口、内容

2023-09-04 02:58:05 作者:熬过去就好了

我有两个问题。

第一个问题去:: 我实施可筛选界面,我CustomListAdapter,使搜索的列表数据后,我意识到,每次我试图重新加载列表视图,它拒绝填充ListView甚至以为我能够从我的服务器上获取数据。 (我的ListView总是在重新加载设备方向的变化,我还增加了一个刷新按钮刷新列表视图的onclick太)

The First Question goes :: After i implementing Filterable interface in my CustomListAdapter to enable search on list data, i realized that each time i try to reload the listview, it refused to populate the listview even thought i was able to fetch the data from my server. (My ListView always reloads on device orientation change and i also added a reload button to reload the listView onclick too)

第二个问题去:: 我的搜索EditView中填充我的ListView很好的onchange但我意识到,当我清空EditView中,在ListView的previous数据不会恢复/来,回到它的previous含量

The Second Question goes :: My search EditView populates my listView very well onchange but i realized that when i empty the EditView, the previous data in the ListView does not revert/come-back to its previous content

下面是我的活动code

Below is my Activity Code

public class MainActivity extends Activity {

   private ListView mList;
   private List<Movie> movieList = new ArrayList<Movie>();
   EditText inputSearch;
       private CustomListAdapter adapter;
   private ImageView  reload_img;


    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
           setContentView(R.layout.activity_main);

       mList = (ListView) findViewById(R.id.list);
       inputSearch = (EditText) findViewById(R.id.inputSearch);
           reload_img = (ImageView) findViewById(R.id.reload_list);


           adapter = new CustomListAdapter(this, movieList);
           mList.setAdapter(adapter);

        //FETCH Movie-Data FROM SERVER TO LIST
         fetchMovie();

        //Reload image click
             reload_img.setOnClickListener(new View.OnClickListener() {
             public void onClick(View arg0) {

                    reload_list();
                    }
            });

        //SEARCH TEXTCHANGE
        inputSearch.addTextChangedListener(new TextWatcher() {

             @Override
             public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                   // When user changed the Text
                  MainActivity.this.adapter.getFilter().filter(cs.toString());
                //FLAGS Cannot resolve method 'getFilter()' here
                }

             @Override
             public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                  // TODO Auto-generated method stub

             }

             @Override
             public void afterTextChanged(Editable arg0) {
                 // TODO Auto-generated method stub
             }
          });




    }


    //RELOAD LISTVIEW METHOD
            private void reload_list()
            {

                pDialog = new ProgressDialog(MainActivity.this);
                // Showing progress dialog before making http request
                pDialog.setMessage("Synchronising....");
                pDialog.show();
                pDialog.setCancelable(false);
                pDialog.setCanceledOnTouchOutside(false);


                flag_loading = true;
                offSet = 0;
                inputSearch.setText("");
                movieList.clear();

                fetchMovie();
                //adapter.notifyDataSetChanged();



            }


    //LOAD DATA FROM SERVER TO LISTVIEW USING VOLLEY
    private void fetchMovie()
            {

                // showing refresh animation before making http call
                //swipeRefreshLayout.setRefreshing(true);

                // appending offset to url
               String url = Config.URL_TOP_250_URL + offSet;
                //String url = Config.MOVIE_JSON_URL;
               //String url = Config.MOVIE_JSON_URL_SHORT;

               // Toast.makeText(MainActivity.this,
                 //       "::Json loaded:: ", Toast.LENGTH_SHORT).show();

                JsonArrayRequest movieReq = new JsonArrayRequest(url,
                        new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {
                                // Log.d(TAG, response.toString());
                                hidePDialog();

                                if (response.length() > 0) {

                                    //Toast.makeText(MainActivity.this,
                                      //     "Json String : \n"+ response.toString(), Toast.LENGTH_LONG).show();

                                    // Parsing json
                                   /**/ for (int i = 0; i < response.length(); i++) {
                                        try {

                                            JSONObject obj = response.getJSONObject(i);

                                            int rank = obj.getInt("rank");

                                            Movie movie = new Movie();

                                            // movie.setRank(obj.getInt("rank"));

                                            movie.setTitle(obj.getString("title"));
                                            movie.setThumbnailUrl(obj.getString("image"));
                                            movie.setRating(((Number) obj.get("rating"))
                                                    .doubleValue());

                                            movie.setYear(obj.getInt("releaseYear"));

                                            // Genre is json array
                                            JSONArray genreArry = obj.getJSONArray("genre");
                                            ArrayList<String> genre = new ArrayList<String>();
                                            for (int j = 0; j < genreArry.length(); j++) {
                                                genre.add((String) genreArry.get(j));
                                            }
                                            movie.setGenre(genre);

                                            // adding movie to movies array
                                            movieList.add(movie);

                                            // updating offset value to highest value
                                            if (rank >= offSet)
                                                offSet = rank;
                                            flag_loading = false;
                                        } catch (JSONException e) {
                                            e.printStackTrace();

                                            Toast.makeText(MainActivity.this,
                                                    "Network connection error \n "+  e.getMessage(), Toast.LENGTH_LONG).show();
                                        }

                                    }
                                    /**/

                                    // notifying list adapter about data changes
                                    // so that it renders the list view with updated data
                                    adapter.notifyDataSetChanged();

                                }
                                else {
                                    Toast.makeText(MainActivity.this,
                                            "End of movie-list" , Toast.LENGTH_LONG).show();

                                }


                            }




                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error :: " + error.getMessage());
                        hidePDialog();


                    }

                });

                // Adding request to request queue
                MyApplication.getInstance().addToRequestQueue(movieReq);
            }


}

这里是code在我CustomListAdapter

And here is the code in my CustomListAdapter

public class CustomListAdapter extends BaseAdapter {

         private Activity activity;
     private LayoutInflater inflater;
     private List<Movie> movieItems;
      private List<Movie> originalMovieList;
         private String[] bgColors;
     private List<String>originalData = null;
     private List<String>filteredData = null;
     private ItemFilter mFilter = new ItemFilter();
         ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();


         public CustomListAdapter(Activity activity, List<Movie> movieItems) {
             this.activity = activity;
             this.movieItems = movieItems;
        this.originalMovieList = movieItems;
             bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
         }
         @Override
         public int getCount() {
                return movieItems.size();
         }

         @Override
        public Object getItem(int location) {
             return movieItems.get(location);
         }

            @Override
            public long getItemId(int position) {
               return position;
            }

         @Override
         public View getView(int position, View convertView, ViewGroup parent) {

            if (inflater == null)
              inflater = (LayoutInflater) activity
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             if (convertView == null)
                convertView = inflater.inflate(R.layout.list_row_image, null);

             if (imageLoader == null)
              imageLoader = MyApplication.getInstance().getImageLoader();
            NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);

             TextView serial = (TextView) convertView.findViewById(R.id.serial);
            TextView title = (TextView) convertView.findViewById(R.id.title);
            TextView rating = (TextView) convertView.findViewById(R.id.rating);
            TextView genre = (TextView) convertView.findViewById(R.id.genre);
            TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

            // getting movie data for the row
            Movie m = movieItems.get(position);

            // thumbnail image
            thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

            // title
            title.setText(m.getTitle());

            // rating
             rating.setText("Rating: " + String.valueOf(m.getRating()));

            // genre
            String genreStr = "";
            for (String str : m.getGenre()) {
              genreStr += str + ", ";
              }
            genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                 genreStr.length() - 2) : genreStr;
             genre.setText(genreStr);

            // release year
             year.setText(String.valueOf(m.getYear()));

             String color = bgColors[position % bgColors.length];
            serial.setBackgroundColor(Color.parseColor(color));




            return convertView;
     }




            public Filter getFilter() {
                return mFilter;
            }

            private class ItemFilter extends Filter {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {

                    String filterString = constraint.toString().toLowerCase();

                    FilterResults results = new FilterResults();

                    //final List<String> list = originalData;
                    final List<Movie> list = movieItems;

                    int count = list.size();
                   // final ArrayList<String> nlist = new ArrayList<String>(count);
                    final ArrayList<Movie> nlist = new ArrayList<Movie>(count);

                    String filterableString ;

                    for (int i = 0; i < count; i++) {
                        filterableString = list.get(i).getTitle();
                        if (filterableString.toLowerCase().contains(filterString)) {
                           // nlist.add(filterableString);
                            nlist.add(list.get(i));
                        }
                    }

                    results.values = nlist;
                    results.count = nlist.size();

                    return results;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    //filteredData = (ArrayList<String>) results.values;
                    movieItems = (ArrayList<Movie>) results.values;
                    notifyDataSetChanged();
                }

            }


}

正如你看到的,我真的把我的时间把这个整个code一起,但在这个路口,我的头旋转,因为我无法弄清楚,为什么

As you see i really took my time to put this whole code together but at this junction, my head spins as i cant figure out why

在方向改变,ListView的数据重新填充在使用前突然停止填充后,我公司推出的可筛选以我的 CustomListAdapter 类也 MySQL反连接的优化总结 r10笔记第51天

On orientation change, the listView data which use to rePopulate before suddenly stops populating after i introduced the Filterable to my CustomListAdapter class and also

当我清除搜索EditView中,在ListView内容不返回

When i clear the search EditView, the content in the ListView does not return to

当我点击刷新按钮,在列表中的内容不重装,就像它当设备方向的变化。

When i click on the reload button, the content in the list does not reload, just like it does when the device orientation changes.

我真的AP preciate的提示来帮助我,因为我是那么那么的卡现在。 在此先感谢

I would really appreciate a hint to help me out as i am so so stuck right now. Thanks in Advance

- 编辑CustomListAdapter 我所做的更改

-Edited CustomListAdapter the changes i made are

               @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    //filteredData = (ArrayList<String>) results.values;
                    //movieItems = (ArrayList<Movie>) results.values;
                    movieItems.clear();
                    movieItems.addAll((ArrayList<Movie>) results.values);
                    notifyDataSetChanged();
                }

            }

也编辑了这一点。新增 this.originalMovieList =新的ArrayList&LT;电影&GT;(movieItems);

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
   // this.originalMovieList = movieItems;
    this.originalMovieList = new ArrayList<Movie>(movieItems);
    bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
}

添加此方法CustomListAdapter

Added this method to CustomListAdapter

//Add Below Method
public void reloadData(){
    this.originalMovieList = new ArrayList<Movie>(movieItems);
    notifyDataSetChanged();
}

也取代notifyDataSetChanged()的MainActivity类与此

Also replaced notifyDataSetChanged() in MainActivity Class with this

                     `// notifying list adapter about data changes
                    // so that it renders the list view with updated data
                     //adapter.notifyDataSetChanged();
                      adapter.reloadData();`

-reload按钮现在工作得很好,方向的变化重新加载页面很好,但搜索不填充ListView。 因此,搜索还没有成型

推荐答案

这是我如何能够解决我的搜索code中的痛苦。

This is how i was able to solve the misery of my searching code.

在我的 MainActivity

public class MainActivity extends Activity {

 private List<Movie> currentMovieList = new ArrayList<Movie>();
 private List<Movie> originalMovieList = new ArrayList<Movie>();
 private CustomListAdapter adapter;
 private ListView mList;
 // Search EditText
 EditText inputSearch;

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

inputSearch = (EditText) findViewById(R.id.inputSearch);

adapter = new CustomListAdapter(this, currentMovieList);
    mList.setAdapter(adapter);


    inputSearch.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            String filterString = cs.toString().toLowerCase();
            Log.e("TAG", "filterString:" + filterString);
            currentMovieList.clear();
            if (TextUtils.isEmpty(filterString)) {
                currentMovieList.addAll(originalMovieList);
            }

            String filterableString;
            for (Movie movie : originalMovieList) {
                if (movie.getTitle().toLowerCase().contains(filterString)) {
                    currentMovieList.add(movie);
                }
                else if (String.valueOf(movie.getYear()).toLowerCase().contains(filterString))
                {
                    currentMovieList.add(movie);
                }
                else if (String.valueOf(movie.getRating()).toLowerCase().contains(filterString))
                {
                    currentMovieList.add(movie);
                }
                else if (movie.getGenre().toString().toLowerCase().contains(filterString))
                {
                    currentMovieList.add(movie);
                }
            }
            adapter.notifyDataSetChanged();
            //FLAGS Cannot resolve method 'getFilter()' here
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });


}
}

和添加到您的 CustomListAdapter

public class CustomListAdapter extends BaseAdapter implements Filterable {

    private Activity activity;
 private LayoutInflater inflater;

 private List<String>originalData = null;
 // private List<String>filteredData = null;

 private List<Movie> movieItems;
 private List<Movie> originalMovieList;
 private String[] bgColors;
 ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
 private ItemFilter mFilter = new ItemFilter();


 public CustomListAdapter(Activity activity, List<Movie> movieItems) {
       this.activity = activity;
        this.movieItems = movieItems;
      // this.originalMovieList = movieItems;
      this.originalMovieList = new ArrayList<Movie>(movieItems);
      bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
        }

    //Add Below Method
    public void reloadData(){
       this.originalMovieList = new ArrayList<Movie>(movieItems);
     notifyDataSetChanged();
        }
     @Override
 public int getCount() {
     return movieItems.size();
        }

 @Override
    public Object getItem(int location) {
        return movieItems.get(location);
        }

    @Override
    public long getItemId(int position) {
       return position;
        }

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {

     if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row_image, null);

    if (imageLoader == null)
        imageLoader = MyApplication.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);

    TextView serial = (TextView) convertView.findViewById(R.id.serial);
    TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView rating = (TextView) convertView.findViewById(R.id.rating);
    TextView genre = (TextView) convertView.findViewById(R.id.genre);
    TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

    // getting movie data for the row
    Movie m = movieItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    // title
    title.setText(m.getTitle());

    // rating
    rating.setText("Rating: " + String.valueOf(m.getRating()));

     // genre
     String genreStr = "";
        for (String str : m.getGenre()) {
          genreStr += str + ", ";
     }
     genreStr = genreStr.length() > 0 ? genreStr.substring(0,
            genreStr.length() - 2) : genreStr;
        genre.setText(genreStr);

        // release year
     year.setText(String.valueOf(m.getYear()));

     String color = bgColors[position % bgColors.length];
        serial.setBackgroundColor(Color.parseColor(color));




      return convertView;
 }


            public Filter getFilter() {
                return mFilter;
            }

            private class ItemFilter extends Filter {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {

                    String filterString = constraint.toString().toLowerCase();

                    FilterResults results = new FilterResults();


                    //results.values = nlist;
                    //results.count = nlist.size();
                    results.values = originalMovieList;
                    results.count = originalMovieList.size();

                    return results;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    //filteredData = (ArrayList<String>) results.values;
                    //movieItems = (ArrayList<Movie>) results.values;
                    movieItems.clear();
                    movieItems.addAll((ArrayList<Movie>) results.values);
                    notifyDataSetChanged();
                }

            }



}

现在这就是一个总结。 由于@DhavalPatel 那家伙是一个Android大师。 他让这一切发生。 -Happy编码

Now thats a WRAP. Thanks to @DhavalPatel That Guy is an Android Guru. He made all this happen. -Happy Coding

 
精彩推荐
图片推荐