简体   繁体   中英

Display images on listView without scrolling?

I used GridView to display images of types of batteries. When user clicks any image, the features of that particular battery will get print on next page. The images and features are fetching from the server. The problem is the first battery image is displaying when I opened the GridView , but the other battery images are displaying in that GridView only after I scroll the screen two to three times. But I want to display all the images once I opened the GridView .

private void getDatasFromIntent() {
    alHM = new ArrayList<>();
    Intent intent = getIntent();
    String typeResp = intent.getStringExtra("IMG_S");
    try {
        JSONObject jsonObject = new JSONObject(typeResp);
        JSONArray jsonArray = jsonObject.getJSONArray("process");
        for (int i = 0; i < jsonArray.length(); i++) {
            HashMap<String, String> hm = new HashMap<>();
            JSONObject bat_json = jsonArray.getJSONObject(i);
            String battery_featues_id = bat_json.getString("battery_featues_id");
            String battery = bat_json.getString("battery_type");
            String battery_image = bat_json.getString("battery_image");
            battery_image = battery_image.replace("\\", "");
            hm.put("battery_featues_id", battery_featues_id);
            hm.put("battery_type", battery);
            hm.put("battery_image", battery_image);
            alHM.add(hm);
        }

        // prepared arraylist and passed it to the Adapter class
        mAdapter = new GridviewAdapter(this, alHM);

        // Set custom adapter to gridview
        GridView gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                Toast.makeText(SampleBatteryList.this, "a: " + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();

            }
        });

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
 public class GridviewAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, String>> list;
    private final SampleBatteryList activity;

    public GridviewAdapter(SampleBatteryList sampleBatteryList, 
   ArrayList<HashMap<String, String>> alHM) {
        this.activity = sampleBatteryList;
        this.list = alHM;
        Log.d("VOLLY", "ADP :" + alHM);
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }

    public class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
        public Button button;
        ViewHolder view;
    }
    @Override
    public View getView(int i, View contentView, ViewGroup viewGroup) {
        Log.d("VOLLY", "INT : " + i);
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if (contentView == null) {
            view = new ViewHolder();
            contentView = inflator.inflate(R.layout.grid_content_sub, null);
            view.txtViewTitle = (TextView)   
            contentView.findViewById(R.id.tv_battery_type);
            view.imgViewFlag = (ImageView)    
            contentView.findViewById(R.id.img_battery);
            view.button = (Button) 
          contentView.findViewById(R.id.btn_card_type);
            contentView.setTag(view);
        } else {
            view = (ViewHolder) contentView.getTag();
            view.txtViewTitle.setText(list.get(i).get("battery_type"));
             view.imgViewFlag.setImageResource(R.drawable.branded_logo);
             view.imgViewFlag.setImageDrawable(null);
            Picasso.with(SampleBatteryList.this)
                    .load(Links._img + list.get(i).get("battery_image"))
                    .fit().centerCrop()
                    .into(view.imgViewFlag);
                  final int ii = i;
                final Button btn = view.button;
               view.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   btn.setText(list.get(ii).get("battery_type"));
                    btn.setSingleLine(true);

                   YoYo.with(Techniques.TakingOff).duration(2000).playOn(btn);
                    showDialog();
               Log.d("VOLLY", "id :" +list.get(ii).get("battery_featues_id"));
                 callVollyForFeature(list.get(ii).get("battery_featues_id"));
                }
            });
        }

        return contentView;
    }
}

here you are using view holder class but after initialization part and assign value part are in if and else so value not showing.

  @Override
    public View getView(int i, View contentView, ViewGroup viewGroup) {
        Log.d("VOLLY", "INT : " + i);
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if (contentView == null) {
            view = new ViewHolder();
            contentView = inflator.inflate(R.layout.grid_content_sub, null);
            view.txtViewTitle = (TextView)   
            contentView.findViewById(R.id.tv_battery_type);
            view.imgViewFlag = (ImageView)    
            contentView.findViewById(R.id.img_battery);
            view.button = (Button) 
          contentView.findViewById(R.id.btn_card_type);
            contentView.setTag(view);
        } else {
            view = (ViewHolder) contentView.getTag();
            view.txtViewTitle.setText(list.get(i).get("battery_type"));
             view.imgViewFlag.setImageResource(R.drawable.branded_logo);
             view.imgViewFlag.setImageDrawable(null);
            Picasso.with(SampleBatteryList.this)
                    .load(Links._img + list.get(i).get("battery_image"))
                    .fit().centerCrop()
                    .into(view.imgViewFlag);
                  final int ii = i;
                final Button btn = view.button;
               view.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   btn.setText(list.get(ii).get("battery_type"));
                    btn.setSingleLine(true);

                   YoYo.with(Techniques.TakingOff).duration(2000).playOn(btn);
                    showDialog();
               Log.d("VOLLY", "id :" +list.get(ii).get("battery_featues_id"));
                 callVollyForFeature(list.get(ii).get("battery_featues_id"));
                }
            });
        }

        return contentView;
    }

change to

  @Override
    public View getView(int i, View contentView, ViewGroup viewGroup) {
        Log.d("VOLLY", "INT : " + i);
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if (contentView == null) {
            view = new ViewHolder();
            contentView = inflator.inflate(R.layout.grid_content_sub, null);
            view.txtViewTitle = (TextView)   
            contentView.findViewById(R.id.tv_battery_type);
            view.imgViewFlag = (ImageView)    
            contentView.findViewById(R.id.img_battery);
            view.button = (Button) 
          contentView.findViewById(R.id.btn_card_type);
            contentView.setTag(view);
        }else{
            view = (ViewHolder) contentView.getTag();
}
            view.txtViewTitle.setText(list.get(i).get("battery_type"));
             view.imgViewFlag.setImageResource(R.drawable.branded_logo);
             view.imgViewFlag.setImageDrawable(null);
            Picasso.with(SampleBatteryList.this)
                    .load(Links._img + list.get(i).get("battery_image"))
                    .fit().centerCrop()
                    .into(view.imgViewFlag);
                  final int ii = i;
                final Button btn = view.button;
               view.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   btn.setText(list.get(ii).get("battery_type"));
                    btn.setSingleLine(true);

                   YoYo.with(Techniques.TakingOff).duration(2000).playOn(btn);
                    showDialog();
               Log.d("VOLLY", "id :" +list.get(ii).get("battery_featues_id"));
                 callVollyForFeature(list.get(ii).get("battery_featues_id"));
                }
            });


        return contentView;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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