我有一个片段中复选框的列表视图,我也实现了适配器类,它在listview上正确填充数据,当我点击列表中的复选框时,勾选将关闭然后打开,但我不能让它执行任何其他操作(比如放置日志消息)。这是我的代码:
片段:
@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
}
}
想法?我需要&#34;这不会打印&#34;打印在日志中!
答案 0 :(得分:1)
您需要将可聚焦标志和可点击标志添加到复选框
android:focusable="false"
android:clickable="false"
比从ListView onListItemClick事件中控制复选框状态。
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());
}
它应该使用findViewById
对象的View
子类
答案 1 :(得分:0)
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");
}