-2

i cant figure out what im doing wrong. i have a navigation drawer set up and when i click on one of the items in the navigation drawer it loads a fragment of a list view which i customized with an adapter. that part works.

but I added a onclicklistener inside that fragment that when clicked i want to load a new fragment. but when i click on an item in the listview nothing happens. here is my code.

this is the EventsFragment.java, this is what loads the custom listview.

package org.nctta.nctta_tournaments.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView;

import org.nctta.nctta_tournaments.R;
import org.nctta.nctta_tournaments.Tournament;
import org.nctta.nctta_tournaments.TournamentAdapter;

import java.util.ArrayList;

/**
 * Created by John on 9/10/2017.
 */

public class EventsFragment extends Fragment {

public EventsFragment() {
    //Required empty public constructor
}

View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.events_layout, container,false);

    //create a list of tournaments for testing
    final ArrayList<Tournament> Tournaments = new ArrayList<Tournament>();
        Tournaments.add(new Tournament("1","Ohio East Fall Tournament","1/1/2017", "1/1/2017","University of Akron"));
        Tournaments.add(new Tournament("2","Ohio West Fall Tournament","1/1/2017", "1/1/2017","Ohio State University"));
        Tournaments.add(new Tournament("3","Upper Midwest Fall Tournament","1/1/2017", "1/1/2017","University of Iowa"));
        Tournaments.add(new Tournament("4","Central Plains Fall Tournament","1/1/2017", "1/1/2017","Univeristy of Centeral Plains"));
        Tournaments.add(new Tournament("5","Lower Midwest Fall Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
        Tournaments.add(new Tournament("6","Midwest Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
        Tournaments.add(new Tournament("7","Great Lakes Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
        Tournaments.add(new Tournament("8","South Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));

    //Create an Tournament Adapter
    TournamentAdapter adapter = new TournamentAdapter(getActivity(),Tournaments);

    //find the listview object in the view hierarchy
    ListView listView = (ListView) myView.findViewById(R.id.tourList);
    listView.setAdapter(adapter);

    //set a click listener to open tournament info page
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
            //get the tournament object at the given position the user clicked on
            Tournament tournament = Tournaments.get(position);
            //start tournamentactivity
            android.app.FragmentManager fragmentManager = getFragmentManager();

            fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, new TournamentFragment())
                        .commit();
        }
    });
    return myView;
}
}

and here is my TournamentFragment that should load when an item is clicked on. this fragment i want to load a pageviewer with tabs which i couldn't get to work so for now i commented out that stuff. just want to get the clicklistener to work first.

package org.nctta.nctta_tournaments.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.nctta.nctta_tournaments.R;

/**
 * Created by John on 9/15/2017.
 */

public class TournamentFragment extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.tournament_tabs, container,false);
/*
    //Find the view pager that will allow the user to swipe between fragments
    ViewPager viewPager = (ViewPager) myView.findViewById(R.id.viewpager);
    //create an adapter that knows which fragment should be shown on each page
    TournTabsAdapter adapter = new TournTabsAdapter(this, getChildFragmentManager());
    //set the adapter onto the view pager
    viewPager.setAdapter(adapter);
    //find the tab layout that shows the tabs
    TabLayout tabLayout = (TabLayout) myView.findViewById(R.id.tabs);
    // Connect the tab layout with the view pager. This will
    //   1. Update the tab layout when the view pager is swiped
    //   2. Update the view pager when a tab is selected
    //   3. Set the tab layout's tab names with the view pager's adapter's titles
    //      by calling onPageTitle()
    tabLayout.setupWithViewPager(viewPager);
*/
    return myView;
}

}

any ideas??

JED
  • 55
  • 8
  • is this the onItemClickListener called? Put a log call in there to see if it is called – joao86 Sep 15 '17 at 23:47
  • i through a toast message in there, when i debug it never gets triggered – JED Sep 16 '17 at 00:58
  • So apparently it is the listener which is not working. Well try adding the onclick event to your adapter – joao86 Sep 16 '17 at 01:00
  • figured out why it wasnt working. my listview adapter i have a ImageButton. when i remove that from the xml it works...so how would i make it work so there is a clicklistener for clicking a item in the list and a click listener for clicking the imagebutton in that item. – JED Sep 16 '17 at 01:08
  • You could still have both, at least with recyclerviews I have several onclick listeners and they work. You must guarantee that they are not listening to same "area" :) – joao86 Sep 16 '17 at 01:25
  • do you have any example code for how to have multiple onclick listeners...does it need to be in the adapter – JED Sep 16 '17 at 01:49
  • Can you see this: https://github.com/maneca/SplitRide/blob/master/app/src/main/java/joao/splitride/app/custom/SegmentListAdapter.java ? This is not the perfect example of what you should do since it calls activities inside the adapter but it may help you to get a grip of how you do it – joao86 Sep 16 '17 at 03:28
  • who ever down voted me, maybe you should comment and tell me why, i can update my post. i was detailed in my question, gave my code. i searched before posting. I am trying to learn here its not like im asking for someone to do the work for me. and thank you to @joao86 i was able to figure out how to get it to work with his help in the comments, see posted answer – JED Sep 17 '17 at 12:37
  • glad to helep ;) – joao86 Sep 18 '17 at 09:53

1 Answers1

0

okay with help from the comments and some googling i figured out how to get the onclicklistener to work...not sure why my post was downvoted... the main reason the item on click listener wasnt working was because my custom adapter for the list view included a imagebuttom. what i ended up doing was removing that listener and adding onclicklisteners inside the adapter. the key was to make use of the settag.

here is my adapter code. i put a toast message in the on click just to verify.

package org.nctta.nctta_tournaments;

/**
 * Created by John on 9/15/2017.
 */

import android.app.Activity;
import android.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.nctta.nctta_tournaments.fragments.TournamentFragment;

import java.util.ArrayList;

public class TournamentAdapter extends ArrayAdapter<Tournament> {

//create a new TournamentAdapter object.
public TournamentAdapter(Context context, ArrayList<Tournament> tournaments) {
    super(context, 0 , tournaments);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //check if an existing view is being reuse, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView ==null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
            R.layout.tournament_list_item, parent, false);
    }
    //get the current tournament object at this position in the list
    Tournament currentTournament = getItem(position);

    //Find the TExtView in the tournament_list_item.xml layout with iD of name
    TextView TourName = (TextView) listItemView.findViewById(R.id.Name);
    //get/set the tournament name of the current tournament object
    TourName.setText(currentTournament.getTournamentName());
    //Find the TextView in the tournament_list_item.xml layout with ID of Location
    TextView TourLocation = (TextView) listItemView.findViewById(R.id.Location);
    //get/set the tournament location of the current tournament object;
    TourLocation.setText(currentTournament.getTournamentLocation());
    //Find the linearlayout with id of text_container
    LinearLayout textArea = (LinearLayout) listItemView.findViewById(R.id.text_container);
    textArea.setTag(currentTournament.getTournamentName());
    //Find map view button with id of imageButton
    ImageButton mapButton = (ImageButton) listItemView.findViewById(R.id.imageButton);
    mapButton.setTag(currentTournament.getTournamentName());
    final Context context = parent.getContext();
    final FragmentManager fm = ((Activity) context).getFragmentManager();
    //set onclicklisteners
    textArea.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = v.getTag().toString();
            Toast.makeText(getContext(),str + "TEXT", Toast.LENGTH_SHORT).show();
            //start tournament fragment

            fm.beginTransaction()
                    .replace(R.id.content_frame, new TournamentFragment())
                    .commit();
        }
    });
    mapButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = v.getTag().toString();
            Toast.makeText(getContext(),str + "MAP", Toast.LENGTH_SHORT).show();
        }
    });

    return listItemView;
}
}
JED
  • 55
  • 8
  • Something you could do to improve this is to use a callback interface, so it don't perform the fragment transaction inside the adapter – joao86 Sep 18 '17 at 09:56