自定义适配器并单击ListView中的项目

时间:2015-02-16 02:04:37

标签: android

我在ListView中的项目中单击图像时遇到问题。当我点击我想要显示已经消失的布局。这是我的代码:

public class MyActualOffers extends BaseActivity {
    private IResponse iResponse = null;
    private ListView actualOffersList;
    private ActualOffersListAdapter actualOffersAdapter;
    private ArrayList<Offers> actualOffersFromJson;

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

        actualOffersFromJson = new ArrayList<Offers>();
        actualOffersList = (ListView) findViewById(R.id.actualOffersList);


        iResponse = new IResponse() {
            @Override
            public void onResponse(JSONObject jObject) {
//                messageUser = new User();
//                messageUser.parseJsonResponse(jObject);
//                usersMessages.add(messageUser);
//                new DownloadImageTask(profileP)
//                        .execute("http://"+messageUser.getAvatarURL());
//
//                messagesListView.refreshDrawableState();
//                adapter.notifyDataSetChanged();
//                adapter.notifyDataSetInvalidated();
            }

            @Override
            public void onResponse(JSONArray jArray) {
                try {
                    for (int i = 0; i < 3; i++) {
                        JSONObject messageJSON = (JSONObject)jArray.get(i);
                        Offers offer = new Offers();

                        offer.parseJsonResponse(messageJSON);
                        actualOffersFromJson.add(offer);
                    }

                }catch (JSONException e) {
                    e.printStackTrace();
                }


                    actualOffersAdapter = new ActualOffersListAdapter(getApplicationContext(), actualOffersFromJson);
                    actualOffersList.setAdapter(actualOffersAdapter);


//                adapter = new MessagesListAdapter(getApplicationContext(), myMessagesList, usersMessages);
//                messagesListView.setAdapter(adapter);
            }

            @Override
            public void onError(JSONObject jObject) {
                ErrorObject error = new ErrorObject();
                error.parseJsonResponse(jObject);
                error.showErrorDialog(MyActualOffers.this);
            }
        };

        getActualMyOffers();
    }

    private void getActualMyOffers(){
        SharedPreferencesData sharedData = new SharedPreferencesData();
        String url = ConnectionParams.URL_OFFERS_FETCH_CURRENT_USER_OFFERS.replace("{0}", ""+"get");
        String token = "?auth_token="+sharedData.getTokenData(getBaseContext());
        connection.get(url + token + "&state=active", iResponse);
    }

    private class ActualOffersListAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<Offers> offersList;
        private LayoutInflater inflater;
        private ViewHolder holder;

        boolean isExpand = false;
        public ActualOffersListAdapter(Context context, ArrayList<Offers> offersList) {
            this.context = context;
            this.offersList = offersList;
            inflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

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

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

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

            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.actual_offers_list_item, null);
                holder = new ViewHolder();
                holder.cities = (TextViewWithImage) convertView.findViewById(R.id.actualOffersFromCityTXT);
                holder.date = (TextView) convertView.findViewById(R.id.actualOffersCalendarTXT);
                holder.type = (TextView) convertView.findViewById(R.id.actualOffersTripTypeTXT);
                holder.shipment = (TextView) convertView.findViewById(R.id.actualOffersShipmentTypeTXT);
                holder.transport = (TextView) convertView.findViewById(R.id.actualOffersTransportTypeTXT);
                holder.deleteOffer = (TextView) convertView.findViewById(R.id.actualOffersDeleteTXT);
                holder.editOffer = (TextView) convertView.findViewById(R.id.actualOffersEditOfferTXT);
                holder.addSimilar = (TextView) convertView.findViewById(R.id.actualOffersAddSimilarTXT);
                holder.addOpposite = (TextView) convertView.findViewById(R.id.actualOffersAddOppositeTXT);
                holder.infoIcon = (ImageView) convertView.findViewById(R.id.actualOffersInfoICON);
                holder.shipmentIcon = (ImageView) convertView.findViewById(R.id.actualOffersShipmentTypeICON);
                holder.transportIcon = (ImageView) convertView.findViewById(R.id.actualOffersTransportTypeICON);
                holder.typeIcon = (ImageView) convertView.findViewById(R.id.actualOffersTripTypeICON);
                holder.expandIcon = (ImageView) convertView.findViewById(R.id.expandIcon);
                holder.actualOffersExpandLayout = (RelativeLayout) convertView.findViewById(R.id.actualOffersExpandLayout);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.cities.setText(offersList.get(position).getStart() + " [img src=ic_in_text_arrow/] " + offersList.get(position).getDestination());
            holder.date.setText(offersList.get(position).getCreatedAt());

            if(offersList.get(position).getFrequency().equals("once")){
                holder.typeIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_in_text_once));
            }else{
                holder.typeIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_in_text_infinity));
            }
            holder.type.setText(offersList.get(position).getType());

            holder.shipmentIcon.setImageDrawable(ShipmentTypeEnum.setIconForShipmentType(ShipmentTypeEnum.fromString(offersList.get(position).getShipmentType()), getApplicationContext()));

            holder.transportIcon.setImageDrawable(TransportTypeEnum.setIconForTransportType(TransportTypeEnum.fromString(offersList.get(position).getTransportType()), getApplicationContext()));

            holder.deleteOffer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.editOffer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.addSimilar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.addOpposite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            holder.expandIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(isExpand){
                        holder.actualOffersExpandLayout.setVisibility(View.INVISIBLE);
                        isExpand = false;
                    }else{
                        holder.actualOffersExpandLayout.setVisibility(View.VISIBLE);
                        isExpand = true;
                    }

                }
            });

            holder.infoIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            return convertView;
        }
    }

    public static class ViewHolder {
        public TextViewWithImage cities;
        public TextView date;
        public TextView type;
        public TextView shipment;
        public TextView transport;
        public TextView deleteOffer;
        public TextView editOffer;
        public TextView addSimilar;
        public TextView addOpposite;
        public ImageView infoIcon;
        public ImageView shipmentIcon;
        public ImageView transportIcon;
        public ImageView typeIcon;
        public ImageView expandIcon;
        public RelativeLayout actualOffersExpandLayout;

    }
}

否,当我点击第一项中的holder.expandIcon时,展开列表最后一项中的布局显示。如何使用我的项目创建列表并正确地点击每个项目中的图像?

1 个答案:

答案 0 :(得分:1)

您正在扩展列表中的最后一项,因为这是getView检索的最后一个视图。现在编写代码的方式只有一个isExpand值。

因此,您需要(a)在ViewHolder上添加isExpand,以及(b)使用附加到事件处理程序中传递的View的ViewHolder。像这样:

        holder.expandIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder vh = (ViewHolder)v.getTag();
                if(vh.isExpand){
                    vh.actualOffersExpandLayout.setVisibility(View.INVISIBLE);
                    vh.isExpand = false;
                }else{
                    vh.actualOffersExpandLayout.setVisibility(View.VISIBLE);
                    vh.isExpand = true;
                }

            }
        });