2013-04-06 44 views
0

我有一个列表视图但不知何故,尽管我就像三重检查它的代码无法点击监听器检测和我找不到什么问题......所以请帮助我,你可以列表视图无法检测onitemclick事件

这是我下载的XML布局:

<?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="match_parent" 
    android:background="@drawable/background" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitXY" 
     android:src="@drawable/topbar" /> 

    <ImageView 
     android:id="@+id/downloaded_back_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/imageView2" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="14dp" 
     android:layout_marginLeft="22dp" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/back" /> 

    <TextView 
     android:id="@+id/downloaded_layout_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/downloaded_back_img" 
     android:layout_centerHorizontal="true" 
     android:paddingTop="5dp" 
     android:text="@string/downloaded_my_title" 
     android:textColor="@android:color/white" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

    <ListView 
     android:id="@+id/downloaded_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/imageView2" 
     android:layout_centerHorizontal="true" > 
    </ListView> 

</RelativeLayout> 

这里是列表项的XML,即时通讯填充:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_item_sel" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <ImageView 
     android:id="@+id/downloaded_album_itempic" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_gravity="center_vertical" 
     android:layout_margin="5dp" 
     android:layout_weight="10" 
     android:background="@android:color/white" 
     android:contentDescription="@string/app_name" 
     android:padding="1dip" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="70" 
     android:gravity="center_vertical" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/downloaded_album_itemtitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" 
      android:textSize="15sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/downloaded_album_itemsinger" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" /> 

     <TextView 
      android:id="@+id/downloaded_album_itemgenre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/downloaded_album_item_delete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="10" 
     android:textColor="@color/red" 
     android:background="@android:color/transparent" 
     android:contentDescription="@string/app_name" 
     android:drawableTop="@android:drawable/ic_delete" 
     android:text="@string/delete" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="10" 
     android:contentDescription="@string/app_name" 
     android:background="@drawable/item_arrow"/> 

</LinearLayout> 

和我的活动代码,也许是关系到列表视图部分:

public class Downloaded extends Activity { 
    private DatabaseHelper helper; 
    private DownloadedAlbumLazyAdapter adapter; 
    private ArrayList<Albums> temp; 
    private ListView list; 
    private ImageView back; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.downloaded); 
     init(); 
    } 

    private void init() { 
     list = (ListView) findViewById(R.id.downloaded_list); 
     back = (ImageView) findViewById(R.id.downloaded_back_img); 
     back.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       finish(); 
      } 
     }); 
     temp = new ArrayList<Albums>(); 
     helper = new DatabaseHelper(this); 
     helper.openDB(); 
     temp = helper.getAllDownloadedAlbums(); 

     adapter = new DownloadedAlbumLazyAdapter(this, temp); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View v, int position, 
        long arg3) { 
       Intent i = new Intent(); 
       i.putExtra("songs", temp.get(position).getSongs()); 
       i.setClass(Downloaded.this, DownloadedDetails.class); 
       // startActivity(i); 
       Toast.makeText(
         Downloaded.this, 
         getString(R.string.downloaded_my_title) + " " 
           + temp.get(position).getSongs().size() 
           + " items", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     helper.closeDB(); 
    } 

我无法弄清楚是什么导致了这个问题。这个活动在tabhost里面,其他活动有listview,但都有效。

+1

你必须做出可聚焦=假到所有其他ListItem对象。 – Harshid 2013-04-06 11:50:16

+0

你的logcat说什么。 – Gunaseelan 2013-04-06 11:50:37

回答

0

首先,你应该从ListActivity而不是Activity延长:

public class Downloaded extends ListActivity 

然后做任何初始化的方法onCreate()。您可以使用getListView()引用YOUT列表:

@Override 
public void onCreate() { 
    ... 
    getListView().setOnItemClickListener(...); 
    ... 
} 

如果你有一个按钮,你ListView你应该设置它的focusable属性设置为false在您适配器的getView()方法:

deleteButton.setFocusable(false); 
deleteButton.setFocusableInTouchMode(false); 

这样你列表项收到事件。

干杯!

+0

非常感谢... – 2013-04-06 11:51:12

+0

缺少一件事:'setFocusable()'用于适配器的'getView()'方法。 – Trinimon 2013-04-06 11:58:28

0

使用此

public class Downloaded extends Activity implements OnItemClickListener { 
} 


@Override 
public void onItemClick(View v){ 
} 

或者......

list.setOnItemClickListener(new View.OnItemClickListener() { 
相关问题