onListItemClick不起作用

时间:2015-07-13 07:17:55

标签: android android-listview onclick onclicklistener listitem

所以,我的onListItemClick因某些原因无效。我在另一个活动中使用相同的代码结构,而且一个工作完美。当我尝试在这里实现相同的方法时,它根本不起作用。 Toast不起作用。而且细节性的意图也不起作用。我确定我的命名和标签都很好。列表项甚至不会被点击。有人可以帮帮我吗?

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);
    }

3 个答案:

答案 0 :(得分:1)

请实现onListItemClickListener

public class MainActivity extends ListActivity implements OnItemClickListener{

//code code code

}

然后当你设置onItemCLick时使用

setOnItemClickListener(this);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

像这样:

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

我已使用此按钮分隔每个列表视图项。但它引起了你的问题。我想如果你要列出项目,你不应该使用clicable组件 模板xml。

快乐编码.. VD。

相关问题