Android ListView的setOnItemClickListener不';行不通
我有一个相当奇怪的情况。我在listview中显示一些产品及其详细信息,这些产品通过内容提供商从SQLite数据库读取。但是,当我在列表视图的任何一行中单击时,什么都不会发生。所以我发布了我的一些代码 main活动Android ListView的setOnItemClickListener不';行不通,android,sqlite,listview,android-contentprovider,Android,Sqlite,Listview,Android Contentprovider,我有一个相当奇怪的情况。我在listview中显示一些产品及其详细信息,这些产品通过内容提供商从SQLite数据库读取。但是,当我在列表视图的任何一行中单击时,什么都不会发生。所以我发布了我的一些代码 main活动 public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{ private static final String TAG =
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);
}
}
我怎样才能纠正这个错误
谢谢
Theo.从xml列表视图中删除以下行
android:focusable="false"
android:focusableInTouchMode="false"
从xml的ListView中删除以下行
android:focusable="false"
android:focusableInTouchMode="false"
要么拆下这些线
android:focusable="false"
android:focusableInTouchMode="false"
或者让它们成为现实
android:focusable="true"
android:focusableInTouchMode="true"
要么拆下这些线
android:focusable="false"
android:focusableInTouchMode="false"
或者让它们成为现实
android:focusable="true"
android:focusableInTouchMode="true"
拆下这些线
android:focusable="false"
android:focusableInTouchMode="false"
和附加活动
listview.setOnClickListener删除这些行
android:focusable="false"
android:focusableInTouchMode="false"
和附加活动
listview.setOnClickListener尝试在活动的main.xml的listview属性中添加以下行,如下所示:-
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
尝试在活动的列表视图属性\u main.xml中添加以下行,如下所示:-
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
将以下行添加到listview:
android:focusable="true"
android:focusableInTouchMode="true"
将此线添加到具有聚焦功能的其他视图:
android:focusable="false"
android:focusableInTouchMode="false"
将以下行添加到listview:
android:focusable="true"
android:focusableInTouchMode="true"
将此线添加到具有聚焦功能的其他视图:
android:focusable="false"
android:focusableInTouchMode="false"
你的单元格布局中有什么?添加我在我的列表视图中添加的。不起作用。但是为什么仍然使用
ListView
RecylerView
比具有自己视图持有者模式的ListView要好。@Piyus。那将是下一个层次。现在我只使用listview。你的单元格布局中有什么?添加我在listview中添加的。不起作用。但是为什么仍然使用ListView
RecylerView
比具有自己视图持有者模式的ListView要好。@Piyus。那将是下一个层次。现在我只使用listview。现在请删除意图代码并尝试此代码,然后在logcat中查看。在logcat中单击log可以看到这一点吗?public void onItemClick(AdapterView AdapterView,View-View,int-position,long-id){Log.d(“onclick”,“onclick”);}无任何内容。当我单击一行时,我将获得以下D/AbsListView:onTouchUp()mTouchMode:1,以便现在删除意图代码并尝试此代码并在logcat中查看。在logcat中单击log可以看到这一点吗?public void onItemClick(AdapterView AdapterView,View-View,int-position,long-id){Log.d(“onclick”,“onclick”);}无任何内容。当我单击一行时,我得到的是D/AbsListView:onTouchUp()mTouchMode:1