Answer the question
In order to leave comments, you need to log in
How to launch Activity from ListFragment?
From the ListFragment, you need to launch the ViewActivity, in the line
intent.setClass(ScreenOne.this, ViewActivity.class);
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ScreenOne extends ListFragment {
String data[] = new String[] {
.....
.....
};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
}
public void onListItemClick (ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id); {
String itemname = new Integer(pos).toString();
Intent intent = new Intent();
intent.setClass(ScreenOne.this, ViewActivity.class);
Bundle b = new Bundle();
b.putString("defStrID", itemname);
intent.putExtras(b);
startActivity(intent);
}
}
}
Answer the question
In order to leave comments, you need to log in
Now, when you click on the ListFragment item, the application closes with an error, from the ListActivity it works.
Here is the ViewActivity
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ru.aam.pril.R;
public class ViewActivity extends Activity {
private WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle bundle = getIntent().getExtras();
String itemname = "n" + bundle.getString("defStrID");
Context context = getBaseContext();
String text = readRawTextFile(context,
getResources().getIdentifier(itemname, "raw", "ru.aam.pril"));
web = (WebView) findViewById(R.id.webview);
web.loadDataWithBaseURL("file:///android_res/raw/",
"<!Doctype html><html><head><meta charset=utf-8></head><body>"
+ text + "</body></html>", "text/html", "utf-8", "");
}
public static String readRawTextFile(Context ctx, int resId)
{
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();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question