2016-10-02 41 views
0

嗨我想刪除Gridview中點擊的單詞,但我不知道如何? 例如,如果我想刪除例如「房子」我應該點擊它,它會被刪除。 我試圖使用onclick,但我得到一個錯誤。如何使用android studio刪除GridView中點擊的單詞?

mainActivity: 
package com.example.hi.myapplication; 

import android.app.Activity; 
import android.os.Bundle; 

import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.GridView; 
import java.util.ArrayList; 

public class MainActivity extends Activity { 

GridView gv; 
ArrayList<String> names = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 

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

    names.add("hi"); 
    names.add("go"); 
    names.add("went"); 
    names.add("house"); 
    names.add("home"); 
    names.add("blind"); 
    names.add("ears"); 
    names.add("hear"); 


    gv = (GridView) findViewById(R.id.gridview_id); 

    //adapter 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names); 
    gv.setAdapter(adapter); 
} 
} 

我的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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.example.hi.myapplication.MainActivity"> 

    <GridView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/textView" 
     android:layout_toRightOf="@+id/textView" 
     android:layout_toEndOf="@+id/textView" 
     android:numColumns="auto_fit" 
     android:id="@+id/gridview_id" /> 
</LinearLayout> 
+0

請詢問 –

回答

0

首先設置一個OnItemClickListener,那麼塊中添加代碼刪除項目

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

      names.remove(position);    
      adapter.notifyDataSetChanged(); 

     } 
    }); 
+0

它的工作之前,計算器搜索。謝謝 –

+0

太棒了,如果您覺得有用,請點擊「✓」號接受答案。 –

相關問題