Android如何在没有ListView的情况下为ArrayAdapter设置onclick监听器

时间:2015-01-27 11:48:29

标签: android listview android-listview

我已经按照Android的SwipeRefreshListFragment示例进行操作。 https://developer.android.com/samples/SwipeRefreshListFragment/src/com.example.android.swiperefreshlistfragment/SwipeRefreshListFragment.html

我的代码的要点在这里:

// BEGIN_INCLUDE (setup_views)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    /**
     * Create an ArrayAdapter to contain the data for the ListView. Each item in the ListView
     * uses the system-defined simple_list_item_1 layout that contains one TextView.
     */
    String[] listOfCustomAssessmentsTitles = {"first", "second", "third"};
    ListAdapter adapter = new ArrayAdapter<String>(
            getActivity(),
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            listOfCustomAssessmentsTitles);

    // Set the adapter between the ListView and its backing data.
    setListAdapter(adapter);

    // BEGIN_INCLUDE (setup_refreshlistener)
    /**
     * Implement {@link SwipeRefreshLayout.OnRefreshListener}. When users do the "swipe to
     * refresh" gesture, SwipeRefreshLayout invokes
     * {@link SwipeRefreshLayout.OnRefreshListener#onRefresh onRefresh()}. In
     * {@link SwipeRefreshLayout.OnRefreshListener#onRefresh onRefresh()}, call a method that
     * refreshes the content. Call the same method in response to the Refresh action from the
     * action bar.
     */
    setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initiateRefresh();
        }
    });
    // END_INCLUDE (setup_refreshlistener)
}
// END_INCLUDE (setup_views)

以前,在使用ArrayAdapter之前,我使用的是ArrayAdapter ListView,然后我将onClickListener设置为listView.SetOnItemClickListener(new OnItemClickListener())

新代码在这里:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout_list_of_assessments, container, false);
        if (listOfCustomAssessmentsTitles!=null){
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, listOfCustomAssessmentsTitles);
            ListView lv = (ListView)fragmentView.findViewById(R.id.customAssessmentList);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {..});
            lv.setAdapter(arrayAdapter);
        }

        //BEGIN_INCLUDE (setup_refreshlistener)
        /**
         * Implement {@link SwipeRefreshLayout.OnRefreshListener}. When users do the "swipe to
         * refresh" gesture, SwipeRefreshLayout invokes
         * {@link SwipeRefreshLayout.OnRefreshListener#onRefresh onRefresh()}. In
         * {@link SwipeRefreshLayout.OnRefreshListener#onRefresh onRefresh()}, call a method that
         * refreshes the content. Call the same method in response to the Refresh action from the
         * action bar.
         */
        setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initiateRefresh();
            }
        });
        // END_INCLUDE (setup_refreshlistener)
        return view;
    }

但是,如果我现在这样做,setOnRefreshListener将抛出空指针异常。我假设Android正在使用的SwipeRefreshFragment使用ListFragment并且它与我的ListView冲突。相反,我认为可能有一种方法可以设置onClickListener而不使用ListView,因为我已经设置了ListAdapter

任何提示?感谢。

LogCat错误:

01-27 20:06:55.173  13769-13769/com.usrs.jiarong.musrs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.usrs.jiarong.musrs, PID: 13769
    java.lang.NullPointerException
            at com.usrs.jiarong.musrs.helpers.SwipeRefreshListFragment.setOnRefreshListener(SwipeRefreshListFragment.java:54)
            at com.usrs.jiarong.musrs.fragment.Fragment_List_Of_Assessments.onCreateView(Fragment_List_Of_Assessments.java:140)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5633)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案