我有一个自定义ListView,代码如下。而不是写:
“http://yoursite.com/image1.png”,“http://yoursite.com/image2.png”..等等。
我想将所有链接存储在.txt文件中,然后我的设备将读取所有链接。无论如何都要这样做??
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" ,
};
}
答案 0 :(得分:1)
将这段代码添加到您的函数中:
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");
你应该好好去。