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条回答
Ridiculous、
2楼-- · 2019-04-17 01:46

Use this to start next intent in your onItemClickListener:

Intent intent = new Intent(getActivity(), nextactivity.class);
                    startActivity(intent);
查看更多
可以哭但决不认输i
3楼-- · 2019-04-17 01:47

Make switch statement for each items click and open activities accordingly as below:

   lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
                     int itm=arg0.getItemAtPosition(arg2);
                switch (itm) {
                case 0:
                Toast.makeText(m_context, "Position Zero", Toast.LENGTH_SHORT).show();
                                 Intent intent = new Intent(getActivity(), FirstActivity.class);
                startActivity(intent);
                    break;
                case 1:
                  Intent intent1 = new Intent(getActivity(), SecondActivity.class);
                   startActivity(intent1);
                              break;
                case 2:
                          //..............................

        }
    });
查看更多
够拽才男人
4楼-- · 2019-04-17 01:51

on Item Click you will get Position based on position you can start fragment

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),
      "Click ListItem Number " + position, Toast.LENGTH_LONG)
      .show();
       switch(position) {
        case CONST_FRAGMENT_1 :
                  //Start fragment 1
            ...
            ...
       }
  }
});
查看更多
放我归山
5楼-- · 2019-04-17 01:52

Implement your OnItemClickListener() like below

listview.setOnItemClickListener(new OnItemClickListener() {

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

                    Intent intent = new Intent(getActivity(), nextactivity.class);
                    startActivity(intent);

        }
    });
查看更多
三岁会撩人
6楼-- · 2019-04-17 01:55

one Generic solution can be ..

Create an array of items that hold class name of activity you want to open .. like ..

Class[] activityArray = new Class[numberOfItemsInListView];
            activityArray[0] = Activity1.class;

//add all activities like that..............

now on ListView onItemCLick :

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                int position, long arg3) {


                    Intent intent = new Intent(CommunityFragment.this.getActivity(), activityArray[postion]);
                    CommunityFragment.this.getActivity.startActivity(intent);

        }
    });
查看更多
对你真心纯属浪费
7楼-- · 2019-04-17 01:55

I think following code help you.

public class PdfListViewFragment extends Fragment {
    ListView listView;
    Activity rootView;
    Activity context;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        View rootView = inflater.inflate(R.layout.pdf_list_view, container, false);
        context = getActivity();
        // Get ListView object from xml
        listView = (ListView) rootView.findViewById(R.id.list);

        // Defined Array values to show in ListView
        String[] values = new String[]{"Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
                "Android Example",
                "List View Source Code",
                "List View Array Adapter",
                "Android Example List View"
        };

        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position == 0) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 1) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 2) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 3) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 4) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 5) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 6) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 7) {
                    Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
                    startActivityForResult(myIntent, 0);
                }


                // ListView Clicked item index
                int itemPosition = position;

                // ListView Clicked item value
                String itemValue = (String) listView.getItemAtPosition(position);

                // Show Alert

                Toast.makeText(context.getApplicationContext(), "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG).show();

            }

        });

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("XYZ");
    }


}
查看更多
登录 后发表回答