Android gridView无法使用onItemClick

时间:2014-04-29 18:42:27

标签: android gridview onitemclick

我正在尝试使用gridView,但我无法在onItemClick上打开一个对话框。我写了代码,但是当我按下一个项目时,它没有做任何事情。我尝试了所有项目,但仍然没有。谢谢!!!

这是我的Galerie.java:

package com.descoper.rom;

import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;



public class Galerie extends Activity {
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
     GalerieAdapter customGridAdapter;
     private Dialog dialog;
     Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galerie);

        //set grid view item
        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ro);
        Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ro);

        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"House"));
        gridArray.add(new Item(userIcon,"Friend"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"Personal"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Building"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"xyz"));


        gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new GalerieAdapter(this, R.layout.galerie_row, gridArray);
        gridView.setAdapter((ListAdapter) customGridAdapter);
    }


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

        if(position == 0){
             dialog = new Dialog(context);
             dialog.setContentView(R.layout.dialog1);
             dialog.setTitle(position);

             TextView text = (TextView) dialog.findViewById(R.id.textView1);
             Button btnSave = (Button) dialog.findViewById(R.id.button1);

             btnSave.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v) {

                     dialog.dismiss();

                 }
             });
             dialog.show();
         }


        if(position == 1){
            Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
        }
        if(position ==2){
            Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您是否错过了对gridView.setOnItemClickListener的调用?这样做:

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 0){
                dialog = new Dialog(context);
                dialog.setContentView(R.layout.dialog1);
                dialog.setTitle(position);

                TextView text = (TextView) dialog.findViewById(R.id.textView1);
                Button btnSave = (Button) dialog.findViewById(R.id.button1);

                btnSave.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        dialog.dismiss();

                    }
                });
                dialog.show();
            }


           if(position == 1){
               Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
           }
           if(position ==2){
               Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
           }
        }
    });