首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnItemClick不工作

OnItemClick不工作
EN

Stack Overflow用户
提问于 2014-05-09 14:57:33
回答 3查看 130关注 0票数 0

下面是我的适配器类代码

在这里,我正在尝试单击列表项,并希望重定向到与所单击的项相关的下一个活动。

但是OnItemCLick不工作

代码语言:javascript
复制
public class AdvancedAdapter extends BaseAdapter {

    public AdvancedAdapter()  {
        super();
    }

    public String getAttributeName(int position) {      
        switch (position) {
        case 0:
            return new String("User Profile");
        case 1:
            return new String("Reset Energy Expended");
        case 2:
            return new String("Reset Pedometer");
        case 3:
            return new String("Organizing Data Tiles");
        case 4:
            return new String("Cloud Upload Period");
        case 5:
            return new String("Auto Reconnect");
        case 6:
            return new String("Watches");
        case 7:
            return new String("Notes");
        case 8:
            return new String("Module Controls");
        case 9:
            return new String("Sencor Module Info");
        case 10:
            return new String("Expert Settings");
        default:
            break;
        }
        return null;
    }

    public void getAttributeValue(int position) {

        switch (position) {
        case 0:


        case 1:forward.setVisibility(View.GONE);

        case 2:forward.setVisibility(View.GONE);

        case 3:forward.setVisibility(View.GONE);

        case 4:
        case 5:
        case 6:
        case 7:
        case 8:

        break;
        default:
            break;
        }

    }

    @Override
    public int getCount() {
        return 11;
    }

    @Override
    public Object getItem(int position) {
        switch (position) {
        case 0:
        case 1:
        case 2:
        case 3:
        default:
            break;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // get already available view or create new if necessary
        FieldReferences fields;
        if (convertView == null) {
            convertView = (View)getLayoutInflater().inflate(R.layout.advanced_list_item, null,false);
            fields = new FieldReferences();
            fields.Name = (TextView)convertView.findViewById(R.id.advanced_list_item_text);
            fields.Button = (Button)convertView.findViewById(R.id.advanced_list_item_button);
            convertView.setTag(fields);
        } else {
            fields = (FieldReferences) convertView.getTag();
        }           

         // set proper values into the view
        /*String value = getAttributeValue(position);*/
        String name = getAttributeName(position);

        fields.Name.setText(name);


        return convertView;
    }

    private class FieldReferences {
        TextView Name;
        @SuppressWarnings("unused")
        Button Button;
    }
}

和我的onitemclick代码

代码语言:javascript
复制
AdvancedList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        if(arg2==0){
                System.out.println("enterd enterd");
                Intent i=new Intent(AdvancedActivity.this,DemographicsActivity.class);
                startActivity(i);
                finish();
            }
        }

    });

当尝试单击列表项时,OnItemclick不工作

EN

回答 3

Stack Overflow用户

发布于 2014-05-09 15:14:30

对于这个问题,只需将onitemclick listener中的代码替换为code,

代码语言:javascript
复制
            System.out.println("enterd enterd");
            Intent i=new Intent(AdvancedActivity.this,DemographicsActivity.class);
            startActivity(i);
            finish();
票数 0
EN

Stack Overflow用户

发布于 2014-05-09 16:29:35

因为在list的item视图中,您有一个Button视图,并且在整个list item能够接受点击之前,它已经获得了焦点。

因此,您只需在列表项目的布局ie的根布局元素中添加一个属性。

在布局的根元素/advanced_list_item file.xml中

android:descendantFocusability="blocksDescendants"

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/test_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello" />

    <TextView
        android:id="@+id/test_txv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Hello World" />

</LinearLayout>

类似于上面的内容

现在你的两个listview的onItemClickListener和你的按钮的onClickListener都可以正常工作了。

票数 0
EN

Stack Overflow用户

发布于 2015-07-11 05:18:21

在花了一整天的时间解决同样的问题后,我意识到最好的方法是在所有的TextView中添加android:focusableInTouchMode="false" android:clickable="false" android:focusable="false",并在根VieGroup中添加android:descendantFocusability="blocksDescendants"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23558381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档