我正在做一个从 strings.xml 文件中选择一个随机字符串的应用程序,我想记录给你的元素的历史记录,所以我使用的是 ListView。
那么,如何将此文本添加到列表中?
非常感谢你!
(对不起,我的英语不好)
++++++++++++++++++++++++++++++++++++编辑+++++++++++++++ +++++++++
抱歉,这里是“escoger.xml”文件
<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>
这里是“escoger.java”文件,“escoger.xml”的控制器
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;
}
}
}
“historia.xml”:
<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>
最后,“Historia.java”,是我不知道该怎么做的......
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
}
非常感谢你!!!