在我们的项目中,对于 ListView 项目,它链接到相同的内容。但我们需要为所有 ListView 项目提供不同的 Activity 。例如:如果我们单击历史 ListView ,它会打开相关的历史 Activity 。 .但相同的历史记录 Activity 正在为所有 ListView 项目打开..请帮助解决。
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);
}
});
}
}
历史.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");
}
}
历史.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>
谢谢,
最佳答案
您需要将 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;
}
}
}
关于android - android 中的 ListView : one activity it applied to all the listview items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23285686/