未在tablelayout中打印的列的值

时间:2014-07-07 05:27:08

标签: android button tablelayout

在我的应用程序中,我从服务器获得价值并以tablelayout的形式显示它。我不是在任何地方存储价值。每行的开头都有一个按钮,当用户按下该按钮时,值第一列和第一行应插入变量中。我想将名为“product_code”的字段的值存储到变量中。

我的问题是我正在尝试提取该字段的值,但没有任何内容被打印出来。

按钮的onclick事件在程序的最后,我用注释标记了它。

这是我的代码

    public class FancyStock extends Activity implements View.OnClickListener {

        String data = "";
        TableLayout tl;
        TableRow tr;
        TextView label;

        ArrayList<Users_fancystock> users = new ArrayList<Users_fancystock>();



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

            tl = (TableLayout) findViewById(R.id.main_table);

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

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

                }
            }).start();



        }

        public ArrayList<Users_fancystock> parseJSON(String result) {
            ArrayList<Users_fancystock> users = new ArrayList<Users_fancystock>();
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Users_fancystock user = new Users_fancystock();
                    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);

            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("Shapes");
            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.




            // 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_fancystock> users) {

            addHeader();

            for (Iterator i = users.iterator(); i.hasNext();) {
                for (int j = 0; j < users.size(); j++) {

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

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

    ///-----------------------button initialization here-------------------------------------------------------
                    Button btn = new Button(this);
                    btn.setTag(j);
                    System.out.println(btn.getTag());

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

                    tr.addView((View) Ll);


                    /** 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"));
                    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.

                    /** 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.



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

///==============onclick event here---------------------------
        public void onClick(View v)
        {
          int k=(Integer)v.getTag();
            System.out.println("int k" +k);
            Users_fancystock rowRecord= users.get(k);
            System.out.println(rowRecord);


        }
    }

这是印刷品。最后一行打印“rowrecord”的值。

07-07 11:00:58.452  10932-10982/com.diamond.traders D/dalvikvm﹕ GC_FOR_ALLOC freed 328K, 3% free 11906K/12264K, paused 16ms, total 16ms
07-07 11:00:58.492  10932-10982/com.diamond.traders I/System.out﹕ [{"id":"1","product_code":"qweq","shaps":"weqwe","pair":"qwe","carats":"qwe","color":"qwe","clarity":"qwe","service":"qwe","polish":"qwe","symetric":"eqwe","tables":"qweqqwew","measurments":"eqw","flourscne":"eqwwwfqw","description":"sfsafasfsa","certificated":"safas","ccode":"asf","cut":"asfas","total":"fa","file":"sfasf"},{"id":"2","product_code":"sdas","shaps":"das","pair":"das","carats":"fasfa","color":"sfasf","clarity":"asfas","service":"fas","polish":"sdg","symetric":"sdg","tables":"sdgsd","measurments":"gsdgsdgsd","flourscne":"sdgsdg","description":"sdgsdg","certificated":"sdgs","ccode":"dgsd","cut":"dsdgsdg","total":"sdgsdg","file":"sdgsdgtrurt"}]
07-07 11:00:58.512  10932-10932/com.diamond.traders I/System.out﹕ 0
07-07 11:00:58.522  10932-10932/com.diamond.traders I/System.out﹕ 1
07-07 11:00:59.273  10932-10932/com.diamond.traders I/System.out﹕ int k0
07-07 11:00:59.273  10932-10932/com.diamond.traders I/System.out﹕ com.diamond.traders.Users_fancystock@421c7998

1 个答案:

答案 0 :(得分:1)

您正在尝试直接打印类的对象。所以它只输出:com.diamond.traders.Users_fancystock@421c7998

像这样使用:

public class Users_fancystock{

    String id;
    String productCode;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getProductCode() {
        return productCode;
    }
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

}

要打印:

Users_fancystock rowRecord= users.get(k);
System.out.println(rowRecord.getProductCode());

这应该有用。