我正在为一个网站制作一个应用程序,我需要一些帮助来制作一个for语句。我从API(服务器)解析JSON并捕获它,这是有效的,但我希望它在ListView中显示,我已经制作了我的适配器和所有这些,这是有效的。现在,当我启动应用程序时,列表视图中只显示一行。所以我不知道如何将所有值都放入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());
}
}
}
适配器和fileObject只是标准配置,如何让listview显示所有值?
答案 0 :(得分:1)
您正在每次循环迭代中创建一个新的ArrayList enter code here
和一个新的MyCustomBaseAdapter
。将其移动到循环外部,它将显示所有项目。
答案 1 :(得分:0)
请参阅:Populating a ListView using an ArrayList?
你在循环的每个循环中重新创建了arraylist。它只会有一个项目。只需在for循环之前创建ArrayList,并在循环内填充它。然后,在完成后,将整个arraylist添加到listview。无需每一个周期都这样做。应该是这样的:
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.