5

I am trying to read a .txt file from dropbox that has a public shared link. What I want to do is read this .txt and display all the data inside this file on a listview in android.

http://txt.do/5zflt (I don't have access to drop on my current computer so I want to use this link as an example)

The file is called PersonStatus that will contains text something along the lines of;

Online
Offline
Active
Holidays
….
….
…
…
…
…

basically what I want to do is used the shared dropbox link to read this text and display it in my listview on android but I am not sure how I can approach this. I have searched online for tutorials and guides but being new to android I haven't been able to find something of much use;

For example, I found this link: Read a file from dropbox where the OP has asked a similar question but has not provided enough code for me to understand how I can approach this. Also through my research I found that dropbox has Android Sync API: https://www.dropbox.com/developers-v1/sync/start/android but being new to programming I am not quite sure how to go about implementing and making it work.

I would really appreciate if anyone can help. Thanks in advance. If my question was not clear please let me know and I will try explaining it better.

Community
  • 1
  • 1
Henry
  • 1,042
  • 2
  • 17
  • 47

2 Answers2

5

I put here on GitHub a sample project implementing scenario you described (I also put a public file with structure you reported here on Dropbox). Inside project you will find the following main components:

  1. MainActivity - It includes a RecyclerView that will be populated with file content rows. In order to get file content, the activity relies on a retained fragment, allowing download task to be kept in case of screen rotation (see here for details on configuration changes). File is downloaded automatically as soon as activity is created, but you can force re-download by using SYNC button on the action bar.
  2. DownloadFragment - It is a retained fragment that wraps the AsyncTask used for downloading file. It provides a Callback implemented by the MainActivity for handling specific events occurring during download (e.g. onPrepare, onProgress, onDownloadCompleted, onDownloadFailed). You can use them for example for showing a progress bar or other feedbacks to user.
  3. FileContentAdapter - It is the adapter used for displaying file content inside the RecyclerView.

Some Limitations

  • This application is not focused on Dropbox. In case file is public on internet you can download it regardless of who is hosting it. In case your purpose is keeping the activity automatically synchronized with file on Dropbox it would be probably better to exploit Dropbox SDK, in particular if you are planning to access files that are private on Dropbox.
  • AsyncTask implementation should be improved, for example by implementing WakeLock management.
andrea.petreri
  • 4,137
  • 2
  • 22
  • 21
  • Would it be possible to add a feature that saves the data on the file read from drop on user phone as caches and then only look for updates from dropbox file, as this will allow user to access the info without having internet connection – Henry Jan 29 '16 at 10:49
  • @Henry Well... yes, it is possible to keep a local copy of the file. Just problem is that you probably need to implement a periodic file retrieval in background for making comparisons between current version and the one you have locally. In case your target is to use Dropbox maybe it's better to have a look at Dropbox SDK, both for handling authentication (if needed) and for more easily getting updates. As a first step I can try to modify code for handling file storage locally. – andrea.petreri Jan 29 '16 at 11:00
  • 2
    Also, would it be possible for you to work with this data: https://dl.dropboxusercontent.com/u/53441658/data.txt and implmenting these conditions https://dl.dropboxusercontent.com/u/53441658/readfile.txt please, it will make it easier for me to combine my code with yours. – Henry Jan 29 '16 at 11:04
  • @Henry I've pushed on repository updated code for handling new file format, including logic you provided for parsing it. I've created a couple of packages for better keeping logic. Now there is a dedicated parser class for each file format, together with a dedicated recycler view adapter for rendering elements coming out from the corresponding parser. Hope this could help! – andrea.petreri Jan 29 '16 at 19:38
  • Thanks, I'll have a look in a bit and let you know. Thanks again. – Henry Jan 29 '16 at 19:42
  • @Henry I've just added storage for downloaded file, so that in case you open the application without connection you will see the latest downloaded version. You find everything in repo on GitHub. – andrea.petreri Jan 29 '16 at 20:37
  • Thanks, look great. I am try implementing it in my app and will let you know how I get along. Btw, did you use the if-else statments? https://dl.dropboxusercontent.com/u/53441658/readfile.txt. Thanks once again. – Henry Jan 30 '16 at 10:49
  • @Henry Yes I used exactly that code for implementing file parser. – andrea.petreri Jan 30 '16 at 10:50
  • Also, you see you have two versions for file adapter, v1 and v2, do I only work with v2 because it implements the layout I want or is v1 part of this – Henry Jan 30 '16 at 10:52
  • @Henry You can remove v1 without problems. I just implemented parsing so that it is eventually easier to handle new file formats. – andrea.petreri Jan 30 '16 at 10:53
  • Thanks, I will try it to merge it with my code and let you know how I get along :) – Henry Jan 30 '16 at 10:57
  • Hi, you see fileV2Adapter is that a class I need to make or will it come under exampleviewholder and/or exampleview model in the recylerview guide I was using. https://github.com/Wrdlbrnft/Searchable-RecyclerView-Demo/tree/master/app/src/main/java/com/github/wrdlbrnft/searchablerecyclerviewdemo/ui/adapter – Henry Jan 30 '16 at 14:01
  • @Henry Well... in repo I just defined an Adapter for my `RecycleView`. You can of course use another one. You just need to be careful with `ViewHolder` type (it is different in my Adapter and in the one you shared), but if you look at code of `FileV2Adapter`, it is quite simple. It just render each element filling some `TextView`s. Of course if you have specific issues with it, let me know. – andrea.petreri Jan 30 '16 at 14:06
  • would there be any issues if i create fileV2Adapter on top of exampleviewholder and exampleview – Henry Jan 30 '16 at 14:09
  • @Henry you mean you want to use my adapter with your code? I think there's no problem. You just need to change the ViewHolder type. In general the cool thing of adapters is that they just wrap items rendering logic, so you can define many of them and use the one you prefer, without impacting on your data model or activity. – andrea.petreri Jan 30 '16 at 14:13
  • But would there be a problem if I use all three of them – Henry Jan 30 '16 at 14:19
  • @Henry wait... you can set just one adapter to your recyclerview. What I mean is that you can define many adapters and decide which one to use. But you must use only one. – andrea.petreri Jan 30 '16 at 14:20
  • oh ok, I will try to implement your adapter logic in mine but I am little confused because I have two adapter one is exampleviewholder and other exampleview, under which one of these do I merge your adpater code? – Henry Jan 30 '16 at 14:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102080/discussion-between-thetonrifles-and-henry). – andrea.petreri Jan 30 '16 at 14:24
2

In my apps i use this code to get content of a shared dropbox file. I call this code inside of AsyncTask.

Edited: Here is a sample

public class DropboxSampleActivity extends Activity {

private ListView listViewDropbox;
private ArrayAdapter<String> adapter = null;
private static String URL_FILE_DROPBOX = "https://www.dropbox.com/s/xxxxxxxxxxxx/xxxxxxxxxxxx?dl=1";
private ArrayList<String> listElementItem;


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

    super.onCreate(savedInstanceState);
    listViewDropbox = (ListView) findViewById(R.id.listViewDropbox);
    DropboxItemAsyncTask dropboxItemAsyncTask = new DropboxItemAsyncTask();
    dropboxItemAsyncTask.execute();
}

class DropboxItemAsyncTask extends AsyncTask {

    protected Integer doInBackground(Object[] params) {

        try {
            listElementItem = new ArrayList<>();
            URLConnection conn = new URL(URL_FILE_DROPBOX).openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            String line = null;
            while ((line = reader.readLine()) != null) {
                listElementItem.add(line);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(Object o) {
        if (adapter == null) {
            adapter = new ArrayAdapter(DropboxSampleActivity.this,
                    android.R.layout.simple_list_item_1, listElementItem);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    listViewDropbox.setAdapter(adapter);
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });

        }
    }
};

}

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
  • Hi, sorry for the late reply. I am not really sure how AsyncTask works or how I can go about writing one. Please could you give me a hand. – Henry Jan 22 '16 at 08:47
  • Also, when you read your file from dropbox using this, does it keep on reading every second or does it read it once and then only read next time when the file is updated/more text was added. – Henry Jan 22 '16 at 09:03
  • and how do you display the text once you have read it, I will be displaying it in listview. Are you doing something similar? – Henry Jan 22 '16 at 11:39
  • Hi, I am finding it hard to get this to work. Please can you help. – Henry Jan 24 '16 at 16:53
  • I'll create a sample project to help you. – Gueorgui Obregon Jan 25 '16 at 00:50
  • Thanks, I look forwward to it :) – Henry Jan 25 '16 at 10:05
  • 1
    Hi @g2o I have tried this code again but I can't seem to get it working. Please could you provide me with the sample project. – Henry Jan 28 '16 at 11:05