I used the below code for displaying all cities time. When i execute my code the time will displayed in a list view, now my problem is once i click any one of the country (position in list), i must pass the Country name, day, month, year and time from the list to another class (*ShowAnalogClock)*. In ShowAnalogClock class i will display the time which is fetched from the list view.
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="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoCompleteZone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1" >
<requestFocus />
</AutoCompleteTextView>
<ListView
android:id="@+id/zoneList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/autoCompleteTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
timezone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical" >
<TextView
android:id="@+id/timezone_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip" />
<TextView
android:id="@+id/timezone_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
My Java code is,
TimeZoneProjectActivity.java
package com.test.timezone;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TimeZoneProjectActivity extends Activity {
/** Called when the activity is first created. */
ListView ZONE_LIST;
AutoCompleteTextView zoneComplete_TXT;
TimeZoneAdaptor timezoneAdaptor;
List<TimeZoneDataList> timezonelist;
String[] listItems;
TextView auto_completeTXT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
auto_completeTXT = (TextView) findViewById(R.id.autoCompleteTextview);
ZONE_LIST = (ListView) findViewById(R.id.zoneList);
zoneComplete_TXT = (AutoCompleteTextView) findViewById(R.id.autoCompleteZone);
timezonelist = new ArrayList<TimeZoneDataList>();
timezoneAdaptor = new TimeZoneAdaptor(this, R.layout.timezoneview,
timezonelist);
ZONE_LIST.setAdapter(timezoneAdaptor);
ZONE_LIST.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long arg3) {
// TODO Auto-generated method stub
//
System.out.println("Clicked Position" + position);
}
});
}
@Override
protected void onStart() {
super.onStart();
listItems = TimeZone.getAvailableIDs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, listItems);
zoneComplete_TXT.setAdapter(adapter);
TimeZone timezone = null;
SimpleDateFormat format = new SimpleDateFormat(
"EEE, MMM d, yyyy h:mm a");
Date now = new Date();
for (int index = 0; index < listItems.length; ++index) {
timezone = TimeZone.getTimeZone(listItems[index]);
format.setTimeZone(timezone);
timezonelist.add(new TimeZoneDataList(
getDiaplayName(listItems[index]), format.format(now)));
timezone = null;
}
}
private String getDiaplayName(String timezonename) {
String displayname = timezonename;
int sep = timezonename.indexOf('/');
if (-1 != sep) {
displayname = timezonename.substring(0, sep) + ", "
+ timezonename.substring(sep + 1);
displayname = displayname.replace("_", " ");
}
return displayname;
}
public class TimeZoneAdaptor extends ArrayAdapter<TimeZoneDataList> {
List<TimeZoneDataList> objects = null;
public TimeZoneAdaptor(Context context, int textViewResourceId,
List<TimeZoneDataList> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
@Override
public TimeZoneDataList getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) TimeZoneProjectActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.timezoneview, null);
}
TimeZoneDataList data = objects.get(position);
if (null != data) {
TextView textName = (TextView) view
.findViewById(R.id.timezone_name);
TextView textTime = (TextView) view
.findViewById(R.id.timezone_time);
textName.setText(data.name);
textTime.setText(data.time);
}
return view;
}
}
}
TimeZoneDataList.java
package com.test.timezone;
public class TimeZoneDataList {
public String name;
public String time;
public TimeZoneDataList(String name, String time) {
this.name = name;
this.time = time;
}
}
In which code i will add in the following block, to pass the listview content
ZONE_LIST.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long arg3) {
// TODO Auto-generated method stub
System.out.println("Clicked Position" + position);
}
});
You can only pass primitives and Parcelable objects between Activities. It should be pretty easy to make a class which encapsulates your primitives and implements the Parcelable interface. You can then pass this class in the Extras bundle of the Intent.
Or you can just pass your primitives directly, also in the Intent Extras.