如何创建包含资源html文件链接的listview?

时间:2012-02-17 15:23:21

标签: android html listview

我有一个活动,我希望向ListView显示主题列表,这些主题链接到assets文件夹中的html文件。

以下是 res / raw 文件夹中的文件代码(名称为n0.txt,n1.txt等):

public class ViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.view);


        Bundle bundle = getIntent().getExtras();

        String itemname = "n" + bundle.getString("defStrID"); //getting string and forming resource name

        Context context = getBaseContext(); //getting context

        //reading text file from resources by name
        String text = readRawTextFile(context, getResources().getIdentifier(itemname, "raw", "ru.falcon5f.carguide;"));

        WebView wWebView = (WebView) findViewById(R.id.webView);
        String summary = "<!Doctype html><html><head><meta charset=utf-8></head><body>" + text + "</body></html>";
        wWebView.loadData(summary, "text/html", "utf-8"); //uploading text to webview
    }

    public static String readRawTextFile(Context ctx, int resId) //reading text raw txt file
    {
         InputStream inputStream = ctx.getResources().openRawResource(resId);

            InputStreamReader inputreader = new InputStreamReader(inputStream);
            BufferedReader buffreader = new BufferedReader(inputreader);
             String line;
             StringBuilder text = new StringBuilder();

             try {
               while (( line = buffreader.readLine()) != null) {
                   text.append(line);
                   text.append('\n');
                 }
           } catch (IOException e) {
               return null;
           }
             return text.toString();
    }
}

如何从资产(名为n0.html等)的html文件中执行此操作?

1 个答案:

答案 0 :(得分:1)

要从您的资产中获取InputStream文件,您可以使用:

InputStream is=getAssets().open("n0.txt");

然后您应该像处理原始资源一样处理流。