İçeriğe geç

Android GridView Tıklanan Hücrenin Değerini Almak

Veri gösterimi için her platforma sıklıkla kullandığımız grid view örneği.

Keys;

  • GridView
  • GridView setAdapter
  • GridView setOnItemClickListener
<?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"
tools:context="com.example.gridview.MainActivity"
android:orientation="vertical">
	<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Selected item text" />
	<GridView
android:id="@+id/gridView"
android:numColumns="5"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {

  TextView selected;
  GridView gridView;
  String[] items = {
    "A",
    "B",
    "C",
    "Ç",
    "D",
    "A",
    "B",
    "C",
    "Ç",
    "D",
    "A",
    "B",
    "C",
    "Ç",
    "D",
    "A",
    "B",
    "C",
    "Ç",
    "D",
    "A",
    "B",
    "C",
    "Ç",
    "D",
    "A",
    "B",
    "C",
    "Ç",
    "D"
  };@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    selected = (TextView) findViewById(R.id.textView);
    gridView = (GridView) findViewById(R.id.gridView);

    ArrayAdapter < String > arrayAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, items);
    gridView.setAdapter(arrayAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
      public void onItemClick(AdapterView < ?>adapterView, View view, int i, long l) {
        selected.setText(items[i]);
      }
    });
  }
}
Tarih:Android

İlk Yorumu Siz Yapın

Bir Cevap Yazın