0

我正在制作通过json解析填充数据的listview,我在getview中使用Customadapter我根据条件将textviews设置到listview中。我的要求是,当我单击listview的行时,我可以检索listview的单击行的项目的id ,但问题是我无法获取项目的 ID。所以请任何人帮助我,我处于危险区域我正在使用此代码 -

public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub

        final MyVoucherOffer voucherOffer = (MyVoucherOffer) voucherListAdapter
                .get(position); // Remove offer

        LayoutInflater inflater = (LayoutInflater) VoucherActiveScreen.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        getIs_bidding = voucherOffer.getIs_bidding();
        Log.i("==getIs_bidding==", getIs_bidding);

        getResale_price = voucherOffer.getResale_price();
        Log.i("==getResale_price==", getResale_price);
        /*if (listType == 3) {
            convertView = inflater.inflate(R.layout.voucher_inactive_list,
                    null);
        } else if(listType == 1){
            convertView = inflater.inflate(R.layout.voucher_active_list,
                    null);
        }else if(listType == 2){
            convertView = inflater.inflate(R.layout.bidding_list, null);
        }*/

        //Code Added

        if (listType == 3) {
            convertView = inflater.inflate(R.layout.voucher_inactive_list,
                    null);
        } else{
            convertView = inflater.inflate(R.layout.voucher_active_list,
                    null);
        }

        //
        // 1. offerTitle-
        // 2. offerPricce
        // 3. star
        // 4. offerDiscription
        // 5. condition
        // 6. number
        ProgressBar progress = (ProgressBar) convertView
        .findViewById(R.id.progressBar1);


        RelativeLayout mylayout = (RelativeLayout) convertView
                .findViewById(R.id.mylayoutV);
        ImageView dealImage1 = (ImageView) convertView
                .findViewById(R.id.dealImageV);
        ImageView birds = (ImageView) convertView
        .findViewById(R.id.birds);


        int z = position % 2;
        if (z == 1) {
            mylayout.setBackgroundResource(R.drawable.dark_gray);
        }

        ArrayList<String> al = voucherOffer.getImageOffer();
        if (al.size() > 0) {
            String url = al.get(0).toString();
            url = "http://bma.in/ian_watt/application/views/scripts/vendor/uploads/"
                    + url;

            try {

                dealImage1.setTag(url);
                imageloader.DisplayImageLoader(url, VoucherActiveScreen.this,
                        dealImage1,progress);

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("error", e.getMessage().toString());
            }
        } else {
            dealImage1.setImageResource(R.drawable.no_img);
        }
        ImageButton arrow = (ImageButton) convertView
                .findViewById(R.id.list_arrowV);
        TextView offerTitle = (TextView) convertView
                .findViewById(R.id.offerTitleV);
        offerTitle.setText(voucherOffer.getFld_name());
        TextView offerPrice = (TextView) convertView
                .findViewById(R.id.offerPriceV);

        offerTitle.setTypeface(font_bold);

        // offerTitle.setText(localMapOffer.getOffer_price()));number
        Log.v("", voucherOffer.getOffer_price());


        try {
            double op = Double.parseDouble(voucherOffer.getOffer_price());

            double cp = Double
                    .parseDouble(voucherOffer.getCustomer_price());

            if (listType == 3)
            {
                if (voucherOffer.getIs_redeem().toString().equals("1")
                        || voucherOffer.getIs_redeem().toString() == "1") {
                    offerPrice.setTypeface(font_bold);
                    offerPrice.setText("Rate");
                } else {
                    offerPrice.setTypeface(font);
                    offerPrice.setText("View");
                }
            } else {
                if (op - cp == 0.0) {
                    offerPrice.setText("Free");
                } else {
                    offerPrice.setText("$" + voucherOffer.getOffer_price());
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("error", e.getMessage().toString());
        }

        if (listType == 3)
        {
            TextView star = (TextView) convertView
                    .findViewById(R.id.starVText);
            star.setTypeface(font_bold);
            Log.e("!!!!!!!!!! VoucherActive", " V :: "
                    + voucherOffer.getIs_redeem().toString());
            if (voucherOffer.getIs_redeem().toString().equals("1")
                    || voucherOffer.getIs_redeem().toString() == "1") {
                star.setText("Redeemed "
                        + getStringDate(voucherOffer.getModified()
                                .toString()));
            } else if (voucherOffer.getIs_sold().toString().equals("1")
                    || voucherOffer.getIs_sold().toString() == "1") {
                star.setText("Sold "
                        + getStringDate(voucherOffer.getModified()
                                .toString()));
            } else if (voucherOffer.getIs_expired().toString().equals("1")
                    || voucherOffer.getIs_expired().toString() == "1") {
                star.setText("Expired "
                        + getStringDate(voucherOffer.getModified()
                                .toString()));
            }

        } else {

            if(voucherOffer.getIs_bidding().equalsIgnoreCase("1"))
                birds.setVisibility(View.VISIBLE);
            ImageView star = (ImageView) convertView
                    .findViewById(R.id.starV);
            hm = Data.getValue(hm);
            Log.v(",val", voucherOffer.getFld_rating() + "");
            if (hm.containsKey(voucherOffer.getFld_rating())) {
                star.setImageResource(hm.get(voucherOffer.getFld_rating()));

            }
            /*else if(voucherOffer.getIs_bidding().equalsIgnoreCase("0") && voucherOffer.getResale_price().equalsIgnoreCase("0.00")){

            }*/
        }
        TextView offerDiscription = (TextView) convertView
                .findViewById(R.id.discriptionV);
        offerDiscription.setText(voucherOffer.getOffer_title());

        offerDiscription.setTypeface(font);

        TextView dayV = (TextView) convertView.findViewById(R.id.dayV);

        TextView distance = (TextView) convertView
                .findViewById(R.id.distacneV);

        if (listType == 3) {
            if (voucherOffer.getIs_redeem().toString().equals("1")
                    || voucherOffer.getIs_redeem().toString() == "1") {
                distance.setTypeface(font_bold);
                distance.setText("This");
            } else {
                distance.setTypeface(font);
                distance.setText("Voucher");
            }
        } else {
            distance.setTypeface(font);
            if (voucherOffer.getDistance().length() > 5) {
                distance.setText(voucherOffer.getDistance().substring(0, 4)
                        + "m");

            } else {
                distance.setText(voucherOffer.getDistance() + "m");
            }
        }

        try {

            DateFormat df = new SimpleDateFormat("MM/dd/yy");

            Date today = df.parse(voucherOffer.getOffer_enddate());
            Log.v("today", today.toString());
            long endDate = today.getTime();
            Calendar currentDate = Calendar.getInstance();
            long current = currentDate.getTimeInMillis();

            Log.v("current milis", "" + current);
            long cal = endDate - current;

            if (listType == 3) {
                if (voucherOffer.getIs_redeem().toString().equals("1")
                        || voucherOffer.getIs_redeem().toString() == "1") {
                    dayV.setTypeface(font_bold);
                    dayV.setText("Deal");
                } else {
                    dayV.setTypeface(font);
                    dayV.setText("Page");
                }
            } else {
                dayV.setTypeface(font);
                dayV.setText(getDay(cal));
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        mylayout.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                try {



                  ---------------------my problem section----------------------------



                    if (listType == 1) {
                        Log.v("active", "On Click " + (position));
                        if(Integer.parseInt(ParsingData.is_bid) == 1 && Integer.parseInt(ParsingData.is_resale) == 0){
                             System.out.println("inside first case");
                            Intent intent = new Intent(
                                    VoucherActiveScreen.this,
                                    MyVoucherDetailPage.class);
                            intent.putExtra("selectedIndex", position);
                            intent.putExtra("listType", listType);
                            intent.putExtra("isPurchasedVoucher ", false);
                            startActivity(intent);
                        }
                        else{
                            System.out.println("inside else case");
                            Intent intent = new Intent(
                                    VoucherActiveScreen.this,
                                    MyVoucherDetailPage.class);
                            intent.putExtra("selectedIndex", position);
                            intent.putExtra("isPurchasedVoucher ", true);
                            intent.putExtra("listType", listType);
                            startActivity(intent);
                ------------------------end----------------------------
                        }
                    }
                    //Code Added
                    /*else if(listType == 2){
                        Log.v("Bidding", "On Click " + (position));
                        Intent intent = new Intent(VoucherActiveScreen.this, 
                                MyVoucherDetailPage.class);
                        intent.putExtra("selectedIndex", position);
                        intent.putExtra("listType", listType);
                        startActivity(intent);
                    }*/else if (listType == 3) {
                        Intent intent = new Intent(
                                VoucherActiveScreen.this,
                                MyVoucherDetailPage.class);
                        intent.putExtra("listType", listType);
                        intent.putExtra("selectedIndex", position);
                        Log.v("inactive", "On Click " + (position));

                        startActivity(intent);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });

        return convertView;
    }
4

2 回答 2

0

如果您尝试在用户单击 ListView 的一行时注册一个事件,您应该使用onItemClickListener而不是 OnClickListener。View 参数是被单击的行。您只需执行 view.getId() 即可找到被点击的行的 id。

于 2012-04-17T15:10:44.713 回答
0

您可以通过在设置文本的位置设置 id 来做到这一点。您可以通过设置您的 id setId(id),然后按照 Bobbake4 所说,您可以使用它来获取它view.getID()

于 2012-04-17T18:43:40.383 回答