如何在动态生成的表中获取第一列的值,其复选框被选中?

时间:2014-05-29 06:19:49

标签: android checkbox

我正在创建一个以表格形式显示来自服务器的数据的应用程序。我也在表的开头生成复选框。

我的问题是,如何从选中复选框的表中获取特定列(尤其是第一列)的数据。

在截图中,我只需要product_code的值,是否有可能获得它的价值? 此外,一旦我获得product_code的值,我将把它发送回服务器并将其存储在另一个数据库中。

我的代码现在不合适,因为我正在使用线程进行网络连接,但我很快就会将其更改为asynctask。

抱歉我的英文不好

public class Availability extends Activity  {

    String data = "";
    TableLayout tl;
    TextView tv;
    TableRow tr;
    TextView label;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
   HttpClient httpclient;
   List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;
    Users_availability q = new  Users_availability();
    Button book;
    CheckBox chk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_availability);

        tl = (TableLayout) findViewById(R.id.main_table);
        tv = (TextView)findViewById(R.id.tv);
        book=(Button)findViewById(R.id.book);

        final GetDatafromDB_Availability getdb = new GetDatafromDB_Availability();
        new Thread(new Runnable() {
            public void run() {
                data = getdb.getDataFromDB();
                System.out.println(data);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Users_availability> users = parseJSON(data);
                        addData(users);
                    }
                });

            }
        }).start();



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

                   Addtocart();


            }
        });
    }




    public ArrayList<Users_availability> parseJSON(String result) {
        ArrayList<Users_availability> users = new ArrayList<Users_availability>();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Users_availability user = new Users_availability();
                user.setId(json_data.getInt("id"));
                user.setProduct_code(json_data.getString("product_code"));
                user.setShapes(json_data.getString("shaps"));
                user.setPair(json_data.getString("pair"));
                user.setCarats(json_data.getString("carats"));
                user.setColor(json_data.getString("color"));
                user.setClarity(json_data.getString("clarity"));
                user.setService(json_data.getString("service"));
                user.setPolish(json_data.getString("polish"));
                user.setSymetric(json_data.getString("symetric"));
                user.setTables(json_data.getString("tables"));
                user.setMeasurements(json_data.getString("measurments"));
                user.setFlourscne(json_data.getString("flourscne"));
                user.setDescription(json_data.getString("description"));
                user.setCerticated(json_data.getString("certificated"));
                user.setCcode(json_data.getString("ccode"));
                user.setCut(json_data.getString("cut"));
                user.setTotal(json_data.getString("total"));
                user.setFile(json_data.getString("file"));
                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 **/
        TextView add = new TextView(this);
        add.setText("Add");
        add.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        add.setPadding(5, 5, 5, 5);
        add.setBackgroundColor(Color.parseColor("#BDB76B"));
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(add,params);
        tr.addView((View)Ll);

        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText("Product code");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        label.setBackgroundColor(Color.parseColor("#BDB76B"));
         Ll = new LinearLayout(this);
       params = new LinearLayout.LayoutParams(LayoutParams.MATCH_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 shapes = new TextView(this);
        shapes.setText("Shapes");
        shapes.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        shapes.setPadding(5, 5, 5, 5);
        shapes.setBackgroundColor(Color.parseColor("#BDB76B"));
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(shapes,params);
        tr.addView((View)Ll); // Adding textview to tablerow.

        TextView pair = new TextView(this);
        pair.setText("pair");
        pair.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        pair.setPadding(5, 5, 5, 5);
        pair.setBackgroundColor(Color.parseColor("#BDB76B"));
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(pair,params);
        tr.addView((View)Ll); // Adding textview to tablerow.



        TextView carats = new TextView(this);
        carats.setText("Carats");
        carats.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        carats.setPadding(5, 5, 5, 5);
        carats.setBackgroundColor(Color.parseColor("#BDB76B"));
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(carats,params);
        tr.addView((View)Ll); // Adding textview to tablerow.


        TextView color = new TextView(this);
        color.setText("Color");
        color.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        color.setPadding(5, 5, 5, 5);
        color.setBackgroundColor(Color.parseColor("#BDB76B"));
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(color,params);
        tr.addView((View)Ll); // Adding textview to tablerow.




        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Users_availability> users) {

        addHeader();

        for (Iterator i = users.iterator(); i.hasNext();) {

            Users_availability p = (Users_availability) i.next();

            /** Create a TableRow dynamically **/
            tr = new TableRow(this);


            chk = new CheckBox(this);

            // btn.setTextSize();
            //btn.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
            // btn.setTag(mLinks.get(index));
           // btn.setOnClickListener(this);
            //Ll = new LinearLayout(this);
           // params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
           //         LayoutParams.WRAP_CONTENT);
          //  params.setMargins(5, 2, 2, 2);
          //  params.width=400;
          //  params.height=60;
            // btn.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
           // Ll.addView(chk,params);

            tr.addView((View)chk);




            /** Creating a TextView to add to the row **/
            label = new TextView(this);
            label.setText(p.getproduct_code());
            label.setId(p.getId());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            ////label.setBackgroundColor(Color.parseColor("#BDB76B"));
            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //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(p.getShapes());
            place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            place.setPadding(5, 5, 5, 5);
          //  place.setBackgroundColor(Color.parseColor("#BDB76B"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(place,params);
            tr.addView((View)Ll); // Adding textview to tablerow.

            label = new TextView(this);
            label.setText(p.getpair());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            ////label.setBackgroundColor(Color.parseColor("#BDB76B"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.


            label = new TextView(this);
            label.setText(p.getCarats());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            ////label.setBackgroundColor(Color.parseColor("#BDB76B"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.

            label = new TextView(this);
            label.setText(p.getColor());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            ////label.setBackgroundColor(Color.parseColor("#BDB76B"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.

            label = new TextView(this);
            label.setText(p.getClarity());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            ////label.setBackgroundColor(Color.parseColor("#BDB76B"));
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            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.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        }
    }



    void Addtocart(){
        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://192.168.0.106/test/cart.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
//------------------------------------------------------------------------------------checkbox here..----------------------------------------------         
              if(chk.isChecked())
              {

            nameValuePairs.add(new BasicNameValuePair("product_code",q.getproduct_code().toString().trim()));


            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            System.out.println("Response : " + response);

            /*
            runOnUiThread(new Runnable() {
                public void run() {
                   // tv.setText("Response from PHP : " + response);
                   // dialog.dismiss();
                }
            });
*/  
            if(response.equalsIgnoreCase("cart")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(Availability.this, "Add to cart successful", Toast.LENGTH_SHORT).show();
                    }
                });

               // startActivity(new Intent(Availability.this, LoginScreen.class));
            }else{
               showAlert();
            }

        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
        }
    }

    public void showAlert(){
        Availability.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(Availability.this);
                builder.setTitle("Can't add to cart.");
                builder.setMessage("Problem in adding to cart. Please try again later")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }

}

Screenshot of the output

1 个答案:

答案 0 :(得分:0)

我建议不要去检查在桌子上检查哪个位置。而不是当你点击特定复选框时将该位置保存到数组并在未选中时从数组中删除。 当您想从检查复选框中获取数据时,只需从存储在临时数组中的所有位置获取数据。多数民众赞成......我认为这是更好,更方便的方式。