0

我有一个列表视图,但不知何故它无法检测到点击侦听器,尽管我已经检查了三次代码并且我找不到问题所在......所以请帮助我,如果你能

这是我下载的 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>

这是我填充的 ​​listitem 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 中,其他活动有列表视图,但都可以工作。

4

2 回答 2

0

首先,您应该扩展 fromListActivity而不是Activity

public class Downloaded extends ListActivity

onCreate()然后在方法中进行任何初始化。您可以使用getListView()参考您的列表:

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

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

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

这样,您的列表项就会收到事件。

干杯!

于 2013-04-06T11:46:51.477 回答
0

用这个

public class Downloaded extends Activity implements OnItemClickListener {
}


@Override
public void onItemClick(View v){
}

或者...

list.setOnItemClickListener(new View.OnItemClickListener() {
于 2013-04-06T12:16:13.870 回答