ListFragment项目无法点击。内容提供程序和自定义游标适配器

时间:2015-03-20 04:13:53

标签: android android-contentprovider android-listfragment android-cursoradapter

我有这个简单的程序,使用Content Provider和使用Custom游标适配器填充数据。我的问题是,即使项目有"android:textIsSelectable="true",listfragment的项目也不可点击。我已完成调试,我可以看到onListItemClick未被调用。

这些是我的XML

activity_item_list.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dream , Believe and Achieve!"
        android:id="@+id/achieveButton"    
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/believe"

        />


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_list"
        android:name="com.toksis.lawofattraction.ItemListFragment" android:layout_width="match_parent"
        android:layout_height="match_parent" android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp" tools:context=".ItemListActivity"
        tools:layout="@layout/fragment_item_detail"
        android:descendantFocusability="blocksDescendants"
        />

</LinearLayout>

Fragment_item_detail.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_detail"
    style="?android:attr/textAppearanceLarge" android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true"
    tools:context=".ItemDetailFragment"
    />

ItemlistFragment Oncreate snippet。

// Create an array to specify the fields to display in the list
        // (only TITLE)
        String[] from = new String[] { WishContentProvider.COLUMN_WISH };
        // and an array of the fields to bind those fields to (in this
        // case, just text1)
        int[] to = new int[] { R.id.item_detail };
        // Now create a simple cursor adapter and set it to display

 /*   mAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_1, null, from, to, 0); */


    mAdapter = new WishCursorAdaptor(getActivity(),   

   getActivity().getContentResolver()
            .query(WishContentProvider.CONTENT_URI, from, null, null, null),
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    setHasOptionsMenu(true);


    setListAdapter(mAdapter);

    getLoaderManager().initLoader(0, null, this);

光标适配器:WishCursorAdapter

    public class WishCursorAdaptor extends CursorAdapter {


        private LayoutInflater inflater;

        public WishCursorAdaptor(Context context, Cursor c,int autoRequry) {
            super(context, c,autoRequry);

            inflater = LayoutInflater.from(context);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            return inflater.inflate(R.layout.fragment_item_detail,parent,false);


        }

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

            TextView wish = (TextView)view.findViewById(R.id.item_detail);

            wish.setText(cursor.getString(cursor.getColumnIndex("wish")));

        }
       }

enter image description here

1 个答案:

答案 0 :(得分:0)

这是怎么做的。

public class ItemListFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v= inflater.inflate(R.layout.hello, container, false);
        return v;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Do your stuff.. 
    }

}

Reference