2017-01-10 5 views
0

私はフラグメント内のチェックボックスのリストビューを持っていますが、アダプタクラスも実装しており、リストビューのデータを正しく入力しています。リストのチェックボックスをクリックするとダニが出てきますが、(ログメッセージを入れるなどの)何かを実行することができません。ここに私のコードは次のとおりです。CheckBoxの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 
} 

}

のアイデア?ログに印刷するには「これは印刷されない」必要があります!

+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 } 

は、この機能を置きますか? – Alexandre

+0

個人的に私は 'onClick'というXMLを使用しています。 'android:onClick =" sledone "'クラスとpublicにメソッド 'sledone'を持たなければなりません。また、それはargとしてViewを持たなければなりません。例えば'public void sledone(view view){....}' – MikeT

+0

私のアダプタで試してみましたが、動作しませんでした。 –

答えて

1

あなたはリストビューonListItemClickイベントの中から、チェックボックスの状態をコントロールよりも、チェックボックス

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オブジェクト

+0

これは動作します、ありがとう!!!フォーカス可能/クリック可能について何も知らなかった! –

0

findViewByIdサブクラスを使用すべきですか?あなたは、アダプタ内で直接あなたのコードを入れてみました、

public void printName(String name) 
{ 
    Log.d(TAG, name + " This does not get printed"); 
} 
関連する問題