Browse Source

List of files is shown in listview

David A. Velasco 11 years ago
parent
commit
d76180eabc

+ 5 - 0
oc_framework/sample_client/res/layout/file_in_list.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"	
+/>

+ 1 - 1
oc_framework/sample_client/res/values/strings.xml

@@ -12,6 +12,6 @@
 	<string name="todo_start_remote_deletion">TODO: start remote deletion</string>    
 	<string name="todo_start_download">TODO: start download</string>    
     <string name="todo_start_local_deletion">TODO: start local deletion</string>
-    <string name="todo_operation_finished_in_success">TODO: operation finished in sucess</string>    
+    <string name="todo_operation_finished_in_success">TODO: operation finished in success</string>    
     <string name="todo_operation_finished_in_fail">TODO: operation finished in fail</string>    
 </resources>

+ 23 - 0
oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/FilesArrayAdapter.java

@@ -0,0 +1,23 @@
+package com.owncloud.android.oc_framework.sampleclient;
+
+import android.content.Context;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+import com.owncloud.android.oc_framework.operations.RemoteFile;
+
+public class FilesArrayAdapter extends ArrayAdapter<RemoteFile> {
+	
+	public FilesArrayAdapter(Context context, int resource) {
+		super(context, resource);
+	}
+	
+	public View getView(int position, View convertView, ViewGroup parent) {
+		TextView textView = (TextView)super.getView(position, convertView, parent);
+	    textView.setText(getItem(position).getRemotePath());
+	    return textView;
+	}		
+}
+

+ 35 - 8
oc_framework/sample_client/src/com/owncloud/android/oc_framework/sampleclient/MainActivity.java

@@ -1,9 +1,12 @@
 package com.owncloud.android.oc_framework.sampleclient;
 
+import java.util.List;
+
 import com.owncloud.android.oc_framework.accounts.AccountUtils;
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;
+import com.owncloud.android.oc_framework.operations.RemoteFile;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFolderOperation;
@@ -14,18 +17,31 @@ import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.view.View;
+import android.widget.ListView;
 import android.widget.Toast;
 
 public class MainActivity extends Activity implements OnRemoteOperationListener {
 	
 	private Handler mHandler;
 	
+	private WebdavClient mClient; 
+	
+	private FilesArrayAdapter mFilesAdapter;
+	
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
+        
         mHandler = new Handler();
+        
+    	Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0);
+    	mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
+    	mClient.setBasicCredentials(getString(R.string.username), getString(R.string.password));
+    	
+    	mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
+    	((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
     }
     
     public void onClickHandler(View button) {
@@ -52,10 +68,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener
     
     private void startRefresh() {
     	RemoteOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
-    	Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0);
-    	WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
-    	client.setBasicCredentials(getString(R.string.username), getString(R.string.password));
-    	refreshOperation.execute(client, this, mHandler);
+    	refreshOperation.execute(mClient, this, mHandler);
     }
     
     private void startUpload() {
@@ -76,11 +89,25 @@ public class MainActivity extends Activity implements OnRemoteOperationListener
 
 	@Override
 	public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
-		if (result.isSuccess()) {
-			Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show();
-		} else {
+		if (!result.isSuccess()) {
 			Toast.makeText(this, R.string.todo_operation_finished_in_fail, Toast.LENGTH_SHORT).show();
+			
+		} else if (operation instanceof ReadRemoteFolderOperation) {
+			onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
+			
+		} else {
+			Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show();
 		}
 	}
-    
+
+	private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) {
+		mFilesAdapter.clear();
+		List<RemoteFile> files = result.getData();
+		if (files != null && files.size() > 0) {
+			mFilesAdapter.addAll(files);
+			mFilesAdapter.remove(mFilesAdapter.getItem(0));
+		}
+		mFilesAdapter.notifyDataSetChanged();
+	}
+	
 }