我有两个活动。一个在列表视图中显示所有通知,并显示指向 pdf 文件的链接。第二个活动在选择项目时运行。基本上第二个活动应该在项目点击时启动,它应该打开 pdf 文件。
问题是它不会在项目选择上启动第二个活动。发布代码供您参考。
代码:
AllEventsActivity.java
public class AllEventsActivity extends ListActivity {
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://myurl/fetchnotice.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "notices";
private static final String TAG_PID = "nid";
private static final String TAG_NAME = "notice";
private static final String TAG_INFO = "noticeinfo";
private static final String TAG_DATE = "date";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_events);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
Button go_btn = (Button) findViewById(R.id.go_btn);
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching pdfviewer activity
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.einfo)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
PdfViewActivity.class);
// sending pid to next activity
in.putExtra("pathid", pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllEventsActivity.this);
pDialog.setMessage("Loading results. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String info = c.getString(TAG_INFO);
String date = c.getString(TAG_DATE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_INFO, info);
map.put(TAG_DATE, date);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// nothing found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllEventsActivity.this, productsList,
R.layout.list_item_eve, new String[] { TAG_PID,
TAG_NAME, TAG_INFO, TAG_DATE },
new int[] { R.id.eid, R.id.ename, R.id.einfo, R.id.date});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
第二个活动:
public class PdfViewActivity extends Activity {
private WebView webview;
//private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoview);
webview = (WebView) findViewById(R.id.my_img);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
String id = getIntent().getExtras().getString("pathid");
webview.loadUrl("http://docs.google.com/viewer?url="+id);
}
}
XML 代码:list_item_eve.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/itm_bg"
android:textColor="@drawable/text_color">
<TextView
android:id="@+id/eid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="@drawable/text_color"/>
<!-- Name Label -->
<TextView
android:id="@+id/ename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="12dip"
android:textStyle="italic"
android:textColor="@drawable/text_color"/>
<TextView
android:id="@+id/einfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dip"
android:autoLink="web"
android:textColorLink="@color/link"
android:paddingTop="6dip"
android:textSize="14dip"
android:textStyle="bold"
android:textColor="@drawable/text_color"/>
<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColorLink="@color/link"
android:paddingLeft="15dip"
android:paddingTop="6dip"
android:textColor="#ffffff"
android:textSize="9dip"
android:textStyle="italic" />
<View android:id="@+id/sepbottom"
android:background="#000000"
android:layout_width = "fill_parent"
android:layout_height="2dip"
android:layout_marginTop="10dip"
/>
</LinearLayout>
all_events.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/itm_bg"
android:textColor="@drawable/text_color">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>