ListView重复项

时间:2014-08-26 21:05:25

标签: android listview android-listview

我有一个片段(电影)里面有一个ListView,当我去另一个片段(电影细节)并通过祖传导回来(电影)时ListView复制项目(电影),我正在使用Volley来填充ListView。我不知道我做错了什么..这是我的代码:

VolleyAdapter:

public class VolleyAdapter extends BaseAdapter {

public VolleyAdapter(Activity activity, ArrayList<Movie> movies) {
        this.activity = activity;
        this.movies = movies;
}

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

    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = activity.getLayoutInflater().inflate(
                R.layout.list_item, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.nameTextView = (TextView) convertView
                .findViewById(R.id.item_lista_barraca_nomeTextView);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Movie movie = getItem(position);
    viewHolder.nameTextView.setText(movie.getName());

    return convertView;
}

static class ViewHolder {
    TextView nameTextView;
}
}

MoviesFragment:

public class MoviesFragment extends ListFragment {
private VolleyAdapter mAdapter;
private ArrayList<Movie> mMovies;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      mMovies = MovieDAO.get(getActivity()).getMovies();
      mAdapter = new VolleyAdapter(getActivity(), mMovies);
      setListAdapter(mAdapter);

      JsonArrayRequest req = new JsonArrayRequest(URL, 
            new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response {
              // create movie..
              mMovies.add(movie);
              mAdapter.notifyDataSetChanged();
            },new Response.ErrorListener() {
              ...
      });
      AppController.getInstance().addToRequestQueue(req);

    }

0 个答案:

没有答案