复选框的列表视图不响应单击



>我在片段中有一个复选框的列表视图,我也实现了适配器类,它在列表视图上正确填充数据,当我单击列表中的复选框时,勾号会响起,但我无法让它执行任何其他操作(例如放置日志消息)。这是我的代码:

片段:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_contact_checklist, container, false);
    // Inflate the layout for this fragment
    database = new ContactDatabase(getActivity().getApplicationContext());
    database.open();
    ArrayList<Contact> contactList = database.getAllContacts();
    checkBoxListView = (ListView) view.findViewById(R.id.contacts_listview);
    ArrayContactCheckboxAdapter checkBoxListAdapter =
            new ArrayContactCheckboxAdapter(getActivity(), R.layout.item_checkbox, contactList);
    checkBoxListView.setAdapter(checkBoxListAdapter);
    // Setting up listener for items that are clicked on the list
    checkBoxListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CheckBox nameCheckBox = (CheckBox) view;
                    String name = nameCheckBox.getText().toString();
                    Log.d(TAG, name + " This does not get printed");
                }
            });
    Log.d(TAG, " This gets printed");
    return view;
}

适配器

public class ArrayContactCheckboxAdapter extends ArrayAdapter<Contact> {
public ArrayContactCheckboxAdapter(Context context, int resource, List<Contact> objects) {
    super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Contact c = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.item_checkbox, parent, false);
    }
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox1);
    checkBox.setText(c.getName());
    return convertView;         // Return the completed view to render on screen
}

}

想法?我需要"这不会被打印"才能打印在日志中!

您需要将

可聚焦标志和可单击标志添加到复选框中

android:focusable="false"
android:clickable="false"

而不是从列表视图列表项单击事件中控制复选框状态。

public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Get related checkbox and change flag status..
CheckBox cb = (CheckBox)v.findViewById(R.id.rowDone);
cb.setChecked(!cb.isChecked());
}

它应该使用View对象的findViewById子类

试试这个?

public class ArrayContactCheckboxAdapter extends ArrayAdapter<Contact> {
public ArrayContactCheckboxAdapter(Context context, int resource, List<Contact> objects) {
    super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Contact c = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.item_checkbox, parent, false);
    }
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox1);
    checkBox.setText(c.getName());
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) 
                    ((FragmentName)mContext).printName(c.getName());
            }
        });
    return convertView;         // Return the completed view to render on screen
    }

在你的片段上放这个函数:

public void printName(String name)
{
     Log.d(TAG, name + " This does not get printed");
}

最新更新