Question -5
Create an application that will pass some number to the next screen and on the next screen that number of items should be display in the list.Step 1: Create New Project in Eclipse.
Step 2: Edit Main ListActivity xml file from res/layout folder of your project.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="71dp"
android:text="Enter Any Number"
android:textSize="30dp" />
<EditText
android:id="@+id/txtnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Integer Only" />
<Button
android:id="@+id/btnenter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Submit" />
</RelativeLayout>
Step 3: Add New ListDisplay xml file to your res/layout folder of your project.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No of Items Entered Are As Follows"
android:textSize="20dp" />
<ListView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/@android:id/list"
android:longClickable="true" android:fastScrollEnabled="false"></ListView>
</TableLayout>
Step 4: Edit Main List Activity Java file from src/package/ folder of your project.
package com.example.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class ListActivity extends Activity implements OnClickListener
{
Button submit;
EditText num;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
submit = (Button)findViewById(R.id.btnenter);
num = (EditText)findViewById(R.id.txtnum);
submit.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
@Override
public void onClick(View arg0)
{
String n = num.getText().toString();
Intent i = new Intent(this,ListDisplay.class);
i.putExtra("number",n);
startActivity(i);
}
}
Step 5: Add New ListDisplay Java class file to your src/package/ folder of your project.
package com.example.List;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ListDisplay extends ListActivity
{
ArrayList<String> arr=new ArrayList<String>();
ArrayAdapter<String> a;
int i=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disp);
a = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,arr);
setListAdapter(a);
for(i=1; i<= Integer.parseInt(getIntent().getStringExtra("number")); i++)
{
arr.add("Item : "+i);
}
//Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
}
}
No comments:
Post a Comment