在gridview中看不到复选框

时间:2015-07-03 18:37:43

标签: android

我在gridviewer添加了一些复选框。但是它们不可见。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true" />

           <CheckBox android:id="@+id/itemCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="#ffedff38"
            android:backgroundTint="#ff46374b"
            android:clickable="true"
            android:visibility="visible"
            android:layout_weight="0.1"
            android:checked ="true"
            />

        <ImageButton
            android:id="@+id/imgbtn"
            android:onClick="VisualizzaInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff387dff"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:src="@drawable/info"
            android:alpha="0.7" />
    </RelativeLayout>

这是gridview的代码:在本节中,我将复选框添加到gridiview;但是复选框不可见。

    public class CustomGallery extends ActionBarActivity {
        private int count;
        private boolean[] thumbnailsselection;
        private String[] arrPath;
        private ImageAdapter imageAdapter;
        private Context mContext;
        private ArrayList<Integer> ordinate = new ArrayList<>();
        private int ID;
        private static int tot;
        private ArrayList<String> imageURLArray = new ArrayList<String>();
        private GridView imagegrid;
        private static boolean[] check = new boolean[]{false};

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            tot = 0;
            final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media._ID;
            Cursor imagecursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy);
            int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
            this.count = imagecursor.getCount();
            this.arrPath = new String[this.count];
            this.thumbnailsselection = new boolean[this.count];
            for (int i = 0; i < this.count; i++) {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
                arrPath[i]= imagecursor.getString(dataColumnIndex);
            }
            imageURLArray.clear();
            GridView imagegrid = (GridView) findViewById(R.id.gridview);
            try {
                imageAdapter = new ImageAdapter(this);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            imagegrid.setAdapter(imageAdapter);
        }

        public class ImageAdapter extends BaseAdapter {
            private Context mContext;
            private LayoutInflater mInflater;

            public ImageAdapter(Context mContext) throws JSONException {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                this.mContext = mContext;
                Collections.sort(SingletonImageCustomize.getIstance().getAllImage(), new DistanceComparator());
                String myString = "http://sistemitelematici.altervista.org/";
                Intent intent = getIntent();
                int id = intent.getIntExtra("userId", -1);
                if(isProfilato(id)) {
                    for (int i = 0; i < 3; i++) {
                        String s = myString + SingletonImageCustomize.getIstance().getAllImage().get(i).getURLWatson();
                        ordinate.add(SingletonImageCustomize.getIstance().getAllImage().get(i).getPlaceID());
                        imageURLArray.add(s);
                    }
                    for (int j = 3; j < SingletonImageCustomize.getIstance().getAllImage().size(); j++) {
                        String t = myString + SingletonImageCustomize.getIstance().getAllImage().get(j).getURL();
                        ordinate.add(SingletonImageCustomize.getIstance().getAllImage().get(j).getPlaceID());
                        imageURLArray.add(t);
                    }
                }else {
                    for (int i = 0; i < SingletonImageCustomize.getIstance().getAllImage().size(); i++) {
                        String s = myString + SingletonImageCustomize.getIstance().getAllImage().get(i).getURL();
                        ordinate.add(SingletonImageCustomize.getIstance().getAllImage().get(i).getPlaceID());
                        imageURLArray.add(s);
                    }
                }
            }

            public int getCount() {
                return imageURLArray.size();
            }

            public Object getItem(int position) {
                return position;
            }

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

            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(
                            R.layout.galleryitem, null);
                    holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                    //holder.watsonImage = (ImageView) convertView.findViewById(R.id.WImage);
                    holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                    holder.btn = (ImageButton) convertView.findViewById(R.id.imgbtn);
                    convertView.setTag(holder);
                }
                else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.checkbox.setId(position);
                holder.imageview.setId(position);
                //holder.watsonImage.setId(position);
                holder.btn.setId(position);

                holder.checkbox.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        CheckBox cb = (CheckBox) v;
                        int id = cb.getId();
                        if (thumbnailsselection[id]) {
                            cb.setChecked(false);
                            thumbnailsselection[id] = false;
                            ID = SingletonImageCustomize.getIstance().getImgID().get(id);
                            SingletonImageCustomize.getIstance().removeIDMappa(ID);
                            tot--;
                        } else {
                            cb.setChecked(true);
                            thumbnailsselection[id] = true;
                            ID = ordinate.get(id);
                            SingletonImageCustomize.getIstance().addIDMappa(ID);
                            tot++;
                        }
                    }
                });
                holder.btn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ImageButton imgBtn = (ImageButton) v;
                        int id = imgBtn.getId();
                        VisualizzaInfo(v,id);
                    }
                });
                Picasso.with(mContext).load(imageURLArray.get(position)).into(holder.imageview);
                holder.checkbox.setChecked(thumbnailsselection[position]);
                holder.id = position;
                position--;
                return convertView;
            }
        }

        class ViewHolder {
            ImageView imageview;
            //ImageView watsonImage;
            CheckBox checkbox;
            ImageButton btn;
            int id;
        }

        public boolean isProfilato(int userID) throws JSONException {
            boolean profilato = false;
            HTTPRequest httpRequest = new HTTPRequest();
            Log.i("ID", String.valueOf(userID));
            httpRequest.execute("estraiProfilo", "http://sistemitelematici.altervista.org/Php/GetProfilato.php", String.valueOf(userID));
            jSonParser js = null;
            JSONArray httpResponse = null;
            try {
                httpResponse = httpRequest.get();
                js = new jSonParser(httpResponse);
            } catch (InterruptedException e) {
                //Toast.makeText(MyCustomAdapter.this, "La richiesta non � stata portata a termine, per favore riprovare", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            } catch (ExecutionException e) {
                //Toast.makeText(MyCustomAdapter.this, "La richiesta non � stata portata a termine, per favore riprovare", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            } catch (NullPointerException e) {
                //Toast.makeText(MyCustomAdapter.this, "La richiesta non � stata portata a termine, per favore riprovare", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            String parser = js.getUserName("profilato");
            if(parser.toString().equals("1"))
                profilato = true;
            return profilato;
        }

        public void VisualizzaItinerario(View view){
            if(tot == 0 )
                Toast.makeText(CustomGallery.this, "Selezionare almeno una destinazione!", Toast.LENGTH_SHORT).show();
            if(tot <= 8 && tot > 0) {
                Intent intent = new Intent(this, MappaItinerario.class);
                Intent i = getIntent();
                int id = i.getIntExtra("userId", -1);
                intent.putExtra("userId", id);
                imageURLArray.clear();
                startActivity(intent);
            }else {
                Toast.makeText(CustomGallery.this, "E' possibile inserire al più 8 destinazioni!", Toast.LENGTH_SHORT).show();
            }
        }
    public void VisualizzaInfo(View v,int id){
        Intent intent = new Intent(this, Info.class);
        Intent i = getIntent();
        int key = ordinate.get(id);
        int idUser = i.getIntExtra("userId", -1);
        intent.putExtra("userId",idUser);
        intent.putExtra("imageID",key);
        startActivity(intent);
    }
} 

我该如何解决?

1 个答案:

答案 0 :(得分:0)

        holder.checkbox.bringToFront();

在适配器中设置imageview的值后尝试此行。我认为这不会解决整个问题,但至少您的复选框将是可见的。如果您找到了更好的答案,请发布。这将是有帮助的

    android:buttonTint="@color/white"
    android:background="@color/blue"

在xml的复选框属性中添加这两行,希望这会有所帮助。