简体   繁体   中英

How can I get a listview with links to assets?

I have the HTML files in assets named n0.html , n1.html , etc. I want to create a listview with links to these files, but I don't know how to do it.

I have such a decision with a raw folder. How should I change it to assets files?

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();
    }
}

Added

I'm sorry if I ask too stupid questions and I ask too much, but I want to work my first application. This is very important for me. So it consists of two activities:

ViewActivity which I've changed according to your advices

public class ViewActivity extends Activity {

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

        setContentView(R.layout.view);

        Bundle bundle = getIntent().getExtras();

        String htmlFileName = "n" + bundle.getString("defStrID"); // Getting file name
        Context context = getBaseContext(); // Getting context. You still need that

        // Reading text file from resources by name
        try {
            String text = readAssetTextFile(context, htmlFileName);

            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
        }
        catch (IOException e) {
            Log.e("TAG", e); // Note that you will need to import android.util.Log
        }
    }

    public static String readAssetTextFile(Context ctx, String fileName) throws IOException // Reading the HTML file from assets
    {
        InputStream inputStream = ctx.getAssets().open(fileName);

        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();
    }
}

In Log.e("TAG", e); , Eclipse wants to change type of "e" to "String".

How can I fix this?

It will not be that much different. You know that you can access a file in the assets like this:

InputStream inputStream = ctx.getAssets().open(fileName);

You can place this instead of your line InputStream inputStream = ctx.getResources().openRawResource(resId); . Then you need to pass in the correct file name. When working with assets you need not use IDs.

Editing your snippet:

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

    setContentView(R.layout.view);

    Bundle bundle = getIntent().getExtras();

    String htmlFileName = "n" + bundle.getString("defStrID") + ".html"; // Getting file name
    Context context = getBaseContext(); // Getting context. You still need that

    // Reading text file from resources by name
    try {
        String text = readAssetTextFile(context, htmlFileName);

        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
    } catch (IOException e) {
        Log.e("TAG", "Exception thrown", e); // Note that you will need to import android.util.Log
    }
}

public static String readAssetTextFile(Context ctx, String fileName) // Reading HTML file from assets
{
     InputStream inputStream = ctx.getAssets().open(fileName);
     .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM