求指教 Listview设置OnItemClickListener事件点击没有响应
package com.wanghui;import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.wanghui.App.AppInfo;
public class MainActivity extends Activity {
private ListView appList;
private ArrayList<AppInfo> app_ArrayList = new ArrayList<AppInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appList = (ListView) findViewById(R.id.listview);
List<PackageInfo> list = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < list.size(); i++) {
PackageInfo packageInfo = list.get(i);
AppInfo appInfo = new AppInfo();
appInfo.AppName = packageInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
appInfo.pageName = packageInfo.applicationInfo.packageName;
appInfo.AppIcon = packageInfo.applicationInfo
.loadIcon(getPackageManager());
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
app_ArrayList.add(appInfo);
}
}
MainAdapter mAdapter = new MainAdapter(app_ArrayList, MainActivity.this);
appList.setAdapter(mAdapter);
// 列表现的单机事件
appList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
System.out.println("listView点击事件");
}
});
}
}
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:id="@+id/appicon"
android:layout_width="50dip"
android:layout_height="50dip"
android:focusable="false"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/app_title"
android:paddingLeft="15dip"
android:layout_width="match_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:focusable="false"
android:text="应用名称"
android:textSize="20sp" />
</LinearLayout>
package com.wanghui;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wanghui.App.AppInfo;
public class MainAdapter extends BaseAdapter
{
ArrayList<AppInfo> appInfoList = new ArrayList<AppInfo>();
Context mContext;
public MainAdapter(ArrayList<AppInfo> appList,Context context)
{
// TODO Auto-generated constructor stub
mContext = context;
for (int i = 0; i < appList.size(); i++)
{
appInfoList.add(appList.get(i));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return appInfoList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return appInfoList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
final AppInfo appInfo = appInfoList.get(position);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= (LinearLayout) layoutInflater.inflate(R.layout.applist_item, null);
convertView.setClickable(true);
}
ImageView appIcon = (ImageView) convertView.findViewById(R.id.appicon);
TextView appName = (TextView) convertView.findViewById(R.id.app_title);
if(appIcon != null)
{
appIcon.setImageDrawable(appInfo.AppIcon);
}
if(appName != null)
{
appName.setText(appInfo.AppName);
}
return convertView;
}
}
这是代码 求指教!谢谢!
--------------------编程问答-------------------- 你看看是否检点没有在listview上面!你在XML文件里面设置一下焦点试一试哈! --------------------编程问答-------------------- convertView.setClickable(true);
把上面这句话删除就行了。
补充:移动开发 , Android