2013-07-10 52 views
0

我正在爲一個網站做一個應用程序,我需要一些幫助作出陳述。我從API(服務器)解析JSON並捕獲它,這是工作,但我希望它顯示在一個ListView中,我做了我的適配器和所有,這是工作。現在,當我啓動應用程序時,只有一行顯示在列表視圖中。所以我不知道如何讓所有的值進入列表視圖。對於每個鍵,顯示值 - Android

我的活動:

public class FilesActivity extends SherlockActivity { 


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

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle("Files"); 

     String response = null; 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     ResponseHandler <String> resonseHandler = new BasicResponseHandler(); 
     HttpPost postMethod = new HttpPost("http://api.bayfiles.net/v1/account/files?session=<SessionId>"); 

     try { 
      JSONObject json = new JSONObject(); 

       postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); 
       postMethod.setHeader("Content-Type", "application/json"); 
       response = httpClient.execute(postMethod,resonseHandler); 
       JSONObject request = new JSONObject(response); 
       for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext();) { 
        String key = (String) keyIterator.next(); 
        JSONObject object = request.optJSONObject(key); 

        ArrayList<fileObject> objectList = new ArrayList<fileObject>(); 
        //ArrayList<fileObject> results = new ArrayList<fileObject>(); 
        if (object != null) { 
         fileObject obj = new fileObject(); 


         obj.setFileId(key); 
         obj.setFileName(object.getString("filename")); 
         obj.setSize(object.getString("size")); 
         obj.setInfoToken(object.getString("infoToken")); 
         obj.setDeleteToken(object.getString("deleteToken")); 
         obj.setSha1(object.getString("sha1")); 
         objectList.add(obj); 

         Log.d("log_tag", object.getString("filename")); 


        } 

        final ListView lv1 = (ListView) findViewById(R.id.listobjects); 
        lv1.setAdapter(new MyCustomBaseAdapter(this, objectList)); 

        lv1.setOnItemClickListener(new OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
        Object o = lv1.getItemAtPosition(position); 
        fileObject fullObject = (fileObject)o; 
        Toast.makeText(FilesActivity.this, "You have chosen: " + " " + fullObject.getFileName(), Toast.LENGTH_LONG).show(); 
        } 
        }); 

       }   
     } 
     catch(Exception e) 
     {  
      e.printStackTrace(); 
      Log.d("log_tag", "Error: " + e.toString()); 

     } 
    } 
} 

和適配器和文件對象僅僅是標準的,我怎樣才能使我的列表視圖顯示所有的值?

回答

0

看到這個:Populating a ListView using an ArrayList?

你重新創建通過循環每個週期的數組列表。它只會有一件物品。只需在for循環之前創建ArrayList,並將其填充到循環中。然後在完成之後,將整個列表添加到列表視圖中。無需每一個循環都能完成。應該看起來像:

  ArrayList<fileObject> objectList = new ArrayList<fileObject>(); 
      for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext();) { 
       String key = (String) keyIterator.next(); 
       JSONObject object = request.optJSONObject(key); 


       //ArrayList<fileObject> results = new ArrayList<fileObject>(); 
       if (object != null) { 
        fileObject obj = new fileObject(); 
        obj.setFileId(key); 
        obj.setFileName(object.getString("filename")); 
        obj.setSize(object.getString("size")); 
        obj.setInfoToken(object.getString("infoToken")); 
        obj.setDeleteToken(object.getString("deleteToken")); 
        obj.setSha1(object.getString("sha1")); 
        objectList.add(obj); 
        Log.d("log_tag", object.getString("filename")); 
       } 
      }//end the for-loop right here. No need to do that other stuff over and over. 
1

您正在每個循環迭代中創建一個新的ArrayList enter code here和一個新的MyCustomBaseAdapter。移動你的循環之外,它會顯示所有的項目。

+0

非常感謝! –