English 中文(简体)
Android 动态控制发电机
原标题:Android dynamic control generator
  • 时间:2011-11-21 06:54:11
  •  标签:
  • android

我正在寻找一个恒星控制发电机。 我有一个网络服务,它将提供信息,以创造某种形式的东西。 这一网络服务将告诉我我我我我我,我对这一形式需要的一切控制(可以是TextViewRadioButtonSpinner等)以及左下/右边阵地w.r.t 其他控制。

为此,我需要写一个通用的班子,将我从网络服务中接手的那项控制目标。 通用班级将履行以下职能:RelativeLayout.LayoutParams,即:使TotLeft(基因控制)。 目的(t)是,先是控制的对象,其次是参照控制。 ToLeft/Rights。 接着,在我的<>上加上这一控制,使用addView<>(第24段)。

我的问题是,如何创造新的控制环境,把它交给一个职能,增加准规则并增加看法?

问题回答
public class DynamicDesignMainActivity extends Activity {


LinearLayout main_layout;
jsonparser json_parser=null;
String jsonurl="URL STRING";
ProgressDialog progress_dialog;
JSONObject json_object_main;

private static final String TAG_NAME_MAIN="name";
private static final String TAG_JOBTYPE ="jobtype";
private static final String TAG_SECTION="sections";
private static final String TAG_SEC_1="sec-1";
private static final String TAG_NAME_MIDDEL="name";
private static final String TAG_OPTIONS="options";
private static final String TAG_NAME_INNER="name";
private static final String TAG_ID="id";
private static final String TAG_VALUES="values";
private static final String TAG_YES="yes";
private static final String TAG_NO="no";
private static final String TAG_TYPE="type";
private static final String TAG_ADDITIONAL_NOTE="additional_note";


class AsynctaskOperation extends AsyncTask<String, String, String>
{
    Context context;
    String json_string;
    public AsynctaskOperation(Context con) {
        // TODO Auto-generated constructor stub
        context=con;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progress_dialog.show();
        json_parser=new jsonparser();
    }


    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        json_string=json_parser.getJsonfromUrl(jsonurl);
        return null;
    }


    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        progress_dialog.dismiss();
        Toast.makeText(DynamicDesignMainActivity.this, json_string,     Toast.LENGTH_LONG).show();
        try {

            json_object_main=new JSONObject(json_string);

            String name_main=json_object_main.getString(TAG_NAME_MAIN);
            String job_typew=json_object_main.getString(TAG_JOBTYPE);

            JSONObject  json_section=json_object_main.getJSONObject(TAG_SECTION);
            JSONObject  json_sec=json_section.getJSONObject(TAG_SEC_1);

            String mid_name=json_sec.getString(TAG_NAME_MIDDEL);


            main_layout=(LinearLayout)findViewById(R.id.main_LinearLayout);

            JSONArray json_option=json_sec.getJSONArray(TAG_OPTIONS);
            for(int i=0;i<json_option.length()-1;i++)
            {

                LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout linearlauout1=new LinearLayout(context);
                linearlauout1.setOrientation(LinearLayout.VERTICAL);
                linearlauout1.setLayoutParams(params);

                LinearLayout linearlayout_question=new LinearLayout(context);
                linearlayout_question.setLayoutParams(params);

                TextView textview_question=new TextView(context);
                textview_question.setLayoutParams(params);


                LinearLayout linearlayout_option=new LinearLayout(context);
                linearlayout_option.setOrientation(LinearLayout.VERTICAL);
                linearlayout_option.setLayoutParams(params);

                JSONObject jsonobject=json_option.getJSONObject(i);
                String question_name=jsonobject.getString(TAG_NAME_INNER);
                textview_question.setText(question_name);

                String type=null;
                type=jsonobject.getString(TAG_TYPE);
                Log.i("TYPE",type);
                JSONObject json_values=jsonobject.getJSONObject(TAG_VALUES);

                String val1=json_values.getString(TAG_YES);
                String val2=json_values.getString(TAG_NO);
                RadioGroup rg=null;
                String values[]={val1,val2};
                if(type.equalsIgnoreCase("radio"))
                {
                    RadioButton[] rb=new RadioButton[2];
                    rg=new RadioGroup(context);
                    rg.setOrientation(RadioGroup.HORIZONTAL);
                    for(int j=0;j<2;j++)
                    {
                        rb[j]=new RadioButton(context);
                        rb[j].setText(values[j]);
                        rg.addView(rb[j]);
                    }

                }

                main_layout.addView(linearlauout1);
                linearlauout1.addView(linearlayout_question);
                linearlauout1.addView(linearlayout_option);
                linearlayout_question.addView(textview_question);
                linearlayout_option.addView(rg);



            }

            Button btn_show=new Button(context);
            btn_show.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            btn_show.setText("Show");
            btn_show.setId(101);
            btn_show.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    switch (v.getId()) {
                    case 101:

                        showAnswer(main_layout);
                        break;

                    default:
                        break;
                    }

                }
            });

            main_layout.addView(btn_show);




        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dynamic_design_main);

    progress_dialog=new ProgressDialog(this);
    progress_dialog.setMessage("Loading...");

    new AsynctaskOperation(this).execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dynamic_design_main, menu);
    return true;
}

public void showAnswer(ViewGroup parent)
{
    //Log.i("Count",parent.getChildCount()+"");
    for(int j=0;j<parent.getChildCount();j++)
    {
        View child= parent.getChildAt(j);
        //Log.i("CALSS",child.getClass().toString());
        if(child instanceof TextView)
        {

            Log.i("TEXT",((TextView)child).getText()+"");
        }
        else
        {
            ViewGroup group=(ViewGroup)child;
            showAnswer(group);
        }
    }
}

}

公共课堂 页: 1

int code;
static InputStream is=null;
String json=null;
    public jsonparser() {
    // TODO Auto-generated constructor stub
    }

    public String getJsonfromUrl(String url)
    {
    StringBuilder builder=new StringBuilder();

    DefaultHttpClient httpClient=new DefaultHttpClient();

    HttpPost httppost=new HttpPost(url);

    try {

        HttpResponse httpResponse=httpClient.execute(httppost);

        code=httpResponse.getStatusLine().getStatusCode();

        if(code==200)
        {
            HttpEntity httpentity=httpResponse.getEntity();
            is=httpentity.getContent();

            BufferedReader br=new BufferedReader(new      InputStreamReader(is, "iso-8859-1"), 8);

            String line=null;
            while((line=br.readLine())!=null)
            {
                builder.append(line+"
");
            }
            is.close();
        }
        json=builder.toString();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return json;

        }

    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...