matiszz
matiszz

Reputation: 616

Add data in listview from another activity

I'm doing an application which selects a random string from the strings.xml file, and I want to make a history of the elements that gives you, so I'm using a ListView.

So, how can I add this text to the list?

Thank you very much!

(Sorry for my bad English)

++++++++++++++++++++++++++++++++++ Edited ++++++++++++++++++++++++

Sorry, here is the "escoger.xml" file

<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>

Here is the "escoger.java" file, the controller of "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;
    }
    }
    }

The "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>

And finally, the "Historia.java", is what I don't know how to do...

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

}

Thank you very much!!!

Upvotes: 1

Views: 727

Answers (1)

J David Smith
J David Smith

Reputation: 4820

There are a few ways to do this. The most straightforward is to Bundle your data and pass it directly through the Intent. Alternatively, you could store them in an SQLite Database.

The flow will look something like this:

  • Activity A records and stores the selected strings locally or in SQLite DB
  • Optional: Pass Bundle containing history to History Activity
  • Load data in History Activity's onCreate. If you pass it through the Intent, use getIntent().getExtras(). If you use SQLite, you'll likely end up using a simple ContentProvider.

If you want the history to be stored persistently, you'll need to use an SQLite DB regardless of your methods here. In that case, I'd recommend just going with the DB for loading the History as well. If you don't want persistant storage, I'd go with the Intent and Bundle.

At this point, you'd simply throw your List<String> into an adapter and call your ListView's setAdapter() function.

Upvotes: 1

Related Questions