I just started with Android programming, and here's what.
How can I check if an item in a GridView has already been clicked? Something like assigning a logical βclickβ to an EACH element in the Grid and changing its value every time I click on the element.
Currently, I just use the bools array, so if I click on the [x] element, it switches bool [x], and then I check if it is true / false and change the element accordingly, but there should be a tidier way to do same!
My code is:
package com.example.mojrecnik; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.AdapterView; import android.widget.TextView; import android.widget.Toast; import android.support.v4.app.NavUtils; public class Glavna extends Activity implements AdapterView.OnItemClickListener { private static final int LENGTH_SHORT = 0; GridView grid; TextView tekst; String[] izfajla = new String[200]; String[] izfajla2 = new String[200]; boolean[] kliknutmrs = new boolean[200]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_glavna); grid=(GridView)findViewById(R.id.grid); grid.setAdapter(new MojAdapter()); grid.setOnItemClickListener(this);
And XML (main):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:horizontalSpacing="@dimen/padding_medium" android:numColumns="3" android:stretchMode="columnWidth" > </GridView> </RelativeLayout>
And the layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gridtekst2" /> </LinearLayout>
source share