Wednesday, 14 March 2012

ListView using StringArray in Android



/***********************AndroidManifest.xml**********************/

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampleapp.application"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SampleAppActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/login_act" >
</activity>
<activity
android:name=".NewListView"
android:label="@string/app_name" >
</activity>
</application>

</manifest>

/**********************SampleAppActivity.java*********************/

package com.sampleapp.application;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SampleAppActivity extends Activity implements OnClickListener {

Button btn_next;
EditText nw_name;
EditText nw_degn;
EditText nw_cmpyUrl;
EditText nw_mob;
EditText nw_address;
Boolean isAlert = false;
private static final String REGULAR_EXP_NUMBER = "[\\d]+";
private static final String REGULAR_EXP_URL = "http://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nw_name = (EditText) findViewById(R.id.nw_name);
nw_degn = (EditText) findViewById(R.id.nw_degn);
nw_cmpyUrl = (EditText) findViewById(R.id.nw_cmpy);
nw_mob = (EditText) findViewById(R.id.nw_mob);
nw_address = (EditText) findViewById(R.id.nw_address);

btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(this);
}

public void onClick(View v) {
String name = (String) nw_name.getText().toString();
String degn = (String) nw_degn.getText().toString();
String cmpy = (String) nw_cmpyUrl.getText().toString();
String mobile = (String) nw_mob.getText().toString();
int mob;
if (!cmpy.matches(REGULAR_EXP_URL)) {
new AlertDialog.Builder(this).setTitle("Error message")
.setMessage("Url incorrect")
.setNeutralButton("Close", null).show();
isAlert = true;
}else
isAlert = false;

if (mobile.equals("") || !mobile.matches(REGULAR_EXP_NUMBER)) {
mob = 0;
} else {
mob = Integer.parseInt(nw_mob.getText().toString());
}

String address = nw_address.getText().toString();
String delimiter = ",";
String[] addArray = address.split(delimiter);

Intent i = new Intent(SampleAppActivity.this, LoginActivity.class);
Bundle b = new Bundle();
b.putString("name", name);
b.putString("degn", degn);
b.putString("cmpy", cmpy);
b.putInt("mob", mob);
b.putStringArray("addArray", addArray);
i.putExtras(b);

if (!isAlert)
startActivity(i);
}
}

/*******************LoginActivity.java*******************/

package com.sampleapp.application;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends Activity implements OnClickListener {
TextView nameText;
TextView address;
Button btn_show;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.register);

Bundle b = this.getIntent().getExtras();
String name = b.getString("name");
String degn = b.getString("degn");
String cmpyUrl = b.getString("cmpy");
int mob = b.getInt("mob");
String[] addArray = b.getStringArray("addArray");

String values = "Name: " + name + "\nDesignation: " + degn + "\nCompanyUrl: " + cmpyUrl + "\nContact: " + mob;
nameText = (TextView) findViewById(R.id.r_name);
nameText.setText(values);

address = (TextView) findViewById(R.id.r_address);
String addressInArray = "";
for (int i = 0; i < addArray.length; i++) {
addressInArray = addressInArray + addArray[i];
}
address.setText("\nAddress: "+addressInArray);
btn_show = (Button) findViewById(R.id.btn_show);
btn_show.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent i = new Intent(LoginActivity.this, NewListView.class);
startActivityForResult(i, 0);
}
}

/*********************NewListView.java*********************/

package com.sampleapp.application;

import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewListView extends ListActivity implements OnItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Resources res = getResources();
String[] cities = res.getStringArray(R.array.cities);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, cities);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}

/***************************main.xml************************/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:stretchColumns="1" >

<TableRow>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name" />

<EditText
android:id="@+id/nw_name"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:hint="Name"
android:text="" />
</TableRow>

<TableRow>
<TextView
android:id="@+id/degn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/designation" />

<EditText
android:id="@+id/nw_degn"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:hint="Designation"
android:text="" />
</TableRow>

<TableRow>
<TextView
android:id="@+id/cmpy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/company" />

<EditText
android:id="@+id/nw_cmpy"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:hint="Company Url"
android:text="" />
</TableRow>

<TableRow>
<TextView
android:id="@+id/mob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mobile" />

<EditText
android:id="@+id/nw_mob"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:hint="Enter number"
android:text="" />
</TableRow>

<TableRow>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/address" />

<EditText
android:id="@+id/nw_address"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:hint="Split with ,"
android:text="" />
</TableRow>

<TableRow>
<Button
android:id="@+id/btn_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/go" >
</Button>

<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/next" >
</Button>
</TableRow>
</TableLayout>
</LinearLayout>

/**************************register.xml***************************/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/r_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<TextView
android:id="@+id/r_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/show" >
</Button>

</LinearLayout>

/***********************list_item.xml**********************/

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

/**********************strings.xml*************************/

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">hi, AlertActivity!</string>
<string name="app_name">SampleApp</string>
<string name="login_act">LoginAct</string>
<string name="name">Name</string>
<string name="designation">Designation</string>
<string name="company">Company Url</string>
<string name="mobile">Mobile</string>
<string name="address">Address</string>

<string-array name="cities">
<item>Chennai</item>
<item>Trichy</item>
<item>Perambalur</item>
<item>Salem</item>
<item>Karur</item>
<item>Namakkal</item>
<item>Pondy</item>
</string-array>

<string name="go">GO</string>
<string name="next">NEXT</string>
<string name="show">SHOW LISTVIEW</string>

</resources>

No comments:

Post a Comment