OnItemClick不起作用

时间:2014-05-09 06:57:33

标签: android

这是我的适配器类代码

在这里,我试图点击列表项,并希望重定向到关于所点击项目的下一个活动。

但OnItemCLick无效

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代码

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无效

3 个答案:

答案 0 :(得分:0)

对于这个问题,只需用代码替换onitemclick监听器中的代码,

            System.out.println("enterd enterd");
            Intent i=new Intent(AdvancedActivity.this,DemographicsActivity.class);
            startActivity(i);
            finish();

答案 1 :(得分:0)

因为在列表的项目视图中,您有一个Button视图,并且在整个列表项可以进行单击之前,它正在获得焦点。

所以你只需要在列表项的布局的根布局元素中添加一个属性,即。

在layout / advanced_list_item file.xml的根元素中

<强> 机器人:descendantFocusability = “blocksDescendants”

<?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都可以正常工作。

答案 2 :(得分:0)

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