English 中文(简体)
启用列表拼放中的列表项目点击
原标题:Enabling an onListItemClick in a ListFragment

我有一个列表拼版, 正在从一个 ActionBar 的 Tab 导航中调用 。 我有一个自定义的调整器, 要为每个列表项目显示两种不同的文本视图 。 我无法执行在 TroupClickListlearker 上的工作 。 以下是我的列表拼贴代码 :

public class MaterialsListFragment extends SherlockListFragment {

    public ERGAdapter db;   

    @Override
    public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) {   
        return inflater.inflate(R.layout.portrait_material_view, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // create new DBAdapter
        db = new ERGAdapter(getActivity());

        // ---- get all records
        db.open();      // Open database

        // Get all of the notes from the database and create the item list
        Cursor c = db.getAllRecords();

        String[] from = new String[] { ERGAdapter.KEY_IDNO, ERGAdapter.KEY_MATERIAL };
        int[] to = new int[] { R.id.idno, R.id.materials };        

        // Now create an array adapter and set it to display using our row
        MyListAdapter materials = new MyListAdapter(getActivity(), R.layout.list_cell, c, from, to);
        setListAdapter(materials);

        // get the list view and the set the listener
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Log.i("MATERIAL"," - position: "+Integer.toString(position));
            }
        });
    }   

    public class MyListAdapter extends SimpleCursorAdapter {

        public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
            super(context, layout , cursor, from, to);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {


            // Create the idno textview with background image
            TextView idno = (TextView) view.findViewById(R.id.idno);
            String unidno = String.format("%1$.0f", cursor.getDouble(3));
            idno.setText(unidno);

            // create the material textview
            TextView materials = (TextView) view.findViewById(R.id.materials);
            materials.setText(cursor.getString(1));
        }
    } 
}

此行的致命例外: ListView lv = getListView ();

以下是相关的日志 :

05-25 16:32:45.802: E/AndroidRuntime(660): Caused by: java.lang.IllegalStateException: Content view not yet created
05-25 16:32:45.802: E/AndroidRuntime(660):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
05-25 16:32:45.802: E/AndroidRuntime(660):  at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
05-25 16:32:45.802: E/AndroidRuntime(660):  at com.cca.ergpro.MaterialsListFragment.onCreate(MaterialsListFragment.java:80)

我认为我需要把它作为我习惯的CursorAdapter的一部分,但我不知道该如何着手。 任何建议都将非常感激。

最佳回答

利用这个问题的解决方案,我得以让它工作:

< a href=" "https://stackoverflow.com/ questions/9690439/Create-Custom-simple-cursor-adapter" > 创建自定义的简单光标适配器

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签