onListItemClick 不起作用

onListItemClick doesn't work

因此,我的 onListItemClick 由于某种原因无法正常工作。我在另一个 activity 中使用了相同的代码结构,那个代码结构运行良好。当我尝试在这里实现相同的方法时,它根本不起作用。 Toast 不起作用。而且 detailactivity 的意图也不起作用。我确定我的命名和标签没问题。列表项甚至感觉不到它们是可点击的。有人可以帮我吗?

public class MainActivity extends ListActivity {

    String objectId;
    protected List<ParseObject> mInfo;

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

    //**** There is a bunch of code here that takes import the list view from an adapter. I believe they are working fine. ***//
    }

    //**** this part below is not working. This is the section where if you click on the list item, it brings you to the detailactivity.
    @Override
    protected void onListItemClick(ListView l,View v, int position, long id){
        super.onListItemClick(l, v, position, id);

        ParseObject infoObject = mInfo.get(position);
        String objectId = infoObject.getObjectId();

        Toast.makeText(getApplicationContext(),objectId, Toast.LENGTH_SHORT).show();

        Intent Details = new Intent(MainActivity.this, DetailsActivity.class);
        Details.putExtra("objectId", objectId);
        startActivity(Details);
    }

请实现 onListItemClickListener

public class MainActivity extends ListActivity implements OnItemClickListener{

//code code code

}

然后当您设置 onItemCLick 时使用

setOnItemClickListener(this);

我通过在 listview/customlayout 的视图中添加 android:focusable="false" 来解决这个问题,该视图将被导入到 activity。我从这个 link 得到了答案: onListItemClick is not working for listview?

İ如果您使用自定义适配器来填充列表视图并涉及可点击组件,例如 Button、İmageView。 onListItemClick 将不起作用,因为它无法决定将监听哪个组件。解决方案是 "android:focusable:"false"" 关键字。多亏了这一点,Listener 将只关注由您填充的自定义适配器的项目。

像这样:

<Button
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="colorCode"
/>

我用这个按钮来分隔每个列表视图项目。但它导致了你的问题。我想如果你要列出项目,你不应该在 模板 xml.

快乐编码.. Vd。