如何获取Android中单击网格的文本名称

时间:2014-04-08 14:27:46

标签: android grid-layout

Iam在一个GridLayout的android中创建一个应用程序,每个网格中都有图像和文本,以下是我在FirstScreen.java中创建的代码,现在我希望当我点击网格中的任何图像或文本时,应该打开新的屏幕或活动。但我没有得到如何打开

所以请帮助我同样的???

package com.abc;

import java.util.ArrayList;

import com.abc.ImageAdapter.RecordHolder;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class FirstScreen extends Activity 
{
GridView gridView;
ImageAdapter imgAdapter;
ArrayList<Item> gridArray = new ArrayList<Item>();


protected void onCreate(Bundle b) 
{
    super.onCreate(b);
    setContentView(R.layout.firstscreen);


    Bitmap home = BitmapFactory.decodeResource(this.getResources(), R.drawable.home);
    Bitmap cinema = BitmapFactory.decodeResource(this.getResources(), R.drawable.cinema);
    Bitmap dining = BitmapFactory.decodeResource(this.getResources(), R.drawable.dining);
    Bitmap electronic = BitmapFactory.decodeResource(this.getResources(), R.drawable.electronic);
    Bitmap fitness = BitmapFactory.decodeResource(this.getResources(), R.drawable.fiteness);
    Bitmap gift = BitmapFactory.decodeResource(this.getResources(), R.drawable.gift);
    Bitmap hotel = BitmapFactory.decodeResource(this.getResources(), R.drawable.hotel);
    Bitmap lifestyle = BitmapFactory.decodeResource(this.getResources(), R.drawable.lifestyle);
    Bitmap moter = BitmapFactory.decodeResource(this.getResources(), R.drawable.moter);
    Bitmap hospital = BitmapFactory.decodeResource(this.getResources(), R.drawable.hospital);
    Bitmap salon = BitmapFactory.decodeResource(this.getResources(), R.drawable.salon);
    Bitmap travel = BitmapFactory.decodeResource(this.getResources(), R.drawable.travel);


    gridArray.add(new Item(dining,"Food & Resturant"));
    gridArray.add(new Item(fitness,"Fitness & Health")); 
    gridArray.add(new Item(salon,"Salons")); 
    gridArray.add(new Item(home,"Home Furnishing")); 
    gridArray.add(new Item(cinema,"Entertainment")); 
    gridArray.add(new Item(moter,"Automobiles")); 
    gridArray.add(new Item(lifestyle,"Lifestye")); 
    gridArray.add(new Item(electronic,"Electronics")); 
    gridArray.add(new Item(travel,"Travels & Lesiure")); 
    gridArray.add(new Item(gift,"Books & Gifts")); 
    gridArray.add(new Item(hospital,"Hospital")); 
    gridArray.add(new Item(hotel,"Hotel")); 


    GridView gridView  = (GridView) findViewById(R.id.gridview);

    imgAdapter = new ImageAdapter(this, R.layout.row_grid, gridArray);
    gridView.setAdapter(imgAdapter);


    gridView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, long id) 
    { 

    }

    }); 


}

}

1 个答案:

答案 0 :(得分:0)

我认为ImageAdapter是您的实施。

你可以像这样获得适配器

ImageAdapter adpater = (ImageAdapter) ((GridView) parent).getAdapter();

然后点击项目

Item item = adapter.getItem(position);

然后用它做任何你想做的事。

另一个解决方案是从网格数组中获取项目。

Item item = gridArray.get(position);