简体   繁体   English

将基本适配器的变量值传递给活动

[英]Pass a variable value from Base Adapter to activity

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. 我想将prodId传递给相关活动。

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? 谁能告诉我该怎么做?

您是否只使用String iGotTheString = adapter.prodId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM