Android GridView

0

I hope you have a great time. I have the following situation a gridview that contains 3 columns in rows in each cell of this grid displaying three information all three being a plain text (TextView that I enter from another xml) I am populating this grid for now with an arraylist just for testing . I would like to know how I can in the click event of this grid (specific cell) I can get the values I passed to it. example.

The cell has 3 information that has been set in it. day month year  all in a single cell but I want to get these values separated after the click. Thanks in advance.

    
asked by anonymous 23.06.2014 / 18:44

1 answer

0

I believe you need to use AdapterView.OnItemClickListener in your GridView , so you can be notified when an item of your GridView is selected.

The code would look something like:

public class MyActivity extends Activity {
     ListAdapter mAdapter;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(...);

         mAdapter = new MyAdapter(); // Inicialize seu Adapter

         GridView gv = (GridView) findViewById(...); // Recupera o GridView

         gv.setAdapter(mAdapter);
         gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
                 Object obj = mAdapter.getItem(position);
                 // Usar o objeto relativo aquela célula
             }
         });
     }
}

public class MyAdapter extends BaseAdapter {

    List<Object> mItems = new ArrayList<Object>();

    // Demais metodos necessário

    public Object getItem(int position) {
        return mItems.get(position);
    }
}

Of course I simplified the code a lot (leaving only the most important code), omitting some methods and code that need to be implemented. But I recommend reading the GridView documentation, and a Tutorial on how to bind data to a AdapterView , class AdapterView.OnItemClickListener and the class itself AdapterView that has many other Listeners that may be useful.

    
23.06.2014 / 19:12