0

I have items inside a list which come through a CustomListAdapter and inside my MainActivity I am having problems getting the imageview etc out.

Inside my MainActivity I have:

CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), urlArrays, images, tabType, ringImage, pseButton);

        list=(ListView) rootView.findViewById(R.id.list_view);
        list.setAdapter(adapter);

Using the above how can I get out items I want to set them to onClickListeners for example?

If I do this:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    final int position, long id) {

This will set the whole row to a listener onItemClick. But I want to get for instance the pseButton and onClick do something with that. I've tried a Hashmap like this inside the onClick event above but this gives me a String cast exception:

   HashMap<String, Object> obj = (HashMap<String, Object>) parent.getAdapter().getItem(position);
                Object pause_button = obj.get("R.drawable.pseButton").toString();
halfer
  • 19,824
  • 17
  • 99
  • 186
Sam
  • 1,207
  • 4
  • 26
  • 50
  • Set the onclick behaviour in the adapter. You can define an interface if you need to execute methods in the Activity – Ken Wolf Aug 21 '17 at 15:09
  • @KenWolf doesn't onclick need to be within an onCreate activity? My mainActivity is a tab and uses the CustomListAdapter as the other tabs do. – Sam Aug 21 '17 at 15:51
  • No it doesn't. In your adapter getView() you can setOnClickListener on any view. – Ken Wolf Aug 21 '17 at 16:08
  • @KenWolf Okay that's true but that would mean I have to move all the arrays and items I have within each tab into the CustomListAdapter and have one big long class which defeats the purpose of this. I just want to get the items I have passed in through the TabActivity and get them out in the Activity class – Sam Aug 21 '17 at 16:10
  • No you don't, you can just call a method in your MainActivity from your adapter. Keep your arrays and items where they are. https://stackoverflow.com/questions/12142255/call-activity-method-from-adapter – Ken Wolf Aug 21 '17 at 16:11
  • Okay will have a go at that, at the moment when I do this: mContext instanceof YourActivityName inside the onClick it shows me the Context cannot be cast to the Activity – Sam Aug 21 '17 at 16:32
  • It's because my Activities extend Fragments as they are tabs :( – Sam Aug 21 '17 at 16:36
  • @KenWolf sorry next question how do you make a context for the fragment to get methods from tabbedactivity? – Sam Aug 21 '17 at 17:11
  • @KenWolf can you please show me how I can put Media playing from an array in TabActivity1 and add this to an onClick button in CustomListAdapter? – Sam Aug 22 '17 at 17:05

0 Answers0