Android Activity ListView单击时未打开相应片段的项目

时间:2015-05-27 12:05:48

标签: android-fragments android-listview

我的活动只有一个列表视图。我已经设置了listview onClick Listener,它应该打开一个新的片段,具体取决于点击了哪个列表项,我已经检查但是无法理解为什么List项目点击无法工作

以下是该活动的代码

    package com.jp.kabs.mybills;

import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

import adapters.NavDrawerAdapter;
import models.NavDrawerItemModel;

/**
 * Created by Kabunyi on 27/05/2015.
 */
public class MainActivity extends ActionBarActivity {

    private ListView lstMain;
    private String[] menuItems;
    private TypedArray menuIcons;
    private NavDrawerAdapter adapter;
    private ArrayList<NavDrawerItemModel> navDrawerItems;

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

        lstMain.setOnItemClickListener(new DrawerItemsClickListener());

        //fetch resources
        // load slide menu items
        menuItems = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        menuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

        addStuffToArrayList();
    }


    private void connnectToXML(){
        lstMain = (ListView)findViewById(android.R.id.list);
    }

    private void addStuffToArrayList(){
        navDrawerItems = new ArrayList<NavDrawerItemModel>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItemModel(menuItems[0], menuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItemModel(menuItems[1], menuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItemModel(menuItems[2], menuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItemModel(menuItems[3], menuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItemModel(menuItems[4], menuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        //navDrawerItems.add(new NavDrawerItemModel(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        menuIcons.recycle();

        // setting the nav drawer list adapter
        adapter = new NavDrawerAdapter(navDrawerItems, getApplicationContext());
        lstMain.setAdapter(adapter);
    }

    //method to display a view fragment of choice
    private void displayView(int position){
        //update the main content by replacing fragments
        Fragment frag = null;

        switch (position){
            case 0:
                frag = new AddMyBillsFragment();
                AddMyBillsFragment selectedBillerToEasyPayFrag = new AddMyBillsFragment();
                getSupportFragmentManager().
                        beginTransaction().
                        replace(R.id.my_layout, selectedBillerToEasyPayFrag, "BillerToEasyPayDetails").addToBackStack(null).commit();
                break;
            case 1:
                frag = new ViewMyBillsFragment();
                break;
            case 2:
                frag = new PayMyBillsFragment();
                break;
            case 3:
                frag = new EasyPayFragment();
                break;
            case 4:
                frag = new MyBillAlertsFragment();
                break;

            default:
                break;
        }
        if(frag !=null){
            //FragmentManager fm = getSupportFragmentManager();
            //fm.beginTransaction().replace(R.id.frame_container, frag).commit();

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.my_layout, frag).commit();


            //update selected items and title then close drawer
            lstMain.setItemChecked(position, true);
            lstMain.setSelection(position);
            setTitle(menuItems[position]);

        //}else{
            Log.e("MenuActivity", "Error in creating fragment");
        }

    }
    //class to handle drawer list item clicks
    private class DrawerItemsClickListener implements ListView.OnItemClickListener{

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

请帮助

活动主要代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

0 个答案:

没有答案