android项目文件简介 及JSON文件解析

来源:互联网 发布:电脑网络查询 编辑:程序博客网 时间:2024/06/05 15:21


android项目文件简介 及JSON文件解析


1  src
存放源文件的地方
2.  gen
自动生成的文件
3  android 2.1 的库文件
4assets 存放外部文件的地方如json文件
4   res
里面有layout ,布局,控件可以画上去的那种
values定义标题名字的地方可以
AndroidManifest.xml 这个文件也很重要,在里面设置新的active的地方。
 
[java] view plaincopyprint?
  1. package com.android.ling;  
  2.   
  3.   
  4.   
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.io.UnsupportedEncodingException;  
  10. import java.util.Iterator;  
  11. import java.util.LinkedList;  
  12. import java.util.Map;  
  13.   
  14. import org.json.JSONArray;  
  15. import org.json.JSONException;  
  16. import org.json.JSONObject;  
  17.   
  18.   
  19.   
  20. import android.app.Activity;  
  21. import android.content.Intent;  
  22. import android.os.Bundle;  
  23. import android.util.Log;  
  24. import android.view.View;  
  25. import android.view.View.OnClickListener;  
  26. import android.widget.AdapterView;  
  27. import android.widget.AdapterView.OnItemClickListener;  
  28. import android.widget.ArrayAdapter;  
  29. import android.widget.Button;  
  30. import android.widget.ListAdapter;  
  31. import android.widget.ListView;  
  32. import android.widget.TextView;  
  33.   
  34.   
  35.   
  36. public class json extends Activity   
  37. {  
  38.     private JSONObject dataJson=null;  
  39.     private JSONObject nbaJson=null;  
  40.     private String TAG="com.android.ling";   
  41.     private ListView lv=null;  
  42.     private String str=new String();  
  43.     private String temp;  
  44.     private String [] result=null;  
  45.     private InputStream fil = null;  
  46.     private BufferedReader br=null;  
  47.     @Override  
  48. public void onCreate(Bundle savedInstanceState)   
  49. {  
  50.    /* try { 
  51.         Thread.sleep(15000,0); 
  52.     } catch (InterruptedException e1) { 
  53.         // TODO Auto-generated catch block 
  54.         e1.printStackTrace(); 
  55.     }*/  
  56.     super.onCreate(savedInstanceState);  
  57.     setContentView(R.layout.index);  
  58.     
  59.           
  60.         try{  
  61.             fil = getAssets().open("list.json");  
  62.         }catch(IOException e){  
  63.             e.printStackTrace();  
  64.             Log.d(TAG,"=========="+fil);    
  65.         }  
  66.           
  67.           
  68.           
  69.         try{  
  70.              br=new BufferedReader(new InputStreamReader(fil,"UTF-8"));  
  71.         }catch (UnsupportedEncodingException e)  {  
  72.   
  73.              e.printStackTrace();  
  74.              Log.d(TAG,"=========="+br);    
  75.         }  
  76.           
  77.           
  78.         try{  
  79.             while(br!=null&&null!=(temp=br.readLine()))  
  80.                 {  
  81.                 str+=temp;   
  82.                 if(null!=str)  
  83.                 Log.d(TAG,"str is =========="+str);   
  84.                 }  
  85.           
  86.         }catch(IOException e){  
  87.             e.printStackTrace();  
  88.             Log.d(TAG,"=========="+str);    
  89.         }  
  90.           
  91.           
  92.         try{  
  93.               
  94.              dataJson = new JSONObject(str);      
  95.                   
  96.              if(null!=dataJson)   
  97.                   
  98.                  Log.d(TAG,"datajson is=========="+dataJson);             
  99.                
  100.                        
  101.         }catch (JSONException e){  
  102.         
  103.             e.printStackTrace();  
  104.              Log.d(TAG," datajson is =========="+dataJson);   
  105.             }             
  106.         
  107.           
  108.           JSONArray js= dataJson.names();  
  109.           result=new String[js.length()];  
  110.           for(int i =0;i<js.length();i++)  
  111.           {  
  112.               
  113.                 result[i]=js.optString(i);<span style="color:#ff0000;">//解析JSON文件,</span>  
  114.           }  
  115.           
  116.           
  117.           lv=(ListView)findViewById(R.id.listView);  
  118.             ListAdapter adapter=new ArrayAdapter<String>(  
  119.                     this,android.R.layout.simple_list_item_1 ,  
  120.                     result);  
  121.             lv.setAdapter(adapter);  
  122.               
  123.             lv.setOnItemClickListener(new OnItemClickListener()  
  124.             {      
  125.               
  126.                 @Override  
  127.                 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  128.                         long arg3) {  
  129.                                           
  130.                       <span style="color:#ff0000;">String map = arg0.getItemAtPosition(arg2).toString();  
  131.                        
  132.                      Intent i = new Intent();  
  133.                          
  134.                      i.setClass(json.this, json1.class);  
  135.                        
  136.                      Bundle bundle=new Bundle();  
  137.                      bundle.putString("KEY_TEXT", map);  
  138.                      bundle.putString("KEY_Object", str); //向另一个active传值   
  139.                        
  140.                      i.putExtras(bundle);  
  141.                        
  142.                      startActivity(i);</span>  
  143.                 }  
  144.                   
  145.             }  
  146.               );  
  147.              
  148.           
  149.    }  
  150. }  

 
[java] view plaincopyprint?
  1. package com.android.ling;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.UnsupportedEncodingException;  
  8. import java.util.Iterator;  
  9. import java.util.LinkedList;  
  10.   
  11. import org.json.JSONArray;  
  12. import org.json.JSONException;  
  13. import org.json.JSONObject;  
  14. import android.app.Activity;  
  15. import android.content.Intent;  
  16. import android.os.Bundle;  
  17. import android.util.Log;  
  18. import android.view.View;  
  19. import android.widget.AdapterView;  
  20. import android.widget.ArrayAdapter;  
  21. import android.widget.ListAdapter;  
  22. import android.widget.ListView;  
  23. import android.widget.TextView;  
  24. import android.widget.AdapterView.OnItemClickListener;  
  25.   
  26.   
  27. public class json1 extends Activity   
  28. {  
  29.     private ListView lv=null;  
  30.     private String TAG="com.android.ling";   
  31.     private JSONObject dataJson=null;  
  32.       
  33.     private JSONObject nbaJson=null;  
  34.         @Override  
  35.       
  36.     public void onCreate(Bundle savedInstanceState)   
  37.     {  
  38.        
  39.        super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.main);  
  41.           
  42.         try{  
  43.               
  44.         <span style="color:#ff0000;">lv=(ListView)findViewById(R.id.listView1);  
  45.         Bundle bundle = this.getIntent().getExtras();   
  46.         String text = bundle.getString("KEY_TEXT");   
  47.         String str = bundle.getString("KEY_Object");</span>           
  48.         dataJson = new JSONObject(str);  
  49.         if(null!=dataJson)   
  50.               
  51.             Log.d(TAG,"datajson is =========="+dataJson);             
  52.                       
  53.         nbaJson = dataJson.getJSONObject(text);  
  54.           
  55.         
  56.         JSONArray js=nbaJson.names();  
  57.         String[] result=new String[js.length()];  
  58.         
  59.         for(int i=0;i<js.length();i++)  
  60.         {  
  61.             result[i]=js.optString(i);  
  62.             String value=nbaJson.optString(result[i]);  
  63.             result[i]=result[i]+"  is  "+ value;  
  64.         }  
  65.           ListAdapter adapter=new ArrayAdapter<String>(  
  66.                 this,android.R.layout.simple_list_item_1 ,  
  67.                 result);  
  68.         lv.setAdapter(adapter);  
  69.   
  70.          }catch (JSONException e){  
  71.         
  72.              e.printStackTrace();  
  73.              Log.d(TAG," nba is=========="+nbaJson);   
  74.         }     

使用了listview的控件
使用了intent 页面转换
 
不知道做游戏的是怎么做的,现在只是知道了这么点皮毛而已。

0 0
原创粉丝点击