如何在表中使用动态创建的复选框


how to use dynamically created checkboxes inside a table

我上面的代码创建了一个包含复选框和文本视图的表。计数和值来自php json上的MySQL数据库。一切都很好。问题:在onClickSenden函数中,我想一步一步地检查每个未选中或已选中的复选框,并根据.执行一些代码

不可能设置Id(i),因为"i"是一个迭代器,而不是内部

你能帮我吗?

public class EinbuchungActivity extends Activity implements OnCheckedChangeListener {
    String data = "";
    TableLayout tl;
    TableRow tr;
    TextView label;
    CheckBox cb; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_einbuchen);
        TextView tv = (TextView) findViewById(R.id.tvNutzer);
        tv.setText(MainActivity.username);
        TextView tv2 = (TextView) findViewById(R.id.tvKst);
        tv2.setText(MainActivity.kst);
        tl = (TableLayout) findViewById(R.id.maintable);
        final GetDataFromDB getdb = new GetDataFromDB();
        new Thread(new Runnable() {
            public void run() {
                data = getdb.getDataFromDB();
                System.out.println(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Users> users = parseJSON(data);
                        addData(users);                     
                    }
                });
            }
        }).start();
    }
    public ArrayList<Users> parseJSON(String result) {
        //Array-Liste erstellen mit definierter Arrayliste
        ArrayList<Users> users = new ArrayList<Users>();
        try {
            JSONArray jArray = new JSONArray(result);
            //Array-Liste mit Daten füllen
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Users user = new Users();
                user.setEan(json_data.getString("ean"));
                user.setName(json_data.getString("name"));
                user.setBetriebszahl(json_data.getString("betriebsdatenalt"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());  
        }
        return users;
    }
    void addHeader(){
        /** Create a TableRow dynamically **/
        tr = new TableRow(this);
        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText("Inventarnummer");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        //label.setBackgroundColor(Color.RED);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label,params);
        tr.addView((View)Ll); // Adding textView to tablerow.
        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText("Name");
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place.setPadding(5, 5, 5, 5);
        //place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place,params);
        tr.addView((View)Ll); // Adding textview to tablerow.
         // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Users> users) {
        addHeader();    
         for (Iterator i = users.iterator(); i.hasNext();) {
            Users p = (Users) i.next();
            /** Create a TableRow dynamically **/
            tr = new TableRow(this);
            /** Creating a Checkbox to add to the row **/
            CheckBox cb = new CheckBox(this);
            cb.setOnCheckedChangeListener(this);
            cb.setText(p.getEan());
            LinearLayout Ll1 = new LinearLayout(this);
            Ll1.addView(cb);
            tr.addView((View)Ll1); // Adding CheckBox to tablerow.  
            /** Creating a TextView to add to the row **/
            label = new TextView(this);
            label.setText(p.getName());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            label.setBackgroundColor(Color.GRAY);
            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.
             // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }

    public void onClickSenden (View view) {

    }

}
CheckBox cb = new CheckBox(this);
cb.setOnClickListener(this);

创建复选框时设置复选框id或标记。

cb.setTag(someIdentifier);

然后使用方法

@Override 
public void onClick(View v) {
                 if (v.getTag.equals(someIdentifier)){
                    //do stuff here
                  }    
                }