在我们的项目中,对于listview项目,它将链接到相同的内容。但是我们需要为所有listview项目提供不同的活动。例如:如果我们单击History listview,它将打开相关的历史记录活动。 。但是所有listview项目的相同历史活动都是开放的.plz有助于解决。
java代码:
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class OurSchool extends ListActivity{
String[] listItems={"History", "Mission And Objectives", "Principals Forewords", "School System",
"Calender", "Rules And Regulations", "Our Traditional", "Our Alumini Council","Our Pholosophy","Our Branches"};
boolean[] listImages= {true, true, true, true, true,true, true, true, true, true};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.our_school);
setListAdapter(new ImageAdapter(this, R.layout.our_school, R.id.text1, R.id.image1, listItems, listImages ));
this.getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
Intent i = new Intent(getApplicationContext(), History.class);
String product = null;
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
History.java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class History extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.setBackgroundColor(0);
mWebView.setBackgroundResource(R.drawable.bg);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("file:///android_asset/History.html");
}
}
history.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="About :LITTLE FLOWERS PUBLIC SCHOOL"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
谢谢,
答案 0 :(得分:2)
您需要将listViewItem Position传递给NextActivity。
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
Intent i = new Intent(getApplicationContext(), History.class);
// sending data to new activity
i.putExtra("pos", pos);
startActivity(i);
}
然后你的nextActivity看起来像
public class History extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
int pos = getIntent().getIntExtra("pos",0);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.setBackgroundColor(0);
mWebView.setBackgroundResource(R.drawable.bg);
mWebView.getSettings().setBuiltInZoomControls(true);
switch (pos) {
case 0:
mWebView.loadUrl("file:///android_asset/History.html");
break;
case 1:
mWebView.loadUrl("file:///android_asset/Objective.html");
break;
case 2:
mWebView.loadUrl("file:///android_asset/Principles.html");
break;
case 3:
mWebView.loadUrl("file:///android_asset/Calender.html");
break;
case 4:
mWebView.loadUrl("file:///android_asset/ShcoolSystem.html");
break;
default:
mWebView.loadUrl("file:///android_asset/History.html");
break;
}
}
}
答案 1 :(得分:0)
您可能需要稍微更改一下逻辑。在目前的代码中,您只在Intent中使用History.class。这就是为什么它为每个列表项开放。您可能必须根据单击的项目更改类名称。
例如
String item = (String)parent.getItemAtPosition(position);
Intent i;
if(item.equals("History")
{
i = new Intent(getApplicationContext(), History.class);
}
else if("Mission And Objectives")
{ i = new Intent(getApplicationContext(), AnotherClass.class);}