0

I have a custom ListView, the code is below. Instead of writing:

"http://yoursite.com/image1.png" , "http://yoursite.com/image2.png".. and so one.

I want to store all the links in .txt files, and there, my device will read all the link. Is there anyway to do this??

package com.android.LazyList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class OnePiece extends Activity {

    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LazyAdapter adapter=new LazyAdapter(this, mStrings);
        final ListView list =(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);                                                                                                                                                                  

       }

    static final String[] mStrings= new String[] {
        "http://yoursite.com/image1.png" ,
        "http://yoursite.com/image2.png" ,
        "http://yoursite.com/image3.png" ,
        "http://yoursite.com/image4.png" ,
        "http://yoursite.com/image5.png" ,
        "http://yoursite.com/image6.png" ,
    };
}
Leon
  • 339
  • 1
  • 7
  • 23

1 Answers1

1

Add this bit of code to your function :

    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/LinkFile.txt");
    // Assuming each link to be on a new line
    StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    }
    String [] mStrings=text.toString().split("\n");

You should be good to go.

Vishnu
  • 2,024
  • 3
  • 28
  • 34
  • Replace the line where you declare and initialize 'mStrings[]' with this piece of code. Here you will read the content of the file, split it into an array of strings and then store it in the String variable 'mStrings[]'. – Vishnu Jan 01 '12 at 22:20
  • if you go to this page(http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview), find the code which is posted by Fedor. I am using that one to load my image in listview – Leon Jan 01 '12 at 22:22
  • I'm too lazy to go through the code for LazyList now. Try defining 'mStrings' as a final/static variable. – Vishnu Jan 01 '12 at 22:27