0

我正在为一个网站制作一个应用程序,我需要一些帮助来制作一个 for 声明。我从 API(服务器)解析 JSON 并捕获它,这是有效的,但是我希望它显示在 ListView 中,我已经制作了我的适配器和所有这些,它正在工作。现在,当我启动应用程序时,列表视图中只显示一行。所以我不知道如何将所有值放入列表视图。

我的活动:

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());

        }
    }
}

适配器和文件对象只是标准的,我怎样才能让我的列表视图显示所有值?

4

2 回答 2

1

您将在每次循环迭代中创建一个新的 ArrayListenter code here和一个新的。MyCustomBaseAdapter将其移到循环之外,它将显示所有项目。

于 2013-07-10T22:25:58.290 回答
0

请参阅:使用 ArrayList 填充 ListView?

您正在通过循环的每个循环重新创建数组列表。它只会有一个项目。只需在 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.
于 2013-07-10T22:33:15.853 回答