【问题标题】:get value of field of table in android on press of button按下按钮在android中获取表字段的值
【发布时间】:2014-08-25 12:11:55
【问题描述】:

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

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

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

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

对不起我的英语不好

这是我的代码

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

    }


}

【问题讨论】:

    标签: android json button tablelayout


    【解决方案1】:

    在您的 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);
    

    现在此行记录将包含您想要的行的所有值

    【讨论】:

    • 对不起,我不明白你的答案。你能给我一些例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    相关资源
    最近更新 更多