2012-04-16 68 views
0

我有一个自定义的baseadapter的列表视图,代码为类和适配器如下的ListView,OnItemClickListener无响应

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



} 

包含列表视图的布局是

<?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> 

和用于为l的自定义布局的代码ist is

<?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监听器没有回应,我在做什么错? 我已经阅读了大量的关于设置焦点假和其他的xamples,我做错了什么? 任何帮助将大大升级...

+0

** list.setItemsCanFocus(false); **是此行所必需的。 – ngesh 2012-04-16 08:41:13

+0

阅读本文推荐的帖子很多 – 2012-04-16 08:43:05

+0

你的列表显示项目? – waqaslam 2012-04-16 08:43:05

回答

1

从您的行布局中的所有视图项删除以下内容,因为这东西是分散ListView自己的项目单击事件。在您的布局的任何地方,并尝试

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

0

不要设置android:clickable="True"

android:clickable 
android:focusable 

例如。所有可点击的视图都与父母绑定,因此他们的父母将无法获得点击事件。

相关问题