Start Activities from onItemClick of ListView in f

2019-04-17 00:59发布

So I have two fragments linking to layout files which display ListViews. The ListViews are defined in the xml and have entries from a string array. I want to click on items in the ListView and open new activities. There are 8 items in one ListView and 9 in the other. In the onItemClick code, how do I create intents to start activities based on the item clicked? I will create 1 class per item as its own activity. How can I start the activities in the classes via intents inside the onItemClick methods of this code?

class CommunityFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.community_fragment, container, false);
        ListView lv = (ListView) view.findViewById(R.id.communityListView);
        lv.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
                // TODO Auto-generated method stub

            }
        });
        return view;
    }
}
class ResourcesFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.resources_fragment, container, false);
        ListView lv = (ListView) view.findViewById(R.id.resourcesListView);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub

            }
        });
        return view;
    }
}

7条回答
三岁会撩人
2楼-- · 2019-04-17 01:58

If you use an adapter to display the items in a list, it is important to differentiate the following:

The ID of the list AdapterView#getId() is different than the ID of the items in the list ArrayAdapter<String>#getId(), because the view of the list contains the views of the elements.

Suppose an example where you are going to launch activities with user roles. You will have to do the transformation for your particular case.

public class SignInFragment extends Fragment implements AdapterView.OnItemClickListener {

    //TODO: Declare constants (GUEST, HOST, EMPLOYEE...)    

    private ArrayAdapter<String> userRolesAdapter;

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

//            ListView Instance
        ListView userRolesList = root.findViewById(R.id.user_roles_list);

        String[] userRoles = {
                GUEST,
                HOST,
                EMPLOYEE
        };

//            Initialize the adapter
        userRolesAdapter = new ArrayAdapter<>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                userRoles
        );

//            Link to the list with the adapter. This reference starts the process of filling the list.
        userRolesList.setAdapter(userRolesAdapter);

//            Events
        userRolesList.setOnItemClickListener(this);

        return root;
    }

    /**
     * @param adapterView: View using the adapter from the list
     * @param view: View of the item that has been pressed
     * @param i: Refers to the position of the item that the adapter handles
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (adapterView.getId() == R.id.user_roles_list) {

//              Obtain the item pressed on the adapter with the entry position
            String currentUserRol = userRolesAdapter.getItem(i);

            assert currentUserRol != null;
            switch (currentUserRol) {
                case GUEST:
                    startActivity(Host.class);
                    break;
                case HOST:
                    startActivity(Host.class);
                    break;
                case EMPLOYEE:
                    startActivity(Employee.class);
                    break;
                default:
                    Log.d("Error", "The activity passed as an argument to startActivity() does not exist");
                    break;
            }
        }
    }

    /**
     * PRECONDITION: The class given as an argument exists.
     */
    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }
}    

Best :)

查看更多
登录 后发表回答