清理我的适配器

时间:2018-03-14 21:19:21

标签: java android android-arrayadapter

我使用自定义图像适配器填充了GridView。我添加了一个按钮来排序图像。我试图弄清楚如何正确地做到这一点。截至目前,我正在尝试使用clear()方法删除所有图像,并使用已排序的图像重新填充它。但是,我似乎无法删除图像。它只是将排序后的图像添加到原始图像中。请帮忙

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;
    private TextView mTextView;
    String githubSearchResults;
    String default_sort = "http://api.themoviedb.org/3/movie/top_rated?api_key=(MY OWN API KEY)";
    ArrayList<String> listdata = new ArrayList<String>();
    List<String> posterData = new ArrayList<String>();
    JSONObject results;
    private MovieAdapter movieAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.movie_image);
        getURL(default_sort);
    }

    JSONArray getResults(String JSONString) throws JSONException {
        JSONObject movieResults = new JSONObject(JSONString);
        JSONArray jArray = (JSONArray)movieResults.getJSONArray("results");
        if (jArray != null) {
            for (int i=0;i<jArray.length();i++){
                listdata.add(jArray.getString(i));
            }
        }
        return jArray;
    }

    void getPosterPath() throws JSONException {
        for(int i = 0;i<listdata.size();i++) {
            results = new JSONObject(listdata.get(i));
            String poster_path = results.getString("poster_path");
            posterData.add(poster_path);
            Log.d("myTag", poster_path);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sort_popular, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.popular:
                movieAdapter.clear();
                movieAdapter.notifyDataSetChanged();
                String sort_popular = "http://api.themoviedb.org/3/movie/popular?api_key=(MY OWN API KEY)";
                getURL(sort_popular);
                return true;

            default:

                super.onOptionsItemSelected(item);

        }
        return true;
    }

    private void getURL(String string){
        URL getURL = NetworkUtils.buildUrl(string);
        new GithubQueryTask().execute(getURL);
    }

    public class GithubQueryTask extends AsyncTask<URL, Void, String> {
        // COMPLETED (2) Override the doInBackground method to perform the query. Return the results. (Hint: You've already written the code to perform the query)

        @Override
        protected String doInBackground(URL... params) {

            URL searchUrl = params[0];
            String githubSearchResults = null;
            try {
                githubSearchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
                getResults(githubSearchResults);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return githubSearchResults;
        }

        // COMPLETED (3) Override onPostExecute to display the results in the TextView

        @Override

        protected void onPostExecute(String githubSearchResults) {

            try {
                getPosterPath();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            movieAdapter = new MovieAdapter(MainActivity.this, posterData);

            GridView listView = (GridView) findViewById(R.id.listview_flavor);
            listView.setAdapter(movieAdapter);
        }
    }
}

这是我的自定义适配器:

public class MovieAdapter extends ArrayAdapter<String> {
    private ImageView mImageView;

    public MovieAdapter(Activity context, List<String> movieArray) {
        super(context, 0, movieArray);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String androidFlavor = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_detail, parent, false);
        }

        mImageView= (ImageView) convertView.findViewById(R.id.movie_image);
        Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w185/"+androidFlavor).into(mImageView);

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

如果我没有误会,问题是您永远不会清除 posterData 列表( listdata 也是如此)。你只是不断添加它。我建议改变 getPosterPath 方法,如下所示:

void getPosterPath() throws JSONException {
    posterData.Clear();
    for(int i = 0;i<listdata.size();i++) {
        results = new JSONObject(listdata.get(i));
        String poster_path = results.getString("poster_path");             
        posterData.add(poster_path);
        Log.d("myTag", poster_path);
    }
    movieAdapter.notifyDataSetChanged();
}

公共布尔值onOptionsItemSelected 中,您不需要致电

        movieAdapter.clear();
        movieAdapter.notifyDataSetChanged();

因为适配器绑定的列表中的任何更改都应在getPosterPath()执行后反映在适配器中。

修改

您收到此错误的原因是

movieAdapter = new MovieAdapter(MainActivity.this, posterData);

在调用getPosterPath()之后发生。

稍微更改 onPostExectute()

protected void onPostExecute(String githubSearchResults) {

    movieAdapter = new MovieAdapter(MainActivity.this, posterData);        

    try {
        getPosterPath();
    } catch (JSONException e) {
        e.printStackTrace();
    }


    GridView listView = (GridView) findViewById(R.id.listview_flavor);
    listView.setAdapter(movieAdapter);


}