获取gridView中单击项的文本

时间:2015-05-14 07:53:34

标签: android

我有一个gridView,其中每个单元格都包含一个ImageView和一个TextView。当用户点击某个项目时,我想将TextView的文本放在用户单击的位置。我尝试了下面的代码,但每次我拿第一个元素。

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        ViewGroup gridChild = (ViewGroup) grid.getChildAt(position);
        int childSize = gridChild.getChildCount();

        for(int k = 0; k < childSize; k++) {
           if(gridChild.getChildAt(k) instanceof TextView ){
                  TextView temp = (TextView) findViewById(gridChild.getChildAt(k).getId());
                  stationId = temp.getText().toString();
                  Log.i("stationId", stationId);
            }
        }

3 个答案:

答案 0 :(得分:1)

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

         String clickedText = yourGridView.getItemAtPosition(position).toString());


       }
   });

答案 1 :(得分:0)

使用此

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        rowView = inflater.inflate(R.layout.gridView_item, null);
        TextView tw1 = (TextView) rowView.findViewById(R.id.textview);
ImageView im = (ImageView) rowView.findViewById(R.id.ImgaeView);

        rowView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
String getText=tw1.getText();

            }

        return rowView;
    }

答案 2 :(得分:0)

gridItems是一个包含gridView中项目数组列表的类。使用数组列表用户定义方法getGridItems()来获取gridItems.On的数组列表然后调用方法.get(position)将触发事件的位置索引作为参数传递给.get(position)来检索哪个曾经与GridIemsList的项目相关联的属性

public class GridItemsList {
private ArrayList<GridItem> gridItems = new ArrayList<GridItem>();

public GridItemsList() {

}

public ArrayList<GridItem> getGridItems() {
    return gridItems;
}

public void addToList(GridItem gridItem) {
    this.gridItems.add(gridItem);
}



  //ManinActivity.java                                       
  private GridItemsList gridItems; 
    //dump some data to arrayList
   gridItems = new GridItemsList();
    for(int i=0;i<12;i++)
    {
        GridItem item = new GridItem();
        if((i%2)==0)
        {
            item.setImageId(R.drawable.family);
            item.setName("Family");
        }
        else {
            item.setImageId(R.drawable.love);
            item.setName("Love");
        }


        gridItems.addToList(item);


    }
    CustomGrid adapter = new 
    CustomGrid(MainActivity.this,gridItems.getGridItems());
    grid=(GridView)findViewById(R.id.grid);
    grid.setAdapter(adapter);                                         

   /***
     *  create a Toast
     */                                         
  grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            String clickedText = gridItems.getGridItems().get(position).getName();
            Toast.makeText(MainActivity.this, "You Clicked at " + clickedText  , Toast.LENGTH_SHORT).show();


        }
    });