aws s3バケットからすべてのファイルを正常にダウンロードできますので、特定のリストアイテムをクリックするとリストアイテムがダウンロードされます。しかし、私はすべてのs3のバケツアイテムが自動的にダウンロードされたときに、活動が開始されます。S3バケット全体を自動的にダウンロードするには
/**
* DownloadSelectionActivityは、バケット内のファイルのリストを表示します。ユーザーは *ダウンロードするファイルを選択できます。 */ パブリッククラスDownloadSelectionActivityあなたは既にバケット内のオブジェクトのリストを持っているListActivity {
// The S3 client used for getting the list of objects in the bucket
private AmazonS3Client s3;
// An adapter to show the objects
private SimpleAdapter simpleAdapter;
private ArrayList<HashMap<String, Object>> transferRecordMaps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_selection);
initData();
initUI();
}
@Override
protected void onResume() {
super.onResume();
// Refresh the file list.
new GetFileListTask().execute();
}
private void initData() {
// Gets the default S3 client.
s3 = Util.getS3Client(DownloadSelectionActivity.this);
transferRecordMaps = new ArrayList<HashMap<String, Object>>();
}
private void initUI() {
simpleAdapter = new SimpleAdapter(this, transferRecordMaps,
R.layout.bucket_item, new String[] {
"key"
},
new int[] {
R.id.key
});
simpleAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
switch (view.getId()) {
case R.id.key:
TextView fileName = (TextView) view;
fileName.setText((String) data);
return true;
}
return false;
}
});
// When an item is selected, finish the activity and pass back the S3
// key associated with the object selected
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int pos, long id) {
Intent intent = new Intent();
intent.putExtra("key", (String) transferRecordMaps.get(pos).get("key"));
setResult(RESULT_OK, intent);
finish();
}
});
}
/**
* This async task queries S3 for all files in the given bucket so that they
* can be displayed on the screen
*/
private class GetFileListTask extends AsyncTask<Void, Void, Void> {
// The list of objects we find in the S3 bucket
private List<S3ObjectSummary> s3ObjList;
// A dialog to let the user know we are retrieving the files
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(DownloadSelectionActivity.this,
getString(R.string.refreshing),
getString(R.string.please_wait));
}
@Override
protected Void doInBackground(Void... inputs) {
// Queries files in the bucket from S3.
s3ObjList = s3.listObjects(Constants.BUCKET_NAME).getObjectSummaries();
transferRecordMaps.clear();
for (S3ObjectSummary summary : s3ObjList) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key", summary.getKey());
transferRecordMaps.add(map);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
simpleAdapter.notifyDataSetChanged();
}
}
}
提案:あなたはdoInBackgroundでハッシュマップまたはリストを返す必要があり、それはあなたが、アダプタを更新することができ、そこからonPostExecuteにパラメータです。それがAsyncTaskの使用方法です –
ありがとうございます.. !! –