如何在Android中使用ASyncTask更新CustomAdapter

时间:2016-04-30 05:59:27

标签: android android-asynctask

我从www.omdbapi.com获取数据。我想在自定义ListView中显示电影ID和电影名称。但我陷入了onPostExecute()中AsyncTask的过程中。我无法更新自定义适配器。

我的ListView活动

public class MovieSearch extends AppCompatActivity {

ListViewAdapter listViewAdapter ;
ListView lview;
EditText moviename ;
ProgressDialog progressDialog;
String name ;

private final static String name1[] = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"};

private final static String id[] = {
        "Month - 1",
        "Month - 2",
        "Month - 3",
        "Month - 4",
        "Month - 5",
        "Month - 6",
        "Month - 7",
        "Month - 8",
        "Month - 9",
        "Month - 10",
        "Month - 11",
        "Month - 12"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie_search);

    moviename = (EditText) findViewById(R.id.search_movie_name);

    lview = (ListView) findViewById(R.id.list_movie);
    //listViewAdapter = new ListViewAdapter(this, new String[]{}, new String[]{});
    lview.setAdapter(listViewAdapter);

    lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object movieid = listViewAdapter.getItem(position);
            //Intent intent = new Intent(MovieSearch.this, MovieDetails .class).putExtra(Intent.EXTRA_TEXT, movieid);
            //startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void searching(View view) {
    name = moviename.getText().toString();
    new RetrieveFeedTask().execute(name);
}

public class RetrieveFeedTask extends AsyncTask<String, Void, String[]>{

    private final String LOG_TAG = RetrieveFeedTask.class.getSimpleName();

    private String[] getMovieDataFromJson(String jsonstring, int cnt) throws JSONException{

        final String SEARCH = "Search";
        final String TITLE = "Title";
        final String YEAR = "Year";
        final String POSTER = "Poster";
        final String MOVIEID = "imdbID" ;

        JSONObject jsonObject = new JSONObject(jsonstring);
        JSONArray jsonArray = jsonObject.getJSONArray(SEARCH);

        String[] resultStrs = new String[cnt];
        String[] movIdStrs = new String[cnt];
        String[] coverStrs = new String[cnt];

        for (int i = 0; i < jsonArray.length(); i++){
            String movname;
            String movyear;
            String movcover;
            String movieID;

            JSONObject titleobj = jsonArray.getJSONObject(i);
            movname = titleobj.getString(TITLE);
            movyear = titleobj.getString(YEAR);
            movcover = titleobj.getString(POSTER);
            movieID = titleobj.getString(MOVIEID);

            resultStrs[i] = movname + " [" + movyear + "]";
            movIdStrs[i] = movieID ;
            coverStrs[i] = movcover ;
            name1[i] = resultStrs[i];
            id[i] = movIdStrs[i];

        }

        for (String s : resultStrs){

            Log.v(LOG_TAG, "Movie Entry : " + s);
        }

        return resultStrs;
    }

    @Override
    protected void onPreExecute(){
        progressDialog = ProgressDialog.show(MovieSearch.this, "", "Searching for "+name+"...", true);
    }

    @Override
    protected String[] doInBackground(String... params) {

        String jsonString;

        try{
            URL url = new URL("http://www.omdbapi.com/?s="+params[0]);
            Log.v(LOG_TAG, "URL : " + url);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();
                progressDialog.dismiss();
                jsonString = stringBuilder.toString();
                Log.v(LOG_TAG, "JSONString : " + jsonString);
            }
            finally{
                urlConnection.disconnect();
            }
        }
        catch(Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return null;
        }

        try{
            return getMovieDataFromJson(jsonString, 10);
        }catch (JSONException e){
            Log.e(LOG_TAG, e.getMessage(), e);
            e.printStackTrace();
        }
        return new String[0];
    }

    @Override
    protected void onPostExecute(String[] res){
        if (res != null){
           // mMovie.clear();
            for (String newmovstr : res){
                //mMovieAdapter.add(newmovstr);
                listViewAdapter = new ListViewAdapter(MovieSearch.this, newmovstr, newmovstr);
            }
        }
    }
}
}

我的CustomListViewAdpter

public class ListViewAdapter extends BaseAdapter {

Activity context;
String name;
String id;

public ListViewAdapter(Activity context, String name, String id) {
    super();
    this.context = context;
    this.name = name;
    this.id = id;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

private class ViewHolder {
    TextView txtViewName;
    TextView txtViewID;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_item_movie, null);
        holder = new ViewHolder();
        holder.txtViewName = (TextView) convertView.findViewById(R.id.list_item_movie_textview);
        holder.txtViewID = (TextView) convertView.findViewById(R.id.list_item_movieID_textview);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewName.setText(name);
    holder.txtViewID.setText(id);

    return convertView;
}
}

3 个答案:

答案 0 :(得分:1)

首先创建电影模型类:

class Movie {
        String movieName;
        String coverName;
        String movieYear;
        String movieId;

        public String getMovieName() {
            return movieName;
        }

        public void setMovieName(String movieName) {
            this.movieName = movieName;
        }

        public String getCoverName() {
            return coverName;
        }

        public void setCoverName(String coverName) {
            this.coverName = coverName;
        }

        public String getMovieYear() {
            return movieYear;
        }

        public void setMovieYear(String movieYear) {
            this.movieYear = movieYear;
        }

        public String getMovieId() {
            return movieId;
        }

        public void setMovieId(String movieId) {
            this.movieId = movieId;
        }
    }

按如下方式更新您的适配器类:

public class ListViewAdapter extends BaseAdapter {

        Context context;

        ArrayList<Movie> movieArrayList;

        public ListViewAdapter(Context context, ArrayList<Movie> arrayList) {
            super();
            this.context = context;
            movieArrayList = arrayList;
        }

        @Override
        public int getCount() {
            return movieArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return movieArrayList.get(position);
        }

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

        private class ViewHolder {
            TextView txtViewName;
            TextView txtViewID;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater =  context.getLayoutInflater();

            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.list_item_movie, null);
                holder = new ViewHolder();
                holder.txtViewName = (TextView) convertView.findViewById(R.id.list_item_movie_textview);
                holder.txtViewID = (TextView) convertView.findViewById(R.id.list_item_movieID_textview);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtViewName.setText(movieArrayList.get(position).getMovieName());
            holder.txtViewID.setText(movieArrayList.get(position).getMovieId());

            return convertView;
        }
    }

创建ArrayList movieList;在activity中并在Oncreate方法中实例化它:movieList = new ArrayList&lt;&gt;();

将AsyncTask更新为:

 public class RetrieveFeedTask extends AsyncTask<String, Void, String>{

                private final String LOG_TAG = RetrieveFeedTask.class.getSimpleName();

                private void getMovieDataFromJson(String jsonstring) throws JSONException{

                    final String SEARCH = "Search";
                    final String TITLE = "Title";
                    final String YEAR = "Year";
                    final String POSTER = "Poster";
                    final String MOVIEID = "imdbID" ;

                    JSONObject jsonObject = new JSONObject(jsonstring);
                    JSONArray jsonArray = jsonObject.getJSONArray(SEARCH);

                    String[] resultStrs = new String[cnt];
                    String[] movIdStrs = new String[cnt];
                    String[] coverStrs = new String[cnt];

                    for (int i = 0; i < jsonArray.length(); i++){
                        String movname;
                        String movyear;
                        String movcover;
                        String movieID;

                        JSONObject titleobj = jsonArray.getJSONObject(i);
                        movname = titleobj.getString(TITLE);
                        movyear = titleobj.getString(YEAR);
                        movcover = titleobj.getString(POSTER);
                        movieID = titleobj.getString(MOVIEID);

                        Movie movie = new Movie();
                        movie.setMovieName(movname);
                        movie.setMovieId(movieID);
                        // similarly add other attributes
                        movieList.add(movie);

                    }




                }

                @Override
                protected void onPreExecute(){
                    progressDialog = ProgressDialog.show(MovieSearch.this, "", "Searching for "+name+"...", true);
                }

                @Override
                protected String[] doInBackground(String... params) {

                    String jsonString;

                    try{
                        URL url = new URL("http://www.omdbapi.com/?s="+params[0]);
                        Log.v(LOG_TAG, "URL : " + url);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        try {
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                            StringBuilder stringBuilder = new StringBuilder();
                            String line;
                            while ((line = bufferedReader.readLine()) != null) {
                                stringBuilder.append(line).append("\n");
                            }
                            bufferedReader.close();
                            progressDialog.dismiss();
                            jsonString = stringBuilder.toString();
                            Log.v(LOG_TAG, "JSONString : " + jsonString);
                            return jsonString;
                        }
                        finally{
                            urlConnection.disconnect();
                        }
                    }
                    catch(Exception e) {
                        Log.e("ERROR", e.getMessage(), e);
                        return null;
                    }


                    return null;
                }

                @Override
                protected void onPostExecute(String jsonString){
                    if(progressDialog != null){

                        //hide your progress dialog
                    }
                    if (jsonString != null){
                        // mMovie.clear();
                        movieList.clear();
                        getMovieDataFromJson(jsonString);
                        if(listViewAdapter == null) {

                            //mMovieAdapter.add(newmovstr);
                            listViewAdapter = new ListViewAdapter(MovieSearch.this, movieList);
listview.setAdapter(listViewAdapter);
                        }else{
                            listViewAdapter.notifyDataSetChanged();
                        }

                    }
                }
            }

答案 1 :(得分:0)

在你的 postExecute()方法删除for循环中设置适配器并按照这样做。

listViewAdapter = new ListViewAdapter(MovieSearch.this, newmovstr, newmovstr);
yourListView.setAdapter(listViewAdapter);

答案 2 :(得分:0)

更改onPostExecute方法

$this->adminNewProductForm->validate($input);