Reputation: 83
Hi I want to remove the clicked word from Gridview but I don't know how? for example if i want to remove for example "house" I should click it and it will be removed. I tried to use onclick but I got an error.
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);
}
}
my xml code :
<?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>
Upvotes: 0
Views: 54
Reputation: 726
First set an OnItemClickListener, then inside the block add the code to remove an item
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
names.remove(position);
adapter.notifyDataSetChanged();
}
});
Upvotes: 1