ListView,OnItemClickListener没有响应

时间:2012-04-16 08:37:01

标签: android listview

我有一个带有自定义baseadapter的列表视图,该类和适配器的代码如下

   public class LazyAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 


    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)

            vi = inflater.inflate(R.layout.custom_row_view1, null);

        TextView title = (TextView)vi.findViewById(R.id.linkname); // merchnts name
        TextView artist = (TextView)vi.findViewById(R.id.imagename); // address
        //TextView duration = (TextView)vi.findViewById(R.id); // distance
       // ImageView thumb_image=(ImageView)vi.findViewById(R.id.mClogo); // logo

        HashMap<String, String> jsn = new HashMap<String, String>();
        jsn = data.get(position);

        // Setting all values in listview
       title.setText(jsn.get(Second.Li_nk));
       artist.setText(jsn.get(Second.Image_name));
        //duration.setText(song.get(CustomizedListView.KEY_DURATION));
        //imageLoader.DisplayImage(jsn.get(NearBy.KEY_THUMB_URL), thumb_image);
        return vi;
    }

}

以及实现它的类如下

    HashMap<String,String> map=new HashMap<String,String>();
        map.put("Id",String.valueOf(i));
        map.put(Li_nk,cutsec);
        map.put(Image_name,j4.getString("image_name"));
        mylist.add(map);

    }

    }
    catch(JSONException e){
        Log.e("loG_tag","Error parsing"+e.toString());
    }



     list=(ListView)findViewById(R.id.lv1);
     list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        "Click ListItem Number " + position, Toast.LENGTH_LONG)
                        .show();

            }

        });
   LazyAdapter adapter = new LazyAdapter(this,mylist);

    list.setAdapter(adapter);
    list.setItemsCanFocus(false);



}

包含listview的布局是

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/zsm" />


    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:clickable="True"
         >

    </ListView>

</RelativeLayout>

并且列表的自定义布局的代码是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >




    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:clickable="True"
        android:focusable="false"


        android:background="@drawable/list_selector"
         >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="21dp"
            android:focusable="false"


            android:src="@drawable/merchntlogotitle" />


        <TextView
            android:id="@+id/imagename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/imageView1"
            android:clickable="false"
            android:focusable="false"

            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000" />


        <TextView
            android:id="@+id/linkname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imagename"
            android:layout_alignLeft="@+id/imagename"
            android:clickable="false"
            android:focusable="false"


            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

    </RelativeLayout>

</RelativeLayout>

现在设置onitemclick监听器没有响应,我做错了什么??? 我已经阅读了很多关于将焦点设置为虚假和其他人的例子,我做错了什么? 任何帮助都会受到很大的好评......

2 个答案:

答案 0 :(得分:1)

从行的布局中的所有视图项中删除以下内容,因为这些内容会分散ListView自己的项目点击事件。

android:clickable
android:focusable

例如:

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"

    android:background="@drawable/list_selector"
     >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="21dp"

        android:src="@drawable/merchntlogotitle" />


    <TextView
        android:id="@+id/imagename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/imageView1"

        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#000000" />


    <TextView
        android:id="@+id/linkname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imagename"
        android:layout_alignLeft="@+id/imagename"

        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

</RelativeLayout>

答案 1 :(得分:0)

不要在布局中设置android:clickable="True"并尝试使用它。所有可点击的视图都与父母一起绑定,因此他们的父母将无法获得点击事件。