Android 如何从JSON在表中添加行

Android 如何从JSON在表中添加行,android,android-tablelayout,Android,Android Tablelayout,由于我还在学习Android,我想我的问题对你们来说会很简单。 我试图显示一个表,其中包含来自mysql的信息令牌。 例如,我想显示客户机命令。如果他订购一种产品,我将有一行,因此我的行数与他订购的产品数相同。我真的不知道怎么做,也没有在网上找到解决办法。(表中的所有框都可以编辑) CommandDetails.java public class CommandDetails extends Activity { Button logout; Button editCommand

由于我还在学习Android,我想我的问题对你们来说会很简单。 我试图显示一个表,其中包含来自mysql的信息令牌。 例如,我想显示客户机命令。如果他订购一种产品,我将有一行,因此我的行数与他订购的产品数相同。我真的不知道怎么做,也没有在网上找到解决办法。(表中的所有框都可以编辑)


CommandDetails.java

public class CommandDetails extends Activity {



Button logout;  
Button editCommand;
Button bachToCommand;
Button exit;
ToggleButton toggleButton;

private Spinner spinner_contatct_Model ;
EditText contatct_Model;

//service web
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> contactlist;  

// url to get all accounts list
private static String url_command_details= "WebService/Command_Details.php";

// JSON Node names 
private static final String TAG_SUCCESS = "success";
private static final String TAG_COMMAND = "command";

private static final String TAG_ID = "ID";
private static final String TAG_COMMANDID = "COMMANDID";
private static final String TAG_QUANTITYCOMMANDED = "QUANTITYCOMMANDED";
private static final String TAG_COMMANDPRICE = "COMMANDPRICE";
private static final String TAG_PRODUCTID = "PRODUCTID";


// url to get all Contacts list
        private static String url_update_contact = "WebService/ContactUpdate.php";

// products JSONArray
JSONArray commands = null;
TextView Id;
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);

    setContentView(R.layout.command_details);

    Bundle data = getIntent().getExtras() ;
    final String username =data.getString("username");
    final String id =data.getString("id");

    logout=(Button)findViewById(R.id.ButtonLogoff);
    logout.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {  
            finish();
        }});

    bachToCommand=(Button)findViewById(R.id.bachToCommand);
    bachToCommand.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Command.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    exit=(Button)findViewById(R.id.exitCommandDetail);
    exit.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Home.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    //addListenerOnSpinnerItemSelection();

toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);

    toggleButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (toggleButton.isChecked()) {

            TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(true);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(true);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(true);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.VISIBLE);
                editCommand.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v) {  

                            //new UpdateCommand().execute();

                        }});


            } else {
                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(false);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(false);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(false);

            }
        }
    });



    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", id));  

    // getting JSON string from URL

    JSONObject json = jParser.makeHttpRequest(getString(R.string.server_address)+url_command_details, "GET", params);

    // Check your log cat for JSON reponse
    Log.d("All Commands: ", json.toString());

    try {
        // Checking for SUCCESS TAG

            commands = json.getJSONArray(TAG_COMMAND);

            // looping through All Contacts
            for (int i = 0; i < commands.length(); i++) {
                JSONObject c = commands.getJSONObject(i);

                // Storing each json item in variable
                String commandid = c.getString(TAG_ID);
                String quantitycommand = c.getString(TAG_QUANTITYCOMMANDED);
                String commandprice = c.getString(TAG_COMMANDPRICE);
                String productid = c.getString(TAG_PRODUCTID);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setText(quantitycommand);
                commandProductQuantity.setEnabled(false);   


                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setText(commandprice);
                commandPrice.setEnabled(false); 

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setText(productid);
                commandProductname.setEnabled(false);   


            }

    }
    catch(Exception e){}

}

          }
公共类CommandDetails扩展活动{
按钮注销;
按钮编辑命令;
按钮bachToCommand;
按钮出口;
切换按钮切换按钮;
私有微调器微调器接触模型;
编辑文本连接模型;
//服务网站
私人对话;
//创建JSON解析器对象
JSONParser JSONParser=新的JSONParser();
JSONParser jParser=新的JSONParser();
ArrayList联系人列表;
//获取所有帐户列表的url
私有静态字符串url\u command\u details=“WebService/command\u details.php”;
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_COMMAND=“COMMAND”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_COMMANDID=“COMMANDID”;
私有静态最终字符串标记_QUANTITYCOMMANDED=“QUANTITYCOMMANDED”;
私有静态最终字符串标记_COMMANDPRICE=“COMMANDPRICE”;
私有静态最终字符串标记_PRODUCTID=“PRODUCTID”;
//获取所有联系人列表的url
私有静态字符串url\u update\u contact=“WebService/ContactUpdate.php”;
//产品JSONArray
JSONArray命令=null;
文本视图Id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.command\u详细信息);
Bundle data=getIntent().getExtras();
最终字符串用户名=data.getString(“用户名”);
最终字符串id=data.getString(“id”);
注销=(按钮)findViewById(R.id.ButtonLogoff);
logout.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
完成();
}});
bachToCommand=(按钮)findViewById(R.id.bachToCommand);
bachToCommand.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
Intent Intent=新的Intent(v.getContext(),Command.class);
intent.putExtra(“用户名”,用户名);
完成();
星触觉(意向);
}});
退出=(按钮)findViewById(R.id.exitCommand详细信息);
exit.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
Intent Intent=newintent(v.getContext(),Home.class);
intent.putExtra(“用户名”,用户名);
完成();
星触觉(意向);
}});
//addListenerOnSpinnerItemSelection();
toggleButton=(toggleButton)findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(toggleButton.isChecked()){
TextView commandProductname=(TextView)findViewById(R.id.commandProductname);
commandProductname.setEnabled(true);
TextView commandProductQuantity=(TextView)findViewById(R.id.commandProductQuantity);
commandProductQuantity.setEnabled(true);
TextView commandPrice=(TextView)findViewById(R.id.commandPrice);
commandPrice.setEnabled(true);
editCommand=(按钮)findViewById(R.id.editCommand);
editCommand.setVisibility(View.VISIBLE);
editCommand.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
//新建UpdateCommand().execute();
}});
}否则{
editCommand=(按钮)findViewById(R.id.editCommand);
editCommand.setVisibility(View.INVISIBLE);
TextView commandProductname=(TextView)findViewById(R.id.commandProductname);
commandProductname.setEnabled(false);
TextView commandProductQuantity=(TextView)findViewById(R.id.commandProductQuantity);
commandProductQuantity.setEnabled(false);
TextView commandPrice=(TextView)findViewById(R.id.commandPrice);
commandPrice.setEnabled(false);
}
}
});
List params=new ArrayList();
参数add(新的BasicNameValuePair(“id”,id));
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(getString(R.string.server_address)+url_命令_详细信息,“GET”,参数);
//检查日志cat中的JSON响应
Log.d(“所有命令:,json.toString());
试一试{
//检查成功标签
commands=json.getJSONArray(TAG_命令);
//通过所有触点循环
对于(int i=0;ipublic class CommandDetails extends Activity {



Button logout;  
Button editCommand;
Button bachToCommand;
Button exit;
ToggleButton toggleButton;

private Spinner spinner_contatct_Model ;
EditText contatct_Model;

//service web
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> contactlist;  

// url to get all accounts list
private static String url_command_details= "WebService/Command_Details.php";

// JSON Node names 
private static final String TAG_SUCCESS = "success";
private static final String TAG_COMMAND = "command";

private static final String TAG_ID = "ID";
private static final String TAG_COMMANDID = "COMMANDID";
private static final String TAG_QUANTITYCOMMANDED = "QUANTITYCOMMANDED";
private static final String TAG_COMMANDPRICE = "COMMANDPRICE";
private static final String TAG_PRODUCTID = "PRODUCTID";


// url to get all Contacts list
        private static String url_update_contact = "WebService/ContactUpdate.php";

// products JSONArray
JSONArray commands = null;
TextView Id;
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);

    setContentView(R.layout.command_details);

    Bundle data = getIntent().getExtras() ;
    final String username =data.getString("username");
    final String id =data.getString("id");

    logout=(Button)findViewById(R.id.ButtonLogoff);
    logout.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {  
            finish();
        }});

    bachToCommand=(Button)findViewById(R.id.bachToCommand);
    bachToCommand.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Command.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    exit=(Button)findViewById(R.id.exitCommandDetail);
    exit.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Home.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    //addListenerOnSpinnerItemSelection();

toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);

    toggleButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (toggleButton.isChecked()) {

            TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(true);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(true);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(true);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.VISIBLE);
                editCommand.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v) {  

                            //new UpdateCommand().execute();

                        }});


            } else {
                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(false);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(false);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(false);

            }
        }
    });



    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", id));  

    // getting JSON string from URL

    JSONObject json = jParser.makeHttpRequest(getString(R.string.server_address)+url_command_details, "GET", params);

    // Check your log cat for JSON reponse
    Log.d("All Commands: ", json.toString());

    try {
        // Checking for SUCCESS TAG

            commands = json.getJSONArray(TAG_COMMAND);

            // looping through All Contacts
            for (int i = 0; i < commands.length(); i++) {
                JSONObject c = commands.getJSONObject(i);

                // Storing each json item in variable
                String commandid = c.getString(TAG_ID);
                String quantitycommand = c.getString(TAG_QUANTITYCOMMANDED);
                String commandprice = c.getString(TAG_COMMANDPRICE);
                String productid = c.getString(TAG_PRODUCTID);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setText(quantitycommand);
                commandProductQuantity.setEnabled(false);   


                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setText(commandprice);
                commandPrice.setEnabled(false); 

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setText(productid);
                commandProductname.setEnabled(false);   


            }

    }
    catch(Exception e){}

}

          }