【发布时间】:2023-03-24 19:40:01
【问题描述】:
我有一个 ListView,可以像这样从字符串文件中获取 TutorialTitels
public class tutorialActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);
registerClickCallBack();
ListView listView = (ListView) findViewById(R.id.tutorialList);
String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);
String[] values = new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.tutorialList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
}
});
}
}
当我点击 listViewItem 时,我想打开一个 Activity,它会显示以下内容:
点击的TutorialTitel
教程内容(这也将来自一个字符串文件,像这样
String tutorialContent1 = getResources().getString(R.string.tutorial1_content);)教程示例((这也将来自一个字符串文件,像这样
String tutorialExample1 = getResources().getString(R.string.tutorial1_example);))
我已经有一个这样的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:id="@+id/tutorialTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/split"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="15dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="5dp"
android:background="#38b34a"
android:orientation="horizontal" />
<TextView
android:id="@+id/tutorialContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text=""/>
<TextView
android:id="@+id/tutorialExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text=""/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
问题:如何将数据传递给我点击的ListviewItem 对应的活动?我应该做类似的事情
if(position == 0){
//Send data through extra bundle
}
else if(position == 1){
//send data through extra bundle
}
但我认为应该有更好的方法,但我不知道具体如何,因为如果我将获得 100 个教程,我应该如何管理这样长的列表? 有人可以指出正确的方向吗?最好的方法是什么?
【问题讨论】:
-
sqlite 数据库呢?例如。只有一个表:rowid、标题、内容、示例。然后您可以通过 id 访问您的教程。第一个活动将显示您所有的教程标题,第二个活动将是详细视图。有一个很好的框架可以与数据库一起使用 - 用于 android 的“ORMlite”。
标签: android android-listview android-activity