在Android中单击更改Listview项目图像

时间:2014-11-18 05:30:49

标签: android listview android-lazyloading

我想在点击nect和prev按钮时更改特定列表视图图像,但在单击下一个和上一个按钮时更改所有列表视图图像

public class ListAdapter extends ArrayAdapter<String>{

    Context context;
     String imgurl;
     String[] imageUrls;
     int i=1;
     ViewHolder holder = null;

    ArrayList<String> strimgview,strstreetname,strLocation,strSquarefootage,strPrice,strPosted,strURL,strId;
     ImageLoader imageLoader = ImageLoader.getInstance();
     DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                    .cacheOnDisc(true).resetViewBeforeLoading(true).build();
    public ListAdapter(Context context,ArrayList<String> strimgview, ArrayList<String> strstreetname, 
            ArrayList<String> strLocation,ArrayList<String> strSquarefootage,
            ArrayList<String> strPrice,ArrayList<String> strPosted,
            ArrayList<String> strId,ArrayList<String> strURL)
    {
        super(context, R.layout.propertyrow,strstreetname);
        this.context= context;
        this.strimgview= strimgview;
        this.strstreetname= strstreetname;
        this.strLocation= strLocation;
        this.strSquarefootage= strSquarefootage;
        this.strPrice= strPrice;
        this.strPosted= strPosted;
        this.strURL= strURL;


    }
     public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View row = convertView;

            if (row == null) {
               row=mInflater.inflate(R.layout.propertyrow, parent,false);

           holder = new ViewHolder();
           holder.imgProperty =(ImageView) row.findViewById(R.id.npTupleImage);

           holder.txtStreetName= (TextView) row.findViewById(R.id.npTupleProjectName);
           holder.txtLocation= (TextView) row.findViewById(R.id.npTupleBuilder);
           holder.txtSquareFotage= (TextView) row.findViewById(R.id.npTupleProjectAddress);
           holder.txtPrice =(TextView) row.findViewById(R.id.npTupleProjectDistance);
           holder.txtprev =(TextView) row.findViewById(R.id.prev);
           holder.txtnext =(TextView) row.findViewById(R.id.next);
         //txtprev.setVisibility(View.INVISIBLE);
         //txtnext.setVisibility(View.INVISIBLE);
           holder.imglike= (ImageView) row.findViewById(R.id.unsavebtn);
           holder.imglike.setVisibility(View.INVISIBLE);
            row.setTag(holder);
            }
            else {
                holder = (ViewHolder) row.getTag();
            }
        // txtposted.setText("EnteredDate :"+strPosted.get(position));
           holder.txtStreetName.setText("Street Name :"+strstreetname.get(position));
           holder.txtLocation.setText("Location :"+strLocation.get(position));
           holder.txtSquareFotage.setText("SquareFootage :"+strSquarefootage.get(position));
           holder.txtPrice.setText("Price :$"+strPrice.get(position));
           holder.imgProperty.setTag(position);
         // Log.d("Img",strimgview.get(position));
         try{
            // ArrayList<String> imgurl=new ArrayList<String>();
              imgurl= ""+strimgview.get(position);
              imageUrls=imgurl.split("\\|\\|");
              for(int j=0;j<imageUrls.length-1;j++)
                  imageLoader.displayImage(imageUrls[i], holder.imgProperty, options);

         }
         catch(Exception e)
         {
             Toast.makeText(context, "Load"+e, Toast.LENGTH_LONG).show();
         }

         holder.imglike.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(context, "Want to save", Toast.LENGTH_LONG).show();

            }
        });

         holder.txtprev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                try{

                  if(i!=1)
                  {
                      i--;
                      imageLoader.displayImage(imageUrls[i], holder.imgProperty, options);
                  Log.d("imageUrlnew ",imageUrls[i]);

                  }


                 }
                 catch(Exception e)
                 {
                    // Toast.makeText(context, "next"+e, Toast.LENGTH_LONG).show();

                 }

                 notifyDataSetChanged();
            }
        });

         holder.txtnext.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     try{


                          if((i<imageUrls.length-1))
                          {
                              i++;
                              imageLoader.displayImage(imageUrls[i], holder.imgProperty, options);

                          Log.d("imageUrlnew ",imageUrls[i]);

                          }
                         }
                         catch(Exception e)
                         {
                             // Toast.makeText(context, "prev"+e, Toast.LENGTH_LONG).show();
                         }
                     notifyDataSetChanged();
                }
            });

         return row;


     }

      class ViewHolder {
            ImageView imgProperty;
            TextView txtprev,txtnext;
            TextView txtStreetName;
            TextView txtLocation;
            TextView txtSquareFotage;
            TextView txtPrice;
            ImageView imglike;
            ProgressBar progressBar;
      }

}

请帮我解决这个问题

提前致谢

enter image description here

1 个答案:

答案 0 :(得分:0)

如果按照您的要求工作,这不是一个完美的解决方案。然后更新解决方案,无需切换。

lstviewHomeList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                switch (position) {
                case 0:
                    Button btnprev = (Button ) arg1.findViewById(R.id.button_id);
                      //same for image and any widgetn your adapter layout xml
                    btnprev.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            //do what u want

                        }
                    });

                    break;

                default:
                    break;
                }
            }

        });