|
|
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
For an introduction to how you can dynamically insert views using an adapter, read Building Layouts with an Adapter.
Step 1-
Create a Android Application Project "ContactDetail".
Step 2-
Create a Android XML file "activity_contact.xml".
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvContact"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
Step 3-
Create a another XML file "single_list_contact.xml".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tvsinglecontact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ff00ff"/>
</LinearLayout>
Step 4-
Create a java class "contacts.java".
package com.example.contactdetails;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
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;
public class Contacts extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] contact_numbers= getResources().getStringArray(R.array.contact_numbers);
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_contacts,R.id.tvContact, contact_numbers));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String numbers= ((TextView)view).getText().toString();
Intent i= new Intent(getApplicationContext(), SingleListContact.class);
i.putExtra("numbers", numbers);
startActivity(i);
}
});
}
}
Step 5- Create a another java file "SingleListContact.java".
package com.example.contactdetails;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleListContact extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_contact);
TextView txtContact=(TextView)findViewById(R.id.tvsinglecontact);
Intent i = getIntent();
String numbers = i.getStringExtra("numbers");
txtContact.setText(numbers);
}
}
Step 6-
Create a xml file in the Value Folder name is "contect_list.xml";
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="contact_numbers">
<item >Name</item>
<item >Name</item>
<item >Name</item>
<item >Name</item>
<item >Name</item>
<item >Name</item>
<item >Name</item>
<item >Name</item>
so on...
</string-array>
</resources>
Step 7 -
Android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactdetails"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Contacts"
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=".SingleListContact"
android:label="Single List">
</activity>
</application>
</manifest>
Step 8-
Save and Run on the Emulator Machine or F11.
0 comments:
Post a Comment