java - GridView setOnItemClickListener don't work -
am trying setonitemclicklistener
working, can not work. newbie android developer.
locationgalleryactivity.java
:
import java.util.arraylist; import java.util.vector; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.toast; public class locationgalleryactivity extends actionbaractivity { private gridview gridview; private gridviewadapter customgridadapter; private vector<photomodel> vphotos; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_location_gallery); //conectamos la base of operations de datos locationssqlite dbloc = new locationssqlite(this, "dblocations.db", null, 1); //obtenemos el vector de imagenes vphotos = dbloc.getphotos(); gridview = (gridview) findviewbyid(r.id.gridview); customgridadapter = new gridviewadapter(this, r.layout.row_grid_image, getdata()); gridview.setadapter(customgridadapter); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { log.i("gridview", "se ha pulsado"); /*intent intent = new intent(intent.action_view, uri.parse( "content://media/internal/images/media")); startactivity(intent); */ toast.maketext(getapplicationcontext(), position + "#selected", toast.length_short).show(); } }); } /** * obtenemos las imagenes para mostrar en el gridlayout * * @return array de imageitems (imagene y su nombre) */ private arraylist<imageitem> getdata() { final arraylist<imageitem> imageitems = new arraylist<imageitem>(); // vamos obteniendo las imagenes del array (int = 0; < vphotos.size(); i++) { photomodel p = vphotos.get(i); //obtenemos la miniatura de la imagen bitmap bitmap = bitmapfactory.decodefile(p.getpath() + ".thumbnails/" + p.getname()); imageitems.add(new imageitem(bitmap, p.getname())); } homecoming imageitems; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.location_gallery, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } }
activity_location_gallery.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantfocusability="blocksdescendants" tools:context=".mainactivity" > <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:columnwidth="100dp" android:drawselectorontop="true" android:gravity="center" android:numcolumns="auto_fit" android:stretchmode="columnwidth" android:verticalspacing="5dp" > </gridview> </relativelayout>
gridviewadapter.java:
import java.util.arraylist; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; public class gridviewadapter extends arrayadapter<imageitem> { private context context; private int layoutresourceid; private arraylist<imageitem> info = new arraylist<imageitem>(); public gridviewadapter(context context, int layoutresourceid, arraylist<imageitem> data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; viewholder holder = null; if (row == null) { layoutinflater inflater = ((activity) context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new viewholder(); holder.imagetitle = (textview) row.findviewbyid(r.id.text); holder.image = (imageview) row.findviewbyid(r.id.image); row.settag(holder); } else { holder = (viewholder) row.gettag(); } imageitem item = data.get(position); holder.imagetitle.settext(item.gettitle()); holder.image.setimagebitmap(item.getimage()); homecoming row; } static class viewholder { textview imagetitle; imageview image; } }
row_grid_image.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:orientation="vertical" android:padding="5dp" android:clickable="true" android:background="@drawable/grid_color_selector" android:focusable="true"> <imageview android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" > </imageview> <textview android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:gravity="center" android:textsize="12sp" > </textview> </linearlayout>
brought while , can not work.
try remove android:clickable="true" linearlayout in row_grid_image.xml
java android gridview
No comments:
Post a Comment