Android 在listview中添加来自其他活动的数据
我正在做一个应用程序,它从strings.xml文件中选择一个随机字符串,我想为提供给您的元素创建一个历史记录,所以我使用的是ListView 那么,我如何才能将此文本添加到列表中 多谢各位 (对不起,我的英语不好) ++++++++++++++++++++++++++++++++++编辑++++++++++++++++++++++++ 对不起,这是“escoger.xml”文件 “historia.xml”:Android 在listview中添加来自其他活动的数据,android,listview,android-activity,Android,Listview,Android Activity,我正在做一个应用程序,它从strings.xml文件中选择一个随机字符串,我想为提供给您的元素创建一个历史记录,所以我使用的是ListView 那么,我如何才能将此文本添加到列表中 多谢各位 (对不起,我的英语不好) ++++++++++++++++++++++++++++++++++编辑++++++++++++++++++++++++ 对不起,这是“escoger.xml”文件 “historia.xml”: 非常感谢 有几种方法可以做到这一点。最简单的方法是获取数据并将其传递。或者,您可以将
非常感谢 有几种方法可以做到这一点。最简单的方法是获取数据并将其传递。或者,您可以将它们存储在一个容器中 该流将如下所示:
在本地或SQLite DB中记录并存储所选字符串活动A
- 可选:将包含历史的
捆绑传递到
历史活动
- 在
的历史记录活动
中加载数据。如果通过onCreate
传递,请使用Intent
。如果使用SQLite,很可能最终会使用一个简单的getIntent().getExtras()
Intent
和捆绑包
此时,您只需将列表
放入适配器,并调用列表视图
的setAdapter()
函数即可。最简单的方法是获取数据并将其传递。或者,您可以将它们存储在一个容器中
该流将如下所示:
活动A
在本地或SQLite DB中记录并存储所选字符串
- 可选:将包含历史的
捆绑传递到历史活动
- 在
历史记录活动
的onCreate
中加载数据。如果通过Intent
传递,请使用getIntent().getExtras()
。如果使用SQLite,很可能最终会使用一个简单的
如果希望持久存储历史记录,则需要使用SQLite DB,而不管这里使用什么方法。在这种情况下,我建议也使用DB来加载历史记录。如果您不想要持久存储,我会选择Intent
和捆绑包
此时,您只需将您的列表
放入适配器,并调用列表视图
的setAdapter()
函数。首先添加一些代码,让我们知道您遇到的确切问题。现在我已经把文件放好了。首先添加一些代码,让我们知道您遇到的确切问题。现在我已经把文件放好了。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<Button
android:id="@+id/btnEscoger"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="Aleatorio"
android:padding="10dp"
android:text="@string/btnEscoger"
android:textSize="20dp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTituloComida"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
</LinearLayout>
package org.example.app;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Escoger extends Activity implements OnClickListener{
TextView tvTituloComida;
Button btnEscoger;
int plato;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.escoger);
tvTituloComida = (TextView)findViewById(R.id.tvTituloComida);
btnEscoger = (Button)findViewById(R.id.btnEscoger);
btnEscoger.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.btnEscoger:
Random r=new Random();
plato = r.nextInt(4-1);
if(plato==1){
tvTituloComida.setText(R.string.Comida1); }
if(plato==2){
tvTituloComida.setText(R.string.Comida2); }
if(plato==3){
tvTituloComida.setText(R.string.Comida3); }
if(plato==4){
tvTituloComida.setText(R.string.Comida4); }
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:id="@+id/tvHistorialTilte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tilte_History"
android:textAppearance="?android:attr/textAppearanceLarge" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</FrameLayout>
</LinearLayout>
package org.example.app;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Historia extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.historia);
//Code here
}