ListView的setOnItemClickListener()无法访问

时间:2015-01-12 09:34:10

标签: android

我在对话框中需要一个列表视图,每行包含应用程序,图标和复选框的名称。 我使用了生成每一行的适配器类。 我做了:

lv = (ListView) findViewById(R.id.aplist);
lv.setAdapter(applistadapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                     /* some code*/
            } });

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
	private List<ApplicationInfo> appsList = null;
	private Context context;
	private PackageManager packageManager;
	private TextView appName;
	private ImageView iconview;
	CheckBox check;
	boolean[] itemChecked;

	public ApplicationAdapter(Context context, int textViewResourceId,
			List<ApplicationInfo> appsList) {
		super(context, textViewResourceId, appsList);
		this.context = context;
		this.appsList = appsList;
		packageManager = context.getPackageManager();
		itemChecked = new boolean[appsList.size()];
		Log.wtf("c", "34werty");
	}

	@Override
	public int getCount() {
		return ((null != appsList) ? appsList.size() : 0);
	}
	
	@Override
	 public boolean isEnabled(int position) {
		return true;
	}

	@Override
	public ApplicationInfo getItem(int position) {
		Log.wtf("c", "23werty");
		return ((null != appsList) ? appsList.get(position) : null);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (null == view) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.row, null);
			Log.wtf("c", "12werty");
		}

		ApplicationInfo data = appsList.get(position);
		if (null != data) {
			Log.wtf("c", "werty");
			appName = (TextView) view.findViewById(R.id.app_name);
			// TextView packageName = (TextView)
			// view.findViewById(R.id.app_paackage);
			iconview = (ImageView) view.findViewById(R.id.app_icon);
			check = (CheckBox) view.findViewById(R.id.checkbox1);

			appName.setText(data.loadLabel(packageManager));
			// packageName.setText(data.packageName);
			iconview.setImageDrawable(data.loadIcon(packageManager));
			 
			check.setChecked(false);
			if (itemChecked[position])
				check.setChecked(true);
			else
				check.setChecked(false);

			check.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (check.isChecked())
						itemChecked[position] = true;
					else
						itemChecked[position] = false;
					 check.setTag(position);
				}
			});

		}

		return view;
	}
};

现在的问题是控件永远不会转到setOnItemClickListener()。

请帮我解决。

谢谢。

1 个答案:

答案 0 :(得分:0)

问题可能是listView中的元素捕获listView之前的单击。要避免这种情况,请在listView layout xml:

中设置此属性
     android:descendantFocusability="blocksDescendants"

或以编程方式:

     lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

或者你也可以在listView中停用你的元素的焦点,例如:

     yourCheckBox.setFocusable(false);