(There is a code) Listview in one fragment... How do I make each of the listview items appear in different activities?(Beginner)

Asked 2 years ago, Updated 2 years ago, 60 views

 static final string[] LIST_MENU = {"★"Club List2", "ClubList3", "ClubList4", "ClubList5", ★ LiteratureList7", "ClubList8", "ClubList9", "ClubList13", "ClubList12", "ClubList"

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.social_layout, null);
        ArrayAdapter Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, LIST_MENU);

        ListView listview = (ListView) view.findViewById(R.id.listView);
        listview.setAdapter(Adapter);
        return view;

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
        adapterView.getItemAtPosition(pos);
        if (LIST_MENU == null) {
            Intent intent = new Intent(getActivity(), List1.class);
            startActivity(intent);
        }
    }
If you write }; //this part), an error appears under the red underline. Please solve this, too.
    }

}

It's coded like this. I'm not sure how to use that if clause over there. ㅠ 저기 When I click on the list of club list 1 and club list 2 above, I want to have a new activity window appear. I'd appreciate it if you could help me

android listview fragment

2022-09-21 21:02

1 Answers

If you fix the error part, it will be as follows.

static final string[] LIST_MENU = {"★"Club List2", "Club List3", "Club List4", "Club List5", ★ Literature-related Division "," "Club List7", "Club List8", "Club List9", "Club 14", "Club List12", "Club List", "Club List", "List", "Club 12"

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.social_layout, null);
        ArrayAdapter Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, LIST_MENU);

        ListView listview = (ListView) view.findViewById(R.id.listView);
        listview.setAdapter(Adapter);
        // Return view; move this part to the end.
        // After return, no code is executed. Therefore, the following line is reported as an error.

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                adapterView.getItemAtPosition(pos);
                if (LIST_MENU == null) {
                    Intent intent = new Intent(getActivity(), List1.class);
                    startActivity(intent);
                }
            }
        });. // This sentence is not a function declaration, it is the part that calls the function.
       return view; // Therefore, return should come here.
    }

This error occurred due to the location of the return statement.

In addition, the index above the currently selected item in the array LIST_MENU is the intpos factor.

There are many ways, and if I can tell you a simple way, try the following.

static final String[] LIST_MENU = { ... }
static final Class<?>[] ACTIVITIES = {ActivityA.class, ActivityB.class, ...} // Write the name of each class of activity corresponding to the element of each LIST_MENU. 
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
    Intent intent = new Intent(getActivity(), ACTIVITIES[pos]);
    startActivity(intent);
}


2022-09-21 21:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.