Android Gridview

Asked

Viewed 464 times

0

People who really need your help. I have the following situation a gridview that contains 3 columns and n rows in each cell of this grid display three information all three being a simple text(Textview that I insert from another xml) I am populating this grid for now with an arraylist just for testing. I would like to know how to do so that in the event click of this grid(cell in specifics) I can get the values I passed to it. example.

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

  • Is wearing a Adapter correct?

1 answer

0


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

The code would be 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 have greatly simplified the code (leaving only the most important code), omitting some methods and code that need to be implemented. But I recommend reading the documentation on the GridView, and a how to link the data to a AdapterView, the class AdapterView.OnItemClickListener and the class itself AdapterView that has many others Listeners that may be useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.