我有一个listview,但不知何故它无法检测点击监听器,虽然我已经检查了代码,我找不到问题所以...所以如果你能帮助我
这是我下载的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中,其他活动具有列表视图,但都可以正常工作。
答案 0 :(得分:0)
首先,您应该从ListActivity
而不是Activity
延伸:
public class Downloaded extends ListActivity
然后在onCreate()
方法中进行任何初始化。您可以使用getListView()
来引用您的列表:
@Override
public void onCreate() {
...
getListView().setOnItemClickListener(...);
...
}
如果您的ListView
中有一个按钮,则应在适配器的focusable
方法中将其getView()
属性设置为false:
deleteButton.setFocusable(false);
deleteButton.setFocusableInTouchMode(false);
这样您的列表项就会收到该事件。
干杯!
答案 1 :(得分:0)
使用此
public class Downloaded extends Activity implements OnItemClickListener {
}
@Override
public void onItemClick(View v){
}
... OR
list.setOnItemClickListener(new View.OnItemClickListener() {