Code Vader
Code Vader

Reputation: 755

onListItemClick never called when using tab navigation

I have a ListActivity which displays a ladder of ranked players in a sports club. The navigation mode is set to tabs. When I try to implement onListItemClick it never gets called. I read through a lot of other posts like Android: onListItemClick not getting called in ListActivity and onListItemClick is not getting called on ListActivity where people recommended setting the widgets in the list to android:focusable="false", however this does not seem to be working out for me. I don't know if the tabs navigation has anything to do with this, but anyway- below is my code.

Activity's onCreate to onListItemClick

public class Rankings extends ListActivity
{

    ArrayList<Club> clubs;
    public static ArrayList<RankedPlayer> Women = new ArrayList<RankedPlayer>();
    public static ArrayList<RankedPlayer> Men = new ArrayList<RankedPlayer>();
    int menuPosition = 0;
    private static Context context;
    public static TabSelected _tab;
    RankingEditorMode mode;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rankings);

        clubs = getIntent().getParcelableArrayListExtra("clubs");

        context = Rankings.this;

        final ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab men = actionbar.newTab();
        men.setText("Men");
        men.setIcon(R.drawable.signs_man_32x32);
        men.setTabListener(new TabListener() 
        {

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) 
            {
                _tab = TabSelected.Men;
                GoToFragment();
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }
        });

        Tab women = actionbar.newTab();
        women.setText("Women");
        women.setIcon(R.drawable.signs_woman_32x32);
        women.setTabListener(new TabListener() 
        {

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) 
            {
                _tab = TabSelected.Women;
                GoToFragment();
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) 
            {
                // TODO Auto-generated method stub

            }
        });

        actionbar.addTab(men);
        actionbar.addTab(women);


        GetRankings();
    }

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id)
    {
        super.onListItemClick(list, view, position, id);

        System.out.println("\n\n\n>>>>>>>>>>   Popup <<<<<<<<<<<<<<<<<\n\n\n");

        AlertDialog.Builder popup = new AlertDialog.Builder(Rankings.this);
        popup.setCancelable(true);
        popup.setTitle("Share Log?");

        String message = clubs.get(menuPosition).getName();
        if(_tab == TabSelected.Men)
        {
            message += "\t-\t Men";
        }
        else
        {
            message += "\t-\t Women";
        }
        popup.setMessage(message);

        popup.setPositiveButton("Share", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                ShareLog();
            }
        });

        popup.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
            }
        });
        popup.show();
    }

Fragment

public class ranking_fragment extends ListFragment
{
    @Override
    public void onActivityCreated(Bundle bundle)
    {
        super.onActivityCreated(bundle);
        ArrayList<RankedPlayer> rankings;
        if(Rankings._tab == TabSelected.Men)
        {
            rankings = Rankings.Men;
        }
        else
        {
            rankings = Rankings.Women;
        }

        RankingAdapter adapter = new RankingAdapter(getActivity(), rankings);
        setListAdapter(adapter);
    }

}

Adapter

public class RankingAdapter extends ArrayAdapter<RankedPlayer>
{

      private final Context context;
      ArrayList<RankedPlayer> rankings;

    public RankingAdapter(Context context, ArrayList<RankedPlayer> rankings) 
    {
        super(context, R.layout.jarvis_log, rankings);
        this.context = context;
        this.rankings = rankings;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.ranking_layout, parent, false);

        TextView rank = (TextView)rowView.findViewById(R.id.tvRanking_rank);
        TextView name = (TextView)rowView.findViewById(R.id.tvRanking_name);
        TextView club = (TextView)rowView.findViewById(R.id.tvRanking_club);
        TextView points = (TextView)rowView.findViewById(R.id.tvRanking_points);

        rank.setText((position+1)+".");
        name.setText("\t"+rankings.get(position).getName());
        points.setText(rankings.get(position).getPoints()+"\t");

        if(rankings.get(position).getClub().length() >= 1)
        {
            club.setText("\t("+rankings.get(position).getClub()+")");
        }
        else
        {
            club.setText("");
        }
        return rowView;
    }

}

Activity's Layout xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="rankings.Rankings"
    tools:ignore="MergeRootFrame" >
<ListView 
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</FrameLayout>

ranking_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/OrangeRed"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingBottom="4dp"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false">

    <TextView
        android:id="@+id/tvRanking_rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="1."
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue" 
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvRanking_rank"
        android:text="Steve Coppinger"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:textStyle="bold" 
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="900"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:focusable="false"
        android:clickable="false" 
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_club"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvRanking_name"
        android:text=" (KZN)"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:focusable="false"
        android:clickable="false" 
        android:focusableInTouchMode="false"/>

</RelativeLayout>

Any help would be appreciated

Upvotes: 0

Views: 44

Answers (2)

Vishal Jain
Vishal Jain

Reputation: 175

There is no declaration of fragment in your main activity, that's why it it not even visible to the activity. You simply make an activity and listview must be loaded in main activity, just call adapter through fragment.

Upvotes: 0

MatF
MatF

Reputation: 1736

Your actual lists are not in the activity, but in the fragments. Therefor onListItemClick in the Activity is never called. You have to implement it in the Fragment class. Your Activity also doesn't has to be a ListActivity.

Upvotes: 1

Related Questions