按下按钮获取android中表的字段值

时间:2014-07-04 08:02:36

标签: android json button tablelayout

我正在创建一个应用程序,其中有一个表,其数据是使用JSON从服务器获取的,并在屏幕上填充。此外,我在表的开头添加了一个按钮,当用户按下按钮时,第一行和第一列的值应存储在变量中。

我没有在任何数据库中存储值,只是从服务器获取值然后在屏幕上显示它。

在屏幕截图中,您可以看到按钮和表格。一旦用户按下第一个按钮("添加到购物篮"),它应该将该行的product_code的值(仅限product_code)存储到字符串变量中。

请告诉我一些可以做到这一点的方法。我无法得到任何答案。

抱歉我的英文不好

here is the screenshot

这是我的代码

public class FancyStock extends Activity implements View.OnClickListener {

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

    @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() {
                        ArrayList<Users_fancystock> 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();) {

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

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




            Button btn = new Button(this);
            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.



            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        }
    }
    public void onClick(View v)
    {
        //  startActivity(f1);

    }


}

1 个答案:

答案 0 :(得分:0)

在你的addData()函数中你必须用那个按钮设置标签值,这个标签值你必须设置i整数,并在onclick中首先获得标签并解析为整数然后从arraylist ArrayList获取该索引值你已经从JSON填充了记录。你将从该索引获得fancystock的对象,该对象包含所选行的记录。

告诉我你是否理解这一点并解决了这个问题:)

//更新的 ***************************************** ************ //

    ArrayList<Integer> users=new ArrayList<Integer>();
   for(int i=0;i<users.size();i++){
       Button b=new Button(this);
       b.setTag(i);

   }
onClickListner

中的

    int i=(Integer)b.getTag();
    Users_fancystock rowRecord= users,get(i);

现在这个rowrecord将包含所需行的所有值