Vinoth
Vinoth

Reputation: 1349

Reading and Displaying form data on Android

I've a simple form page that I want to use to fetch user data. It contains three fields.

1.Name Field (Edit Text type)

2.Radio Button "Male or Female" (Radio Group within Radio Button)

3.Drop Down Menu "choose your country" (Spinner)

main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:padding="1px">
<TextView  
android:id="@+id/tasks_title"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/tasks"/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_below="@id/tasks_title"
android:layout_above="@+id/add_button"/>
<TextView
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:id="@id/android:empty"
android:text="@string/no_tasks"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@id/tasks_title"
android:layout_above="@+id/add_button"/>
<Button
android:id="@id/add_button"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/add_task"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

the program as viewed while running the emulator

I've created another Activity called it FormActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;   

public class FormActivity extends Activity {
private Button submitbutton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    submitbutton=(Button)findViewById(R.id.submit_button);
    submitbutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    Intent intent = new Intent(FormActivity.this, DisplayActivity.class);
    startActivity(intent);
    }
});
    /**Implements DropDownMenu (spinner) by pushing items into an array an displaying them**/
            Spinner spinner = (Spinner)findViewById(R.id.spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.countries_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }

 }

How can I use the data from the xml file to be displayed on another page. Please give me instructions as I am learning android now.

Thanks and have a good day

Upvotes: 0

Views: 2368

Answers (2)

Dani bISHOP
Dani bISHOP

Reputation: 1289

You can pass data as Extras in the intent but you can also have a custom class, inherited from Application, make it visible and fill it with values that you need. This is useful when you have a constant domain across your application (the same user, the initialization values, etc.) or 'global' functions (read-write local data, test any resource, etc.).

You add the application:name to the Manifest and then you can add your custom code in your own application class.

Upvotes: 1

Finn Larsen
Finn Larsen

Reputation: 2199

Intent intent = new Intent(FormActivity.this, DisplayActivity.class); 
EditText inputName = (EditText) findViewById(R.id.inputText);
String name = inputName.getText().toString();
intent.putExtra("name" , name );
startActivity(intent);

In the new Intent you use this snip:

Bundle extras = getIntent().getExtras();
String name = extras.getString("name");

Upvotes: 1

Related Questions