早上好,我在更新通过json接收数据的listview的位置时遇到了问题。我能够过滤信息,但在将信息从一个列表项传递到另一个活动时,他只是传递过滤前的位置项。
这是我收到listview的课程:
public class HomeFragment extends Fragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
Movie movie;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
// Inflate the layout for this fragment
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(obj.getString("rating"));
movie.setGenre(obj.getString("genre"));
movie.setYear(obj.getString("releaseYear"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
//calls getFilter
adapter.getFilter().filter("The");
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
movie = movieList.get(position);
String thumbNail = movie.getThumbnailUrl();
String titulo = movie.getTitle();
String pontuacao = movie.getRating();
String genero = movie.getGenre();
String ano = movie.getYear();
// Starting single contact activity
Intent in = new Intent(getActivity(),
SearchResultsActivity.class);
in.putExtra("img", image);
in.putExtra("title", title);
in.putExtra("pontuacao", rating);
in.putExtra("genero", genre);
in.putExtra("ano", year);
startActivity(in);
}
});
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
这是我的适配器类:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private List<Movie> mMovieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
this.mMovieItems = movieItems;
}
@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, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
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
genre.setText("Genero: " + String.valueOf(m.getGenre()));
// release year
year.setText(String.valueOf(m.getYear()));
return convertView;
}
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence filter) {
FilterResults results = new FilterResults();
//if it was not carried out no filter inserts all items.
if (filter== null || filter.length() == 0) {
results.count = mMovieItems.size();
results.values = mMovieItems;
} else {
//creates an array to store the filtered objects.
List<Movie> itens_filtrados = new ArrayList<Movie>();
//runs list checking that contains the filter word in the description of the obje
for (int i = 0; i < mMovieItems.size(); i++) {
Movie data = mMovieItems.get(i);
filter= filter.toString().toLowerCase();
String condicao = data.getTitle().toLowerCase();
if (condicao.contains(filter)) {
//restrain adds to the list of filtered items .
itens_filtrados.add(data);
}
}
//Sets the filter result in variable FilterResults
results.count = itens_filtrados.size();
results.values = itens_filtrados;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
movieItems = (List<Movie>) results.values; //filtered values.
notifyDataSetChanged(); //Notifies the change list
}
};
return filter;
}
我希望有人可以帮助我,提前谢谢你。
答案 0 :(得分:0)
问题是您不使用适配器中的过滤后的movieList。您可以在活动中使用该列表(未过滤)。
要解决此问题,只需将已过滤列表的getter添加到适配器。
public List<Movie> retrieveDisplayedMovies(){
return movieItems;
}
然后在您的活动中更改以下行
// getting values from selected ListItem
movie = movieList.get(position);
到这个
// getting values from selected ListItem
movie = adapter.retrieveDisplayedMovies().get(position);
答案 1 :(得分:0)
在onItemClick中,您将从完整列表中获取该项目,而不是已过滤的项目。
而不是:
movie = movieList.get(position);
您应该从适配器获取该项目,如下所示。
movie = (Movie) adapter.getItem(position);
这应该返回已过滤商品列表中的商品,因为您的publishResults
会将过滤商品列表设置为movieItems
。