ListView的setOnItemClickListener不起作用

时间:2017-08-28 06:47:40

标签: android sqlite listview android-contentprovider

我有一个相当奇怪的情况。我在列表视图中显示一些产品及其详细信息,这些产品通过内容提供程序从SQLite数据库中读取。但是,当我单击列表视图的任何行时,没有任何反应。所以我发布了一些代码。

MainActivity

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private static final String TAG = MainActivity.class.getSimpleName();
private static final int PRODUCT_LOADER = 0;
ProductCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddProductDialog();
        }
    });

    ListView productListView = (ListView) findViewById(R.id.list_view_product);

    View emptyView = findViewById(R.id.empty_view);
    productListView.setEmptyView(emptyView);

    mCursorAdapter = new ProductCursorAdapter(this, null);
    productListView.setAdapter(mCursorAdapter);

    productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent i = new Intent(MainActivity.this,EditActivity.class);
            Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI,id);
            i.setData(currentProductUri);
            startActivity(i);
        }
    });

    getLoaderManager().initLoader(PRODUCT_LOADER, null, this);
}

private void showAddProductDialog() {
    AddDialogFragment newFragment = new AddDialogFragment();
    newFragment.show(getSupportFragmentManager(), getString(R.string.add_product));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
            ProductEntry._ID,
            ProductEntry.COLUMN_PRODUCT_NAME,
            ProductEntry.COLUMN_PRODUCT_QUANTITY,
            ProductEntry.COLUMN_PRODUCT_PRICE,
            ProductEntry.COLUMN_PRODUCT_IMAGE};

    return new CursorLoader(this,
            ProductContract.ProductEntry.CONTENT_URI,
            projection,
            null,
            null,
            null
    );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursorAdapter.swapCursor(null);
}
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
tools:context="com.example.android.invetoryapp.MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_input_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="@dimen/fab_margin" />

<ListView
    android:id="@+id/list_view_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_margin"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

    />
<RelativeLayout
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/empty_title_text"
        style="@style/EmptyTextView"
        android:text="@string/empty_view_subtitle_text" />
</RelativeLayout>

正如你所看到的那样,我正在使用这些属性,正如我发现的不同答案所示,但是当我点击一行时仍然没有任何反应。

 android:focusable="false"
 android:focusableInTouchMode="false"

我该如何解决这个错误?

谢谢,

西奥。

5 个答案:

答案 0 :(得分:1)

从xml中的ListView中删除以下行;

    android:focusable="false"
    android:focusableInTouchMode="false"

答案 1 :(得分:1)

删除这些行

android:focusable="false"
android:focusableInTouchMode="false"

或使它们成真

android:focusable="true"
android:focusableInTouchMode="true"

答案 2 :(得分:1)

尝试在 activity_main.xml ListView 属性中添加以下行,如下所示: -

b/c

答案 3 :(得分:0)

将此行添加到列表视图:

android:focusable="true"
android:focusableInTouchMode="true" 

将此行添加到具有可聚焦功能的其他视图中

android:focusable="false"
android:focusableInTouchMode="false" 

答案 4 :(得分:-1)

删除这些行

android:focusable="false"
 android:focusableInTouchMode="false"

添加活动

listview.setOnClickListener