user2774394
user2774394

Reputation: 45

How to get the value from element in the GridView in Android app?

basically, I'm a noob in Android programming and among other problems I'm stuck with this one: I have a GridView declared in my main layout and the XML definition for this grid in the separate xml. I'm adapting grid with SimpleCursorAdapter (not custom adapter). The thing is that I want to remember the value of corresponding ROW_ID when I touch the checkbox in the one of the displayed rows of data ... So, is there an option to do this without the need to customize SimpleCursorAdapter (BTW, is it possible?).

Here are snippets of Layout XML files and code, where I adapt cursor with SimpleCursorAdapter: main.xml

    <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"
    android:background="#BAF0ED"
    android:gravity="right" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1"
        android:numColumns="2" >
    </GridView>

</RelativeLayout>

grid.xml:

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>

        <TextView
            android:id="@+id/row_ID"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5px"
            android:textColor="#4BA79E"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/name"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5px"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5px"
            android:textSize="22dp" />

        <CheckBox
            android:id="@+id/chk"
            android:layout_width="10px"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:onClick="x"/>
    </TableRow>

</TableLayout>

Using SimpleCursorAdapter:

    public void showGridData() {
    @SuppressWarnings("static-access")
    String[] fromColumns = { db.KEY_ROWID, db.KEY_NAME, db.KEY_EMAIL };
    int[] toViews = new int[] { R.id.row_ID, R.id.name, R.id.email };
    db.openDatabase();
    @SuppressWarnings("deprecation")
    SimpleCursorAdapter sCAdapter = new SimpleCursorAdapter(this,
            R.layout.table_row, db.getContacts(), fromColumns, toViews);
    sCAdapter.setViewResource(R.layout.table_row);
    gv.setAdapter(sCAdapter);
    db.closeDatabase();
}

...and the method to read the value of the ROW_ID in the same record as the chebox is positioned (I'am getting allways the value of the first ROW_ID, wheather I touch first, second or n-th checkbox and this is the problem!):

    public void x(View view) {
    TextView tv = (TextView) findViewById(R.id.row_ID);

    recordIds.add(Long.valueOf(tv.getText().toString()));
    long[] longArray = new long[recordIds.size()];
    for (int i = 0; i < recordIds.size(); i++) {
        longArray[i] = recordIds.get(i);
        Toast.makeText(this, String.valueOf(longArray[i]),Toast.LENGTH_LONG).show();

    }
}

Thanks in advance and sorry for bothering you, regards, Erik D.

add1 (1. edit)

Dear friends! Thank you all very much for the answer, but unfortunatelly this suggestions doesn't work for me. Let me post entire code, which I had altered with "setOnItemClickListener(new OnItemClickListener()". It still doesn't work... I'm posting entire code (xml & java) if someone would have some spare time to look at it--I can't figure out what is wrong.

**activity_main.xml**

    <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"
    android:gravity="right" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="20dp"
        android:numColumns="3" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gridView1"
        android:layout_toLeftOf="@+id/button1"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:onClick="AddRecord"
        android:text="Insert" />

</RelativeLayout>

**table_row.xml**

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>

        <TextView
            android:id="@+id/row_ID"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:padding="5px"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/name"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:padding="5px"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:padding="5px"
            android:textSize="22dp" />

        <CheckBox
            android:id="@+id/chk"
            android:layout_width="10px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </TableRow>

</TableLayout>

*MainActivity.java*

> package net.learn2develop.databases;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    DBAdapter db;
    EditText name, mail;
    GridView gv;
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.editText1);
        mail = (EditText) findViewById(R.id.editText2);
        db = new DBAdapter(this);
        gv = (GridView) findViewById(R.id.gridView1);
        showGridData();
    }

    public void AddRecord(View view) {
        if (!(name.getText().toString().isEmpty())
                && !(mail.getText().toString().isEmpty())) {
            db.openDatabase();
            if (db.insertContact(name.getText().toString(), mail.getText()
                    .toString()) >= 0) {
                showMessage("Record added succesfully!", "Inserting...");
                db.closeDatabase();
                showGridData();
            }
        } else {
            showMessage("You can't add empty record!", "Inserting...");
        }
    }

    public void showGridData() {
        String[] fromColumns = { db.KEY_ROWID, db.KEY_NAME, db.KEY_EMAIL };
        int[] toViews = new int[] { R.id.row_ID, R.id.name, R.id.email };
        db.openDatabase();
        SimpleCursorAdapter sCAdapter = new SimpleCursorAdapter(this,
                R.layout.table_row, db.getContacts(), fromColumns, toViews);
        sCAdapter.setViewResource(R.layout.table_row);
        gv.setAdapter(sCAdapter);
        // Implement On Item click listener
        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });
        db.closeDatabase();
    }

    public void showMessage(String message, String source) {
        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
        dlgAlert.setCancelable(false);
        dlgAlert.setMessage(message);
        dlgAlert.setTitle(source);
        dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
    }
}

*DBAdapter.java*

    package net.learn2develop.databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
    static final String KEY_ROWID = "_id";
    static final String KEY_NAME = "name";
    static final String KEY_EMAIL = "email";
    static final String TAG = "DBAdapter";

    static final String DATABASE_NAME = "MyDB";
    static final String DATABASE_TABLE = "contacts";
    static final int DATABASE_VERSION = 1;

    static final String DATABASE_CREATE = "create table " + DATABASE_TABLE
            + " (" + KEY_ROWID + " integer primary key autoincrement, "
            + KEY_NAME + " text not null, " + KEY_EMAIL + " text not null);";

    final Context context;

    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context ctx) {// CONSTRUCTOR!!
        this.context = ctx;

        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS contacts");
            onCreate(db);
        }
    }

    public DBAdapter openDatabase() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void closeDatabase() {
        DBHelper.close();
    }

    public long insertContact(String name, String email) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_EMAIL, email);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteContact(long rowId) {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor getContacts() {
        return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
                KEY_EMAIL }, null, null, null, null, null);
    }

    public Cursor getContact(long rowId) throws SQLException {

        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME, KEY_EMAIL }, KEY_ROWID + "=" + rowId,
                null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public boolean updateContact(long rowId, String name, String email) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_EMAIL, email);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

Dear friends, thanks in advance once again, regards, Erik D.

Upvotes: 3

Views: 3485

Answers (3)

user2774394
user2774394

Reputation: 45

I have found the origin of the problem - basically, all it has to be done is defining the view element of the GridView with: android:focusable="false" android:focusableInTouchMode="false" for instance:

<TextView
    android:id="@+id/row_ID"
    android:layout_width="50px"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textSize="20dp" />

With these two parameters elements of the GridView do not steal the focus of the grid and listener on the grid works.

Have a great time, Erik D.

Upvotes: 0

prijupaul
prijupaul

Reputation: 2086

You need to implement ItemClickListener. onItemClick will be invoked for each item clicked in the gridview and provides the position of click.

Check this sample code on how to implement it. Fetch this position data from adapter.

Upvotes: 1

Yuichi Araki
Yuichi Araki

Reputation: 3458

You can use setTag and getTag methods of View class to tie a piece of information to your views.

Upvotes: 1

Related Questions