将数据从listview传递到android中的另一个活动?

时间:2012-01-31 13:22:32

标签: android

我使用以下代码显示所有城市时间。当我执行我的代码时,时间将显示在列表视图中,现在我的问题是我点击任何一个国家(列表中的位置),我必须传递国家名称,日,月,年和时间列表到另一个类(* ShowAnalogClock *。在 ShowAnalogClock 类中,我将显示从列表视图中提取的时间。

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>

我的Java代码是,

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;
    }
}

我将在下面的块中添加哪些代码,以传递listview内容

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);
            }
        });

3 个答案:

答案 0 :(得分:1)

您只能在“活动”之间传递基元和Parcelable对象。创建一个封装基元并实现Parcelable接口的类应该很容易。然后,您可以在Intent的Extras包中传递此类。

或者你可以直接传递你的原语,也可以在Intent Extras中传递。

答案 1 :(得分:0)

您可以使用ListView.getItemAtPosition(position)获取当前项目,然后将此信息传递给下一个活动。

参见例如this example

答案 2 :(得分:0)

我这样使用,效果很好。

ZONE_LIST.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long arg3) {
                // TODO Auto-generated method stub
               Intent i = new Intent(TimeZonesList.this, TimerClass.class);
        i.putExtra("the_image_id", timezonelist.get(position).name);
        i.putExtra("the_image_id2", timezonelist.get(position).time);
        startActivity(i);

            }
        });