2017-11-13 15 views
0

私はデータベース内のレコードによってチェックボックスを動的に作成しています。私は正常にデータベースの各レコードのcheckboxechを生成することができます。チェックボックスをすべてデータベースに追加するにはどうしたらいいですか?

ボタンをクリックした後、チェックしたチェックボックスに基づいて別のテーブルにレコードを追加したいとします。すべてのチェックボックスを調べる必要があります。チェックされているかどうかを確認し、YESの場合はidをid_participantに渡してレコードを作成する必要があります。しかし、私はそれを行う方法を失っています。

お手伝いできますか?

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    myDB = new DBAdapter(getContext()); 
    final View root = inflater.inflate(R.layout.fragment_add_from_db, container, false); 
    final LinearLayout layout = root.findViewById(R.id.check_add_layout); 
    btn_ad_group = root.findViewById(R.id.btn_add_group); 
    Bundle bundle = getArguments(); 
    if (bundle != null) { 
     id_eventu = bundle.getLong("idevent"); 
    } 
    myDB.open(); 
    pocet = myDB.getParticipantsCount(); 
    Cursor cursor = myDB.getAllRowsParticipant(); 
    cursor.moveToFirst(); 
    for(int i = 0; i < pocet; i++) { 
     CheckBox cb = new CheckBox(root.getContext()); 
     String name = cursor.getString(cursor.getColumnIndex("name")); 
     String surname = cursor.getString(cursor.getColumnIndex("surname")); 
     String text = name + " " + surname; 
     cb.setText(text); 
     cb.setHeight(90); 
     cb.setId(cursor.getInt(cursor.getColumnIndex("_id"))); 
     layout.addView(cb); 
     cursor.moveToNext(); 
    } 
    myDB.close(); 

    btn_ad_group.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      myDB.open(); 
      // here I want to write into database by selected checkboxes 
      // I want to read id of checked checkbox and pass it to id_participant 
      myDB.insertRowRegistr(id_eventu, id_participant); 
      myDB.close(); 

     } 
    }); 

    return root; 
} 

答えて

1

あなたは多くの情報を与えていないので、私はいくつか考えます。

ことのLinearLayout「レイアウトは」チェックボックスの親である場合は、2つの関数を使ってその子にアクセスすることができるはずです。

int numChildren = layout.getChildCount(); 
for(int i=0;i<numChildren;i++){ 
    //create a temporary checkbox variable 
    //make it equal to layout.getChildAt(i); 
    //if temp is checked, add it to a list or hand it straight to database 

は動作するはずです。アンドロイド内でチェックボックスやデータベースを使用したことはありませんが、各チェックボックスにアクセスしてクリックされているかどうかを確認する必要があります。

UPDATE

私は、必要に応じてこのコードは動作します。手伝ってくれてありがとう!

btn_ad_group.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      myDB.open(); 
      int numChildren = layout.getChildCount(); 
      for(int i=0;i<numChildren;i++){ 
       CheckBox temp; 
       temp = (CheckBox) layout.getChildAt(i); 
       if (temp.isChecked()) { 
        id_participant = (long) temp.getId(); 
        myDB.insertRowRegistr(id_eventu, id_participant); 
       } 
      } 
      myDB.close(); 
      fm.popBackStackImmediate(); 
     } 
    }); 
+0

これはリサイクラビューかリストビューですか? – Richardweber32

+0

レイアウトはLinearLayoutのみ – Raddino

+0

それからあなたに子供を連れてくるはずです。それがうまくいかない場合は、 – Richardweber32

関連する問題