android通过json生成视图

 比如,有这么个需求,要求从服务器上返回的数据来动态生产视图,来看

activity_main.xml
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/autorunlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

 </RelativeLayout>

public class MainActivity extends Activity {

 private List<AutoRun> list;
 private AutoRun autoRun;
 private LinearLayout autorunlayout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  autorunlayout = (LinearLayout) findViewById(R.id.autorunlayout);
  list = new ArrayList<AutoRun>();
  autoRun = new AutoRun();
  dataProvieder();
  GetView();
 }

 /**
 *
 * @desc  用来模拟服务器得到的数据
 *
 */
 private List<AutoRun> dataProvieder() {
  String str = "["
    + "{\"category\":0,\"label\":\"姓名\",\"minValue\":\"0\",\"maxValue\":\"0\",\"defaultValue\":\"0\",\"listValue\":[]},"
    + "{\"category\":0,\"label\":\"年龄\",\"minValue\":\"4\",\"maxValue\":\"120\",\"defaultValue\":\"20\",\"listValue\":[]},"
    + "{\"category\":1,\"label\":\"城市\",\"minValue\":\"0\",\"maxValue\":\"0\",\"defaultValue\":\"2\",\"listValue\":[\"北京\",\"上海\",\"广东\",\"深圳\",\"江西\"]},"
    + "{\"category\":1,\"label\":\"职业\",\"minValue\":\"0\",\"maxValue\":\"0\",\"defaultValue\":\"0\",\"listValue\":[\"IT\",\"教育贸易\",\"贸易\",\"金融\",\"政府\"]}"
    + "]";
  Gson gson = new Gson(); //Google提供的对象和Json互转的jar包
  list = gson.fromJson(str, new TypeToken<List<AutoRun>>() {
  }.getType());
  return list;
 }

 private void GetView() {
  for (int i = 0; i < list.size(); i++) {
   if (list.get(i).category == 0) {
    autorunlayout.addView(GetEditLayout(list.get(i).label,
      list.get(i).defaultValue));
   } else if (list.get(i).category == 1) {
    autorunlayout.addView(GetSpinnerLayout(list.get(i).label,
      list.get(i).listValue, list.get(i).defaultValue));
   }
  }
 }

 private View GetEditLayout(String txt, String hint) {
  LayoutInflater inflater = getLayoutInflater().from(this);
  View view = inflater.inflate(R.layout.autorun_edittext, null);
  ((TextView) view.findViewById(R.id.label)).setText(txt);
  ((EditText) view.findViewById(R.id.edit)).setHint(hint);
  return view;
 }

 private View GetSpinnerLayout(String txt, List<String> value,
   String defaulValue) {
  LayoutInflater inflater = getLayoutInflater().from(this);
  View view = inflater.inflate(R.layout.autorun_spnnier, null);
  ((TextView) view.findViewById(R.id.label)).setText(txt);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_expandable_list_item_1, value);
  Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
  spinner.setAdapter(adapter);
  spinner.setSelection(Integer.parseInt(defaulValue));
  return view;
 }
}

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="50.0dp"
  android:gravity="center_vertical"
  android:orientation="horizontal" >

  <TextView
   android:id="@+id/label"
   android:layout_width="80.0dp"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:text="test" />

  <EditText
   android:id="@+id/edit"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginRight="10.0dp" />

 </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="50.0dp"
  android:gravity="center_vertical"
  android:orientation="horizontal" >

  <TextView
   android:id="@+id/label"
   android:layout_width="80.0dp"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:text="test" />

  <Spinner
   android:id="@+id/spinner"
   android:layout_width="match_parent"
   android:layout_height="50.0dp"
   android:layout_marginRight="10.0dp" />

 </LinearLayout>


运行效果:

android通过json生成视图 


 
 注:
 
 获得 LayoutInflater 实例的三种方式
 
 1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()
 2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 3. LayoutInflater inflater = LayoutInflater.from(context);  
 
  这三种方式最终本质是都是调用的Context.getSystemService()

  我的博客其它文章列表
  http://my.oschina.net/helu

你可能感兴趣的:(android动态生成视图)