删除gridview中的按钮

时间:2014-07-31 23:06:16

标签: android android-gridview

我有一个带8个按钮,2行4列的gridview。我试图删除一个按钮甚至一行..一行可能更好..我想在我点击一个按钮时发生这个删除。任何按钮。我遇到了麻烦。到目前为止,我将向您展示代码:

Mainactivity:

public class MainActivity extends Activity {

private TextView text;
private GridView gridView;
private TextView timerValue;
private Boolean firstclick = false;
private long startTime = 0L;
private Handler customHandler = new Handler();
private Button buttonclicked;
Random r = new Random();
//private int[] rannumbers_array = new int[32];
private int[] shuffle_array = new int[4];
private String[] items = new String[8];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.feedback);
    timerValue = (TextView) findViewById(R.id.timerValue);
    timerValue.setTextColor(Color.GREEN);
    int j = 0;
    int k = 0;
    int ranval = 0;
    for(int i = 0; i < 8; i++)
    {
        if ( (i % 4) == 0)
        {
            for(int x = 0; x < 4; x++)
            {
                shuffle_array[x] = 0;
            }
            do {
                ranval = r.nextInt(100);
            } while((ranval % 2) != 0);
        }
        else
        {
            do {
                ranval = r.nextInt(100);
            } while((ranval % 2) == 0); 
        }
        shuffle_array[j] = ranval;
        j++;
        if(j >= 4)
        {
            j = 0;
            shuffle(shuffle_array);
            do {
               items[k] = Integer.toString(shuffle_array[j]);
               k++;
               j++;
            } while(k <= i);
            j = 0;
        }
        //rannumbers_array[i] = r.nextInt(100);
        //items[i] = Integer.toString(rannumbers_array[i]);
    }
    k = 0;




    gridView = (GridView) this.findViewById(R.id.myGridView);
    final CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
    gridView.setAdapter(gridAdapter);
    buttonclicked = gridAdapter.getbutton();
    gridView.post(new Runnable() {
        public void run() {
            gridView.setSelection(gridView.getCount() - 1);
        }
    });
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(!firstclick)
            {
               customHandler.postDelayed(updateTimerThread, 0);
               firstclick = true;
            }
            buttonclicked.setVisibility(view.GONE);

我的CustomGridAdapter:

public class CustomGridAdapter extends BaseAdapter {

private Context context;
private String[] items;
private Button button;
LayoutInflater inflater;

public CustomGridAdapter(Context context, String[] items) {
    this.context = context;
    this.items = items;
    inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }
    button = (Button) convertView.findViewById(R.id.grid_item);
    button.setHeight(175);
    button.setText(items[position]);

    return convertView;
}

@Override
public int getCount() {
    return items.length;
}

@Override
public Object getItem(int position) {
    return items[position];
}

@Override
public long getItemId(int position) {
    return position;
}

public Button getbutton()
{
    return button;
}

}

cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/grid_item"
android:layout_gravity="center"
android:maxHeight="175dip"
android:layout_marginLeft="20dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="05"
android:textSize="25sp"
android:clickable="false"
android:focusable="false"/>

activitymain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/timerValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="2dp"
    android:text="@string/timerVal"
    android:textColor="#ffffff"
    android:textSize="30sp" />

<GridView
    android:id="@+id/myGridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:columnWidth="90dp"
    android:horizontalSpacing="0dp"
    android:numColumns="4"
    android:verticalSpacing="0dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/feedback"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

buttonclicked.setVisibility(view.GONE);不行,给我一个错误我想。我试图点击一个按钮或项目,并将该项目和/或项目所在的行删除。我可以这样做吗?我错过了什么?删除发生后,我需要重新绘制网格以减少其中的项目。这些是我现在难倒的两件事

2 个答案:

答案 0 :(得分:0)

在OnItemClick实现上使用view参数,此函数会为您提供网格的位置,单元格视图以及适配器上自定义项的ID。

   gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(!firstclick)
        {
           customHandler.postDelayed(updateTimerThread, 0);
           firstclick = true;
        }
        //buttonclicked.setVisibility(view.GONE);
        view.findViewById(R.id.grid_item).setVisibility(view.GONE);
     }
   }

答案 1 :(得分:0)

尝试使用final将声明的输入的位置更改为OnCreate。