doukang5966907 2015-02-06 07:16
浏览 98
已采纳

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

my code above creates a table with checkboxes and textviews inside. the count and the values comes from a MySQL database over php-json. It works all fine. Problem: In the onClickSenden function, I want to check step by step every created checkbox unchecked or checked and do some code depending on.

Its not possible to setId(i), coz "i" is an Iterator and not int.

can u help me?

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) {


    }


}
  • 写回答

1条回答 默认 最新

  • duanben4771 2015-02-06 07:33
    关注
    CheckBox cb = new CheckBox(this);
    cb.setOnClickListener(this);
    

    Set checkbox id or a tag while creating it.

    cb.setTag(someIdentifier);
    

    then use the method

    @Override 
    public void onClick(View v) {
                     if (v.getTag.equals(someIdentifier)){
                        //do stuff here
                      }    
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办