Question

I'm pretty new to Android and I have been playing about with GridView. I've got stuck trying to retrieve Strings from individual elements of the GridView so that I can save state based on the changes a user has made.

I've mocked up a stripped down version of what I'm trying to do for this question:

The view of each element in the grid is made up of a TextView and an EditText defined by the following xml file; grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"            
    android:id = "@+id/single_item_id"          
    android:layout_width="fill_parent"             
    android:layout_height="fill_parent"             
    android:orientation="vertical"
>  
    <TextView
      android:id = "@+id/label"
      android:layout_width = "fill_parent"             
      android:layout_height="wrap_content"
      android:inputType="text">
    </TextView>
    <EditText
      android:id = "@+id/item_value"
      android:layout_width = "fill_parent"             
      android:layout_height="wrap_content"
      android:inputType="text">
    </EditText>     
 </LinearLayout> 

My main.xml, consisting of a button and a gridview:

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

        <Button
            android:id="@+id/editbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRetrieveButtonClick"
            android:text="@string/get" android:clickable="true"/>

        <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:columnWidth="250dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:paddingTop="50dp">
        </GridView>

    </LinearLayout>

My Activity Class:

    package com.jdev.simplegrid;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.GridView;
    import android.widget.Toast;

    public class SimpleGridActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            GridView gridview = (GridView) findViewById(R.id.gridview);

            //Some hard-coded sample data -each person has a birth-name and a nickname
            ArrayList<Person> people = new ArrayList<Person>();
            people.add(new Person("David","Dave"));
            people.add(new Person("Bruce","Batman"));
            people.add(new Person("John","J"));

            gridview.setAdapter(new SimpleAdapter(this, people));
        }

        public void onRetrieveButtonClick(View view)  
        {  
            Toast.makeText(getBaseContext(), "Get the String value of EditText at position 1 of the gridview e.g Batman", Toast.LENGTH_LONG).show();
            //Get the String value of EditText at position 1 of the gridview. e.g Batman 
        }
    }

And finally my Adapter for the view:

    package com.jdev.simplegrid;

    import java.util.ArrayList;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.EditText;
    import android.widget.TextView;

    public class SimpleAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<Person> personList;

        public SimpleAdapter(Context c, ArrayList<Person> people) {
            mContext = c;
            personList = people;
        }

        public int getCount() {
            return personList.size();
        }

        public Person getItem(int position) {
            return personList.get(position);
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view=inflater.inflate(R.layout.grid_item, parent, false);

            TextView birthnameLabel=(TextView)view.findViewById(R.id.birthname);
            birthnameLabel.setText(personList.get(position).getBirthname());

            EditText nicknameText=(EditText)view.findViewById(R.id.nickname);
            nicknameText.setText(personList.get(position).getNickname());

            return view;
        }


    }

So my question is, how do I retrieve the a value from the gridview on the click of a button. Say the value "Batman" in EditText at position 1 of the gridview. I feel like I'm really missing something here!

Was it helpful?

Solution

gridview.getAdapter().getItem(1).getNickname();

Is that what you're looking for? assuming your Person object has a getter method for the nickname of course. - Sorry, obviously I could've seen that in your custom adapter.

If your the idea is the user can change the nickname in the EditText, you'll probably want to add a TextChangedListener (TextWatcher) to each of them and after editting update the nickname on the Person object associated with that position in the grid; e.g. with the help of a setNickname(String) method.

The easiest way to think about this is probably in terms of 'models' and 'views'. The TextViews and EditTexts are the views, whereas the Person objects in the adapter are the models. If you want to make any changes to the data, modify the underlying models with some logic and simply have the views update/refresh after that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top