使用CursorAdapter的Android ListView项目仅在点击几次后才能工作

时间:2017-10-11 10:39:21

标签: android listview android-cursoradapter onitemclicklistener onitemclick

这是 ListFragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_list, container, false);

        listList = (ListView) root.findViewById(R.id.list_list);
        mListAdapter = new ListCursorAdapter(getActivity(),null);
        FloatingActionButton addFab = (FloatingActionButton) root.findViewById(R.id.fabAdd);
        addFab.setFocusable(false);
        addFab.setFocusableInTouchMode(false);

        mDBAdapter = new DBAdapter(getContext());
        mDBAdapter.open();

        id = new ArrayList<Integer>();
        nombres = new ArrayList<String>();
        latitudes = new ArrayList<Double>();
        longitudes = new ArrayList<Double>();
        descripciones = new ArrayList<String>();
        categorias = new ArrayList<Integer>();


        listList.setAdapter(mListAdapter);

        empty = (TextView) root.findViewById(R.id.tvEmpty);
        Cursor listas = mDBAdapter.getListaListas();
        if(listas==null || listas.getCount()<=0) empty.setVisibility(View.VISIBLE);

        listList.setClickable(true);
        AdapterView.OnItemClickListener myListViewClicked = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cur = mDBAdapter.getListaListas();
                int id_lis;
                String name_lis;
                if (cur.moveToFirst()) {
                    for (int k=0;k<i;k++){
                        cur.moveToNext();
                    }
                }
                id_lis = cur.getInt(0);
                name_lis = cur.getString(1);
                Intent intent = new Intent(getContext(), MarkerListActivity.class);
                intent.putExtra("numLista", id_lis);
                Log.i("id_lista", ""+ id_lis);
                intent.putExtra("nameLista", name_lis);
                startActivity(intent);
            }
        };

        listList.setOnItemClickListener( myListViewClicked );
        listList.setLongClickable(true);

它还有一个显示一些选项的长按事件。起初,我把Intent放在那里,但这是常用的方法。

适配器它是一个普通的CursorAdapter,其中只有一个textview。 这些是XML文件:

LIST_ITEM

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/click"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false">

        <TextView
            android:id="@+id/list_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:longClickable="true"
            android:clickable="false"
            android:focusableInTouchMode="false"
            android:focusable="false"/>


        <View xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:background="#B6B6B6"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            />
    </LinearLayout>

</LinearLayout>

这是 fragment_list 布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListFragment"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants">

    <ListView
        android:id="@+id/list_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/tvEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:hint="Ninguna lista para mostrar"
        android:textSize="25sp"
        android:textStyle="bold"
        android:visibility="invisible"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabAdd"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="12dp"
        android:tint="@android:color/white"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_action_add" />
</FrameLayout>

那么,如何通过简单的点击让onItemClick正常工作呢?打开新活动总是需要四次或更多次点击。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

您的AsyncTask方法在UI线程上执行数据库操作。这是你应该避免的,它会导致糟糕的用户体验。将代码包含在execute@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.d("onItemClick", "clicked Item") AsyncTask task = new AsyncTask<Void, Void, Void>() { private int id_lis; private String name_lis; protected Long doInBackground(Void... voids) { cur = mDBAdapter.getListaListas(); if (cur.moveToFirst()) { for (int k=0;k<i;k++){ cur.moveToNext(); } } id_lis = cur.getInt(0); name_lis = cur.getString(1); } protected void onPostExecute(Long result) { Intent intent = new Intent(getContext(), MarkerListActivity.class); intent.putExtra("numLista", id_lis); Log.i("id_lista", ""+ id_lis); intent.putExtra("nameLista", name_lis); startActivity(intent); } }.execute(); } 内,如下所示:

double litre; double gallon;