将当前项目位置传递给内部类?

时间:2015-08-14 09:05:11

标签: java android hashmap inner-classes

如何将当前的Hashmap键位置传递给内部类?

我需要将data.get(position)传递给我的内部类Click。通常我会宣布它是最终的,但我不知道在这种情况下如何做到这一点?

    public class UpcomingGridViewAdapter extends BaseAdapter {

    public boolean pressedMovieItem;

    Context context;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> mylist = new HashMap<>();

    public UpcomingGridViewAdapter(Context a, ArrayList<HashMap<String, String>> d) {
        context = a;
        data = d;
    }

    public int getCount() {
        return data.size();
    }

    public HashMap<String, String> getItem(int position) {
        return data.get(position);
    }

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

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.upcoming_grid_item, parent, false);
        }
        final ImageView poster = (ImageView) convertView.findViewById(R.id.upcoming_image);




        mylist = data.get(position);


        final String posterPath = mylist.get("poster_path");


        // set image url correctly
        // sizes for image 45, 92, 154, 185, 300, 500
        final String url = "http://image.tmdb.org/t/p/w185" + posterPath;


        // load image url into poster
        Picasso.with(context).load(url).into(poster);

        // load image url into poster

        // Get onclick of item and pass data to singleitemview for upcoming

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


          // need to pass data.get(position) to the inner class click
          new Click().execute();

            }
        });

        return convertView;


    }

    // Downloading data asynchronously
    class Click extends AsyncTask< Void, Void, Void> {

        @Override
        protected void onPreExecute() {

         // TODO setup progressdialog

        }

        @Override
        protected Void doInBackground(Void... params) {


            mylist = data.get(position); // need to get position 

            Intent intent = new Intent(context, UpcomingSingleItem.class);

            intent.putExtra("poster_path", mylist.get(Upcoming.TAG_POSTER));

            intent.putExtra("title", mylist.get(Upcoming.TAG_TITLE));

            intent.putExtra("release_date", mylist.get(Upcoming.TAG_RELEASE));

            intent.putExtra("overview", mylist.get(Upcoming.TAG_OVERVIEW));

            intent.putExtra("id", mylist.get(Upcoming.TAG_ID));

            intent.putExtra("vote_average", mylist.get(Upcoming.TAG_VOTE_AVG));


            context.startActivity(intent);


            return null;
        }


        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);

           // TODO stop progressdialog
        }

    }

}

1 个答案:

答案 0 :(得分:2)

如果我找对你,你只需要用params执行你的AsyncTask。请参阅文档:

http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)