2014-12-19 182 views
1

我实现了一个gridview,在其中填充一个“产品”数组以及另一个“产品图像”数组以填充Sqlite数据库中的产品的网格视图列表。但是,当我设置OnItemClickListener时,在我的设备上运行应用程序时,gridview项目不可点击。它只是能够向下滚动产品和图像,但不能点击。Android Eclipse:GridView项目不可点击?

该应用程序运行良好,图像与产品详细信息一起显示,但每个网格项目都不可点击。为什么会发生这种情况?

这里是我的代码:

MainActivity类别

gridView = (GridView)findViewById(R.id.grid); 
      ProductGridInflator bA = new ProductGridInflator(this, R.layout.home ,dbProducts, dbImages); 
      gridView.setAdapter(bA); 

      gridView.setOnItemClickListener(new OnItemClickListener() { 


       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

        Product currentProduct = new Product(); 

        String product_name=null; 

        for(int i=0;i<dbProducts.size();i++) 
        { 
         product_name = dbProducts.get(i).NAME; 
        } 

      //Sharing product details with all windows 
        SharedPreferences pref = getApplicationContext().getSharedPreferences("AppProduct", 0); // 0 - for private mode 
        Editor editor = pref.edit(); 
        editor.putString("pname", product_name); 
        editor.commit(); 

        Intent inn1=getIntent(); 
        inn1=new Intent(HomeActivity.this, ProductSingle_Activity.class); 
        startActivity(inn1); 

       } 
      }); 

单个网格项目的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/feed_bg" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/grid_text" 
        android:layout_width="285dp" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 
        android:textSize="18sp" /> 

       <TextView 
        android:id="@+id/more_details" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="02:12pm" 
        android:textColor="@color/timestamp" 
        android:textSize="@dimen/feed_item_timestamp" /> 

       <TextView 
        android:id="@+id/grid_desc" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 
        android:textColor="@color/timestamp" 
        android:textSize="15sp" /> 
      </LinearLayout> 
     </LinearLayout> 

     <HorizontalScrollView 
      android:id="@+id/slider" 
      android:layout_width="match_parent" 
      android:layout_height="282dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_weight="0.55" > 

      <ImageView 
       android:id="@+id/grid_image" 
       android:layout_width="285dp" 
       android:layout_height="285dp" /> 
     </HorizontalScrollView> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/interaction_text" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Likes: 34 Comments: 45" 
        android:textColor="@color/timestamp" 
        android:textSize="@dimen/feed_item_timestamp"/> 

      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

的GridView适配器

public class ProductGridInflator extends ArrayAdapter<Product>{ 
    private int[] Imageid; // this is the image id you are using... this is not initiatiliehs 
    ArrayList<Product> dbProducts; // your person arraylist 
    Context ctx; // the activity context 
    int resource; // this will be your xml file 
    ArrayList<Image> urlArr = new ArrayList<Image>(); 
    ArrayList<Image> dbImages = new ArrayList<Image>(); 

    public ProductGridInflator(Context context, int resource,ArrayList<Product> objects, ArrayList<Image> obj) { 
     super(context, resource, objects); 
     // TODO Auto-generated constructor stub 
     this.dbProducts = objects; 
     this.ctx = context; 
     this.resource = resource; 
     this.dbImages = obj; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     if(dbProducts.size() == 0){ 
      return 0; 
     }else{ 
      return dbProducts.size(); 
     } 
    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View child = convertView; 
     RecordHolder holder; 
     LayoutInflater inflater = ((Activity) ctx).getLayoutInflater(); // inflating your xml layout 


     if (child == null) {    
      child = inflater.inflate(R.layout.grid_single, parent, false); 
      holder = new RecordHolder(); 
      holder.productName = (TextView) child.findViewById(R.id.grid_text); // fname is the reference to a textview 
      //holder.productImage = (TextView) child.findViewById(R.id.txtLname); // in your xml layout file 
      //holder.bio =(TextView) child.findViewById(R.id.txtBio); // you are inflating.etc 
      holder.image = (ImageView) child.findViewById(R.id.grid_image); 
      holder.productDesc = (TextView) child.findViewById(R.id.grid_desc); 
      holder.moreDetails = (TextView) child.findViewById(R.id.more_details); 
      child.setTag(holder); 
     }else{ 
      holder = (RecordHolder) child.getTag(); 
     } 

     final Product user = dbProducts.get(position); // you can remove the final modifieer. 

     holder.productName.setText(user.getNAME());  
     //holder.lname.setText(user.getLName()); 
     //holder.bio.setText(user.getBiography()); 


     HorizontalScrollView scroller = (HorizontalScrollView) child.findViewById(R.id.slider); 
     for(int i=0;i<dbImages.size();i++) 
     { 
      if(dbImages.get(i).PID.equals(user.PID)) 
      { 
       //Picasso.with(ctx).load(dbImages.get(i).URL).into(holder.image); 
       Picasso.with(ctx) 
        .load(dbImages.get(i).URL) 
        .placeholder(R.drawable.loading_placeholder) 
        .error(R.drawable.error_placeholder) 
        .into(holder.image); 
      } 
      else 
       continue; 
     } 

     holder.productDesc.setText(user.DESC); 
     holder.moreDetails.setText(user.CATEGORY + " - " + "Rating: " + user.RATING + " - " + "Sizes: " + user.SIZE); 

     // the string as url and set it to your imageview.. 
     return child; 
    } 

    static class RecordHolder { 
     TextView productName; 
     ImageView image; 
     TextView productDesc; 
     TextView moreDetails; 
    } 

    @Override 
    public void notifyDataSetChanged() { // you can remove this.. 
     // TODO Auto-generated method stub  
     if(getCount() == 0){ 
      //show layout or something that notifies that no list is in.. 
     }else{ 
      // this is to make sure that you can call notifyDataSetChanged in any place and any thread 
      new Handler(getContext().getMainLooper()).post(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        ProductGridInflator.super.notifyDataSetChanged(); 
       } 
      }); 
     } 
    } 
} 

主要的GridView XML(窗口,GridView的所在地):

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="activity.classes.HomeActivity" > 

    <RelativeLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="8pt" 
      android:gravity="center_horizontal" 
      android:text="Jezza" 
      android:textSize="25dip" /> 

     <TextView 
      android:id="@+id/hub_error" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dip" 
      android:textColor="#e30000" 
      android:textStyle="bold" /> 

     <GridView 
      android:id="@+id/grid" 
      android:layout_width="fill_parent" 
      android:layout_height="395dp" 
      android:layout_alignParentBottom="true" 
      android:columnWidth="100dp" 
      android:gravity="center" 
      android:numColumns="1" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="10dp" /> 
    </RelativeLayout> 

    <ListView 
     android:id="@+id/drawerList" 
     android:background="#EEEEEE" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@null" 
     android:dividerHeight="0dp" /> 

</android.support.v4.widget.DrawerLayout> 

THANK YOU!

+0

'onItemClick'被开除了 ? – 2014-12-19 08:38:53

+0

@MagicalPhoenixϡ不,我不这么认为我删除了所有的代码并添加了一个Toast:“点击物品!”甚至没有去那里。没有错误。 Gridview只是一个'固体蔬菜':/ – Pjayness 2014-12-19 08:42:54

+0

为主Gridview显示xml – 2014-12-19 08:45:18

回答

0

尝试将内容添加到另一个布局,然后将该布局充气到网格视图。

public View getView(int position, View convertView, ViewGroup parent) { 

        View grid = convertView; 
        if (convertView == null) { 

        grid = inflater.inflate(R.layout.gridview_layout, parent, false); 
        } else { 
        grid = (View) convertView; 
      } 

     final Button bt = (Button) grid.findViewById(R.id.ready); 
     final TextView tno=(TextView)grid.findViewById(R.id.txt); 
     final TextView name=(TextView)grid.findViewById(R.id.txt3); 
     final TextView qty=(TextView)grid.findViewById(R.id.txt4); 
     final TextView instr=(TextView)grid.findViewById(R.id.txt5); 

     dish = data.get(position); 

     tno.setText(dish.get(KEY_TNO)); 
     name.setText("NAME: "+dish.get(KEY_NAME)); 
     qty.setText("QTY:"+dish.get(KEY_QUANTITY)); 
     instr.setText("INSTRN: "+dish.get(KEY_INSTRUCTION)); 


     bt.setOnClickListener(new View.OnClickListener() { 

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

       String tn = tno.getText().toString(); 
       System.out.println("TABLE NO:"+tn); 
       Toast.makeText(getApplicationContext(), "Successssss"+tn, Toast.LENGTH_SHORT).show(); 


      } 
     }); 

     return grid; 
    } 
0

GridView onItemClick在网格项内的任何视图都是可点击时未触发。我看到您的网格项目布局包含默认可点击的Horizo​​ntalScrollView(这非常合理,当点击事件不由scrollview处理时,如何与滚动视图进行交互)。

如果你想使用这样的布局,你必须处理网格项目级别的所有点击事件,而不是网格级别。