I implemented a listview in one of the fragments. How do I make another activity appear when I click?(Beginner)

Asked 2 years ago, Updated 2 years ago, 102 views

I implemented a listview in one of the fragments. How do I code to make another activity appear when I click? I'm a beginner. Please help me.

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View v, int position, long id) {

        }
    }) ;

If you put this in and use it, the method is different from the one in the fragment, so there is an error... Help me

android listview fragment

2022-09-22 21:57

3 Answers

Read the comments and add more content.

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
    adapterView.getItemAtPosition(pos); 
    // You can import items that you clicked on in this way.  I don't know what kind of data structure the item was made in the list view, but you can bring the item, check it, and make a branch with an if statement or case clause to call the activity. 
            }


2022-09-22 21:57

You can just write the code you wrote down. Take a listview in the Fragment, attach setOnItemClickListener to the listview, and in the onItemClick function

Intentint=newIntent(getActivity(), the activity you want to invoke.class);
startActivity(intent);

You can do it. If you show me the error log, I can see what the problem is.


2022-09-22 21:57

public class SocialFragment extends Fragment {
    Static final String[] LIST_MENU = {"★"Club List2", "Club List3", "Club List4", "Club List5", ★ Literature-related Division", "Club List8", "Club List9", "Club List10", "★", "Club List13", "Club List12"

    @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. I want a new activity window to appear when I click on the list of club list 1 and club list 2 up there. I'd appreciate your help.


2022-09-22 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.