2017-08-28 167 views
0

我有一個相當奇怪的情況。我在列表視圖中顯示一些產品的細節,這些細節是通過內容提供者從SQLite數據庫中讀取的。但是,當我點擊列表視圖的任何一行時,什麼都不會發生。所以我發佈了一些我的代碼。ListView的setOnItemClickListener不起作用

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" 

我該如何解決這個錯誤?

謝謝,

泰奧。

+0

是什麼在你的佈局? – algrid

+0

add'android:descendantFocusability =「blocksDescendants」' –

+0

我在我的列表視圖中添加了。不起作用。 – Theo

回答

1

下面行加試在的ListView你activity_main.xml中屬性是這樣的: -

android:descendantFocusability="blocksDescendants" 
android:focusable="true" 
android:focusableInTouchMode="true" 
1

從xml中的ListView中移除下面的行;

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

要麼刪除這些行

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

或使它們真正

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

感謝您的答案,但仍然沒有任何反應:(。 – Theo

+0

試圖捕獲日誌時發生點擊發生 –

+0

我試過日誌仍然沒有。 – Theo

-1

刪除這些線

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

,並添加活動

listview.setOnClickListener

相關問題