2012-11-16 82 views
3

我正在編寫一個Android應用程序,該應用程序顯示一個textview動態填充兩行JSON文件。查找JSON數組的列表項索引

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="7dp" 
    > 
<TextView 
    android:id="@+id/item_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:padding="2dp" 
    android:textSize="20dp" /> 
    <TextView 
    android:id="@+id/item_type" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:textSize="13dp" /> 
</LinearLayout> 

當任何列表項被點擊,我需要的是指數的JSON陣列中的其他內容,並傳遞到下一個活動是要顯示的陣列。

這裏的第一個活動的Java:

public class TutListActivity extends ListActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    String sJSON = ""; 

    if(sJSON == ""){ 

     InputStream is = this.getResources().openRawResource(R.raw.data); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String readLine = null; 
     StringBuilder sb = new StringBuilder(); 

     try { 

      while ((readLine = br.readLine()) != null) { 

      sb.append(readLine); 
     } 
     sJSON = sb.toString(); 

     is.close(); 
     br.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     }  
    } 

    String name = ""; 
    String type = ""; 
    String material = ""; 
    String welding = ""; 
    String primarybase = ""; 
    String tipmaterial = ""; 
    String shape = ""; 
    String shellcoating = ""; 

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

    try{ 
     JSONObject jObject = new JSONObject(sJSON); 
     JSONArray pluginfo = jObject.getJSONArray("parts"); 

     for(int i=0; i < pluginfo.length(); i++) { 
      JSONObject array = pluginfo.getJSONObject(i); 

      name = array.getString("name"); 
      type = array.getString("type"); 
      material = array.getString("material"); 
      welding = array.getString("welding"); 
      primarybase = array.getString("primarybase"); 
      tipmaterial = array.getString("tipmaterial"); 
      shape = array.getString("shape"); 
      shellcoating = array.getString("shellcoating"); 

      HashMap<String, String> list = new HashMap<String, String>(); 
      JSONObject e = pluginfo.getJSONObject(i); 

      list.put("id", String.valueOf(i)); 
      list.put("name", "Plug name: " + e.getString("name")); 
      list.put("type", "Type: " + e.getString("type")); 
      mylist.add(list); 

      } 
     } 

    catch(JSONException e){ 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_item, 
      new String[] { "name", "type" }, 
      new int[] { R.id.item_name, R.id.item_type }); 

    setListAdapter(adapter); 

    final Intent intent1 = new Intent(getApplicationContext(), TutViewerActivity.class); 

    intent1.putExtra("name", name); 
    intent1.putExtra("type", type); 
    intent1.putExtra("material", material); 
    intent1.putExtra("welding", welding); 
    intent1.putExtra("primarybase", primarybase); 
    intent1.putExtra("tipmaterial", tipmaterial); 
    intent1.putExtra("shape", shape); 
    intent1.putExtra("shellcoating", shellcoating); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      startActivity(intent1); 
     } 
    }); 
} 

}

,第2活動的Java:

public class TutViewerActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     Bundle extras = getIntent().getExtras(); 

     String name = extras.getString("name"); 
     String type = extras.getString("type"); 
     String material = extras.getString("material"); 
     String welding = extras.getString("welding"); 
     String primarybase = extras.getString("primarybase"); 
     String tipmaterial = extras.getString("tipmaterial"); 
     String shape = extras.getString("shape"); 
     String shellcoating = extras.getString("shellcoating"); 

     TextView itemName = (TextView)findViewById(R.id.item_name); 
     TextView itemType = (TextView)findViewById(R.id.item_type); 
     TextView itemMaterial = (TextView)findViewById(R.id.item_material); 
     TextView itemWelding = (TextView)findViewById(R.id.item_welding); 
     TextView itemPrimarybase = (TextView)findViewById(R.id.item_primarybase); 
     TextView itemTipmaterial = (TextView)findViewById(R.id.item_tipmaterial); 
     TextView itemShape = (TextView)findViewById(R.id.item_shape); 
     TextView itemShellcoating = (TextView)findViewById(R.id.item_shellcoating); 

     itemName.setText(name); 
     itemType.setText(type); 
     itemMaterial.setText(material); 
     itemWelding.setText(welding); 
     itemPrimarybase.setText(primarybase); 
     itemTipmaterial.setText(tipmaterial); 
     itemShape.setText(shape); 
     itemShellcoating.setText(shellcoating); 

    } 
} 

名稱和類型的字段都填充到第一頁。我需要獲取其餘的信息「材料」 - 「shellcoating」傳遞給我點擊的項目,並將其作爲數組傳遞給下一個活動。

的logcat的錯誤:

11-06 00:42:07.585: E/AndroidRuntime(3122): FATAL EXCEPTION: main 
11-06 00:42:07.585: E/AndroidRuntime(3122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mamlambo.tutorial.tutlist/com.mamlambo.tutorial.tutlist.TutViewerActivity}: java.lang.NullPointerException 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.os.Looper.loop(Looper.java:137) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at dalvik.system.NativeStart.main(Native Method) 
11-06 00:42:07.585: E/AndroidRuntime(3122): Caused by: java.lang.NullPointerException 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at com.mamlambo.tutorial.tutlist.TutViewerActivity.onCreate(TutViewerActivity.java:62) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.Activity.performCreate(Activity.java:5008) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
11-06 00:42:07.585: E/AndroidRuntime(3122):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
11-06 00:42:07.585: E/AndroidRuntime(3122):   ... 11 more 

如何做到這一點很容易有什麼想法?謝謝。

+0

你是如何獲得這個'JSONArray',當您檢索'JSONArray'你可以同時解析,然後通過'意圖'? – jnthnjns

+0

@Asok我將JSON文件轉換爲字符串。然後進入HashMap,使用listAdapter根據其ID來填充兩個不同的文本視圖。 –

回答

2

實例化變量的任何方法外,只要他們不會在你的當前活動更改值沒有你想要他們。這使您可以將方法中的變量值傳遞給方法。

String material; 

下面是分析JSONArray的簡化版本:

for(i=0; i < jArray.length(); i++) { 
    JSONObject jObject = jArray.getJSONObject(i); 
    material = jObject.getString("material"); 
} 

使用Intent,通過上面顯示到您的下一個活動的 「材料」 字符串:

Intent passJSONIntent = new Intent(CurrentActivity.this, NextActivity.class); 
passJSONIntent.putExtra("material", material); 
startActivity(passJSONIntent); 

獲取額外的從IntentNextActivityonCreate()

Bundle extras = getIntent().getExtras(); 
String material = extras.getString("material"); 
TextView itemType = (TextView)findViewById(R.id.item_type); 
itemType.setText(material); 

編輯:

好了,現在你就錯過是setContentView(R.layout.yourLayout);

public class TutViewerActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.yourLayout); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     Bundle extras = getIntent().getExtras(); 

     //etc... 
+0

因此,對於此代碼,我會爲數組的每個部分以及getString(「字段名稱」)執行'String(field name)'並將每個字符串傳遞給intent方法?或者有沒有辦法一次獲得所有信息? –

+0

是的,我在上面構建的方式會通過'Intent'傳遞每個字段的名稱。您可以將'JSONArray'轉換爲單個'String',然後將一個'String'傳遞給'NextActivity',然後將字符串轉換回'JSONArray'並解析。 http://stackoverflow.com/q/5983932/1134705 – jnthnjns

+0

我在打開第二個活動時出錯。它與'itemType.setText(material)有關;' –

-1

你可以把一個僞代碼解釋你想要什麼?

我不知道,如果你正在尋找:

myList.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 


       Intent intent = new Intent(MYCLASS.this, NEXTACTIVITYCLASS.class); 
       Bundle b = new Bundle(); 
       //TRY TO DECODE JSON 
       String myJsonString = decodeMYJSON(MYJSON); 

       b.putString("JSON",myJsonString); 
       intent.putExtras(b); 
       startActivity(intent); 

              }}); 
+0

我使用JSON更新了問題,以及格式是如何填充的。我希望這有助於更好地解釋 –