将变量值从基本适配器传递到活动

发布于 2024-12-11 09:35:25 字数 8080 浏览 0 评论 0原文

我在我的基本适配器类中设置了一个变量,现在我想在我的相关活动中获取(传递)这个变量。我不知道该怎么做。

这是我的代码。

  public class TourDescAdapter extends BaseAdapter {

    private List<Descriptions> descriptList;
    private LayoutInflater mInflater;
    ViewHolder holder;
    @SuppressWarnings("unused")
    private OnClickListener clickListener; 
    Activity context;
    //TourDescription tourDesc;
    ArrayList<HashMap<String, Object>> obj = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> discountedTourDetails = null;
    String price = null, prodId = null;
    String promoTourname, tourName;

    public TourDescAdapter(List<Descriptions> descriptList,
            TourDescription activity) {
        this.context = activity;
        this.descriptList = descriptList;
        mInflater = LayoutInflater.from(activity);
        clickListener = (OnClickListener) activity;

    }

    @Override
    public int getCount() {

        return this.descriptList.size();
    }

    @Override
    public Object getItem(int position) {

        return this.descriptList.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.tourlist, null);

            /****
             * Creates a ViewHolder and store references to the two children
             * views we want to bind data to
             ****/
            holder = new ViewHolder();
            holder.rlayout = (RelativeLayout) convertView
                    .findViewById(R.id.tourlayout);

            holder.title = (TextView) convertView
                    .findViewById(R.id.tourtitletext);
            holder.desc = (TextView) convertView.findViewById(R.id.tourdes);
            holder.amountButton = (Button) convertView
                    .findViewById(R.id.amtBtn);
            holder.pinButton = (Button) convertView.findViewById(R.id.pinBtn);
            holder.arrowButton = (Button)convertView.findViewById(R.id.arrowBtn);
            holder.serialText = (EditText)convertView.findViewById(R.id.pinText);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText((String) descriptList.get(position)
                .getImageTitle());
        holder.desc.setText((String) descriptList.get(position)
                .getImageDescription());
        ((ImageView) holder.rlayout.getChildAt(0)).setImageBitmap(BitmapFactory
                .decodeFile((RaconTours.PATH + RaconTours.city + File.separator
                        + TourDescription.currentTour.getObjtourName()
                        + File.separator + descriptList.get(position)
                        .getImagePath().split("/")[2]).replace(" ", "_")));

        if (position == 0) {
            SharedPreferences settings = context.getSharedPreferences("downloadDetails", 0);
            String isTourDownloaded = settings.getString(TourDescription.currentTour.getObjtourName(), "");
            if (isTourDownloaded.equals("true")) {
            //if (!(TourDescription.downloadFile.exists())||TourDescription.downloadFile.exists() == false ) {
            //if (TourDescription.currentTour.getIsTourDownloaded() == true) {
                //holder.pinButton.setVisibility(View.INVISIBLE);
                //holder.arrowButton.setVisibility(View.INVISIBLE);
                //holder.serialText.setVisibility(View.INVISIBLE);
                }
                holder.amountButton.setVisibility(View.VISIBLE);
                holder.amountButton.setText("Start");



            } else {
                File promoPlistPath = new File(RaconTours.PATH + "promocode.txt");
                checkPromoCode(promoPlistPath);
                if (discountedTourDetails != null) {
                    tourName = (String) discountedTourDetails.get("promoTour");
                    price = (String) discountedTourDetails.get("discountPrice");
                    prodId = (String) discountedTourDetails.get("disProId");

                    holder.amountButton.setVisibility(View.VISIBLE);
                    // Setting the background color
                    holder.title
                            .setBackgroundColor(Color.parseColor("#993333"));
                    // Setting the Title color
                    holder.title.setTextColor(Color.WHITE);
                    // Centering the title
                    holder.title.setGravity(Gravity.LEFT);
                    // setting the city
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setText(RaconTours.city);
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setVisibility(View.VISIBLE);
                    // setting the Tour Amount
                    holder.amountButton.setText("$" +price);
                    //promoPlistPath.delete();
                } else {

                    // Enabling the two buttons
                    holder.amountButton.setVisibility(View.VISIBLE);
                    // Setting the background color
                    holder.title
                            .setBackgroundColor(Color.parseColor("#993333"));
                    // Setting the Title color
                    holder.title.setTextColor(Color.WHITE);
                    // Centering the title
                    holder.title.setGravity(Gravity.LEFT);
                    // setting the city
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setText(RaconTours.city);
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setVisibility(View.VISIBLE);
                    // setting the Tour Amount
                    holder.amountButton.setText(TourDescription.currentTour
                            .getObjPrice());

                }
            }
        } else {
            holder.amountButton.setVisibility(View.INVISIBLE);
            holder.pinButton.setVisibility(View.INVISIBLE);
            holder.arrowButton.setVisibility(View.INVISIBLE);
            holder.serialText.setVisibility(View.INVISIBLE);
            holder.title.setBackgroundColor(Color.WHITE);
            holder.title.setTextColor(Color.BLACK);
            holder.title.setGravity(Gravity.CENTER_HORIZONTAL);
            ((TextView) holder.rlayout.getChildAt(1))
                    .setVisibility(View.INVISIBLE);
        }
        return convertView;
    }


    @SuppressWarnings("unchecked")
    private void checkPromoCode(File promoPlistPath) {
        if (promoPlistPath.exists()) {
            try {

                ObjectInputStream inStream = new ObjectInputStream(
                        new FileInputStream(promoPlistPath));
                obj = (ArrayList<HashMap<String, Object>>) inStream
                        .readObject();
                for (HashMap<String, Object> tmpObj : obj) {
                    promoTourname = (String) tmpObj.get("promoTour");
                    if (promoTourname.equals(TourDescription.currentTour.getObjtourName())) {
                        discountedTourDetails = tmpObj;
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    class ViewHolder {
        Button pinButton;
        Button amountButton;
        RelativeLayout rlayout;
        TextView title;
        TextView desc;
        Button arrowButton;
        EditText serialText;

    }
}

这里

    prodId = (String) discountedTourDetails.get("disProId");

我想将 prodId 传递给相关活动。

注意:基础适配器是从活动中调用的

    adapter = new TourDescAdapter(currentTour.getListOfDescriptions(), this);

    setListAdapter(adapter);

任何人都可以告诉我如何做到这一点?

I have a set a variable in my Base Adapter class, now I want to get(pass) this variable in my related Activity. I am not getting how to do this.

Here is my code.

  public class TourDescAdapter extends BaseAdapter {

    private List<Descriptions> descriptList;
    private LayoutInflater mInflater;
    ViewHolder holder;
    @SuppressWarnings("unused")
    private OnClickListener clickListener; 
    Activity context;
    //TourDescription tourDesc;
    ArrayList<HashMap<String, Object>> obj = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> discountedTourDetails = null;
    String price = null, prodId = null;
    String promoTourname, tourName;

    public TourDescAdapter(List<Descriptions> descriptList,
            TourDescription activity) {
        this.context = activity;
        this.descriptList = descriptList;
        mInflater = LayoutInflater.from(activity);
        clickListener = (OnClickListener) activity;

    }

    @Override
    public int getCount() {

        return this.descriptList.size();
    }

    @Override
    public Object getItem(int position) {

        return this.descriptList.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.tourlist, null);

            /****
             * Creates a ViewHolder and store references to the two children
             * views we want to bind data to
             ****/
            holder = new ViewHolder();
            holder.rlayout = (RelativeLayout) convertView
                    .findViewById(R.id.tourlayout);

            holder.title = (TextView) convertView
                    .findViewById(R.id.tourtitletext);
            holder.desc = (TextView) convertView.findViewById(R.id.tourdes);
            holder.amountButton = (Button) convertView
                    .findViewById(R.id.amtBtn);
            holder.pinButton = (Button) convertView.findViewById(R.id.pinBtn);
            holder.arrowButton = (Button)convertView.findViewById(R.id.arrowBtn);
            holder.serialText = (EditText)convertView.findViewById(R.id.pinText);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText((String) descriptList.get(position)
                .getImageTitle());
        holder.desc.setText((String) descriptList.get(position)
                .getImageDescription());
        ((ImageView) holder.rlayout.getChildAt(0)).setImageBitmap(BitmapFactory
                .decodeFile((RaconTours.PATH + RaconTours.city + File.separator
                        + TourDescription.currentTour.getObjtourName()
                        + File.separator + descriptList.get(position)
                        .getImagePath().split("/")[2]).replace(" ", "_")));

        if (position == 0) {
            SharedPreferences settings = context.getSharedPreferences("downloadDetails", 0);
            String isTourDownloaded = settings.getString(TourDescription.currentTour.getObjtourName(), "");
            if (isTourDownloaded.equals("true")) {
            //if (!(TourDescription.downloadFile.exists())||TourDescription.downloadFile.exists() == false ) {
            //if (TourDescription.currentTour.getIsTourDownloaded() == true) {
                //holder.pinButton.setVisibility(View.INVISIBLE);
                //holder.arrowButton.setVisibility(View.INVISIBLE);
                //holder.serialText.setVisibility(View.INVISIBLE);
                }
                holder.amountButton.setVisibility(View.VISIBLE);
                holder.amountButton.setText("Start");



            } else {
                File promoPlistPath = new File(RaconTours.PATH + "promocode.txt");
                checkPromoCode(promoPlistPath);
                if (discountedTourDetails != null) {
                    tourName = (String) discountedTourDetails.get("promoTour");
                    price = (String) discountedTourDetails.get("discountPrice");
                    prodId = (String) discountedTourDetails.get("disProId");

                    holder.amountButton.setVisibility(View.VISIBLE);
                    // Setting the background color
                    holder.title
                            .setBackgroundColor(Color.parseColor("#993333"));
                    // Setting the Title color
                    holder.title.setTextColor(Color.WHITE);
                    // Centering the title
                    holder.title.setGravity(Gravity.LEFT);
                    // setting the city
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setText(RaconTours.city);
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setVisibility(View.VISIBLE);
                    // setting the Tour Amount
                    holder.amountButton.setText("$" +price);
                    //promoPlistPath.delete();
                } else {

                    // Enabling the two buttons
                    holder.amountButton.setVisibility(View.VISIBLE);
                    // Setting the background color
                    holder.title
                            .setBackgroundColor(Color.parseColor("#993333"));
                    // Setting the Title color
                    holder.title.setTextColor(Color.WHITE);
                    // Centering the title
                    holder.title.setGravity(Gravity.LEFT);
                    // setting the city
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setText(RaconTours.city);
                    ((TextView) holder.rlayout.getChildAt(1))
                            .setVisibility(View.VISIBLE);
                    // setting the Tour Amount
                    holder.amountButton.setText(TourDescription.currentTour
                            .getObjPrice());

                }
            }
        } else {
            holder.amountButton.setVisibility(View.INVISIBLE);
            holder.pinButton.setVisibility(View.INVISIBLE);
            holder.arrowButton.setVisibility(View.INVISIBLE);
            holder.serialText.setVisibility(View.INVISIBLE);
            holder.title.setBackgroundColor(Color.WHITE);
            holder.title.setTextColor(Color.BLACK);
            holder.title.setGravity(Gravity.CENTER_HORIZONTAL);
            ((TextView) holder.rlayout.getChildAt(1))
                    .setVisibility(View.INVISIBLE);
        }
        return convertView;
    }


    @SuppressWarnings("unchecked")
    private void checkPromoCode(File promoPlistPath) {
        if (promoPlistPath.exists()) {
            try {

                ObjectInputStream inStream = new ObjectInputStream(
                        new FileInputStream(promoPlistPath));
                obj = (ArrayList<HashMap<String, Object>>) inStream
                        .readObject();
                for (HashMap<String, Object> tmpObj : obj) {
                    promoTourname = (String) tmpObj.get("promoTour");
                    if (promoTourname.equals(TourDescription.currentTour.getObjtourName())) {
                        discountedTourDetails = tmpObj;
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    class ViewHolder {
        Button pinButton;
        Button amountButton;
        RelativeLayout rlayout;
        TextView title;
        TextView desc;
        Button arrowButton;
        EditText serialText;

    }
}

Here

    prodId = (String) discountedTourDetails.get("disProId");

I want to pass prodId to related activity.

Note: Base Adapter is called from the activity

    adapter = new TourDescAdapter(currentTour.getListOfDescriptions(), this);

    setListAdapter(adapter);

Any one can tell me how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-12-18 09:35:25

你不能只使用String iGotTheString =adapter.prodId吗?

Couldn't you just use String iGotTheString = adapter.prodId?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文