简体   繁体   English

从片段内的ListView开始活动

[英]Start an activity from the ListView inside a fragment

I want to start an activity from the listview elements made inside a fragment. 我想从片段内的listview元素开始活动。 I have searched for this but didn't got any solution. 我已经搜索过了,但是没有任何解决方案。 I have tried everything i got from stackoverflow earlier asked questions but nothing worked and every time my app crashed. 我已经尝试了我从stackoverflow获得的所有内容,但我的应用程序每次崩溃都无济于事。

Here is my code. 这是我的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag_cse1_notes, container, false);
ListView sem1List = (ListView) rootView.findViewById(R.id.sem1Lv);
final ArrayList<String> sem1Courses = new ArrayList<String>();
    sem1Courses.add("Applied Physics 1");
    sem1Courses.add("Applied Mathematics 1");
    sem1Courses.add("Engineering Mechanics");
    sem1Courses.add("English Language Usage Essentials");
ArrayAdapter<String> adapterSem1 = new ArrayAdapter<String>(getActivity(),
                                  android.R.layout.simple_list_item_1, sem1Courses);
sem1List.setAdapter(adapterSem1);
sem1List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final Intent intent;
switch (position){
case 0:
    intent = new Intent(getActivity(), phy1Details.class);
    startActivity(intent);
    break;
case 1:
    intent = new Intent(getActivity(), appMath1Details.class);
    startActivity(intent);
    break;
case 2:
    intent = new Intent(getActivity(), engMech1Details.class);
    startActivity(intent);
    break;
case 3:
    intent = new Intent(getActivity(), engLangUseDetails.class);
    startActivity(intent);
    break;
        }
    });
return rootView;
}

Here is my xml file 这是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:transcriptMode="alwaysScroll"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvSem1"
            />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="550dip"
            android:id="@+id/sem1Lv"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        </LinearLayout>`
    </ScrollView>
</RelativeLayout>

Try this: 尝试这个:

intent = new Intent(rootView.getContext(), phy1Details.class);
view.getContext().startActivity(intent);

Just set your itemClickListener inside onActivityCreated . 只需在onActivityCreated设置itemClickListener

Your activity is not initiated inside onCreateView yet, so you're probably experiencing a NullPointerException. 您的活动尚未在onCreateView内部启动,因此您可能遇到了NullPointerException。

You can use 您可以使用

setOnItemClickListener setOnItemClickListener

on list view and start your new activity. 在列表视图上,然后开始新的活动。 By clicking on any value in your list view you the will the value as well as you can start any operation with that. 通过单击列表视图中的任何值,您将获得该值以及可以对此进行任何操作。

See the following code: Here 请参见以下代码:

selected

variable stored the value of selected item in the list view. 变量将所选项目的值存储在列表视图中。

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


            status=true;
            String selected = ((TextView) view.findViewById(R.id.singleMessage)).getText().toString();

            //Toast.makeText(getApplicationContext(),"selected"+selected ,Toast.LENGTH_LONG).show();



            i.putExtra("place_lat", lat);
            i.putExtra("place_lng", lng);
            startActivity(i);

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM