在android中获取listview中的重复行

eqfvzcg8  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(346)

我正在处理一个从webservice填充数据的listview,我正在按照json中webservice的要求结果来完成,但是当我在listview的adapter中设置它时,它只给我重复的行,我还检查了我的arraylist,这是完美的。

public class ListingAdapter extends BaseAdapter {
    private Activity mContext;
    ArrayList<Bussiness> busList;

    public ListingAdapter(Activity c, ArrayList<Bussiness> busList) {
        mContext = c;
        this.busList = busList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return busList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();

        if (convertView == null) {

            grid = new View( mContext );
            grid = inflater.inflate( R.layout.raw_tab, null );
            TextView tv_cat = (TextView) grid.findViewById( R.id.tv_cat );
            ImageView iv_pic = (ImageView) grid.findViewById( R.id.iv_pic );
            TextView tv_features = (TextView) grid.findViewById( R.id.tv_features );
            TextView tv_location = (TextView) grid.findViewById( R.id.tv_location );
            TextView tv_rating = (TextView) grid.findViewById( R.id.tv_rating );
            RatingBar rt_bar = (RatingBar) grid.findViewById( R.id.rt_fvrt );
            TextView tv_votes = (TextView) grid.findViewById( R.id.tv_votes );
            ImageView iv_call = (ImageView) grid.findViewById( R.id.iv_call );
            ImageView iv_share = (ImageView) grid.findViewById( R.id.iv_share );

            tv_cat.setText( busList.get( position ).getBiz_name() );
            tv_features.setText( busList.get( position ).getFeatures() );
            tv_location.setText( busList.get( position ).getCity() );
            tv_rating.setText( busList.get( position ).getAverage_rate() );
            tv_votes.setText( busList.get( position ).getVotes() );
            rt_bar.setRating( Float.parseFloat( busList.get( position ).getAverage_rate() ) );
            if(!busList.get( position ).getImg_1().equals( "" )){
                Picasso.with( mContext )
                        .load( busList.get( position ).getImg_1().replaceAll(" ", "%20") )
                        .placeholder( R.drawable.ic_no_img )
                        .error( R.drawable.ic_no_img )
                        .into( iv_pic );
            }

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

                    Intent intent = new Intent( Intent.ACTION_CALL );

                    intent.setData( Uri.parse( "tel:" + (busList.get( position ).getMobile_1()) ) );
                    if (ActivityCompat.checkSelfPermission( mContext, Manifest.permission.CALL_PHONE ) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    mContext.startActivity( intent );
                }
            } );

            iv_share.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent sharingIntent = new Intent( Intent.ACTION_SEND );
                    sharingIntent.setType( "text/html" );
                    sharingIntent.putExtra( android.content.Intent.EXTRA_TEXT, Html.fromHtml( "TEst From all About City." ) );
                    mContext.startActivity( Intent.createChooser( sharingIntent, "Share using" ) );
                }
            } );
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}
e7arh2l6

e7arh2l61#

改变一下这个 getItemId() 在你的listingadapter类中,如下所示

public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
}

相关问题