使用带有Extra的Intent启动活动不会显示所有Extras [英] Start activity using intent with extra does not show all the extras

查看:204
本文介绍了使用带有Extra的Intent启动活动不会显示所有Extras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在setOnItemClickListener内,我使用一种意图来启动新活动.我为此目的添加了7个字符串作为额外内容.当我单击(ListView的)一项时,我希望所有这些额外内容都显示在新屏幕上.但我只看到前4个临时演员.这是新活动的布局文件:

Inside a setOnItemClickListener I use an intent to start a new activity. I put 7 strings as extras for this intent. When I click an item (of a ListView) I want all these extras to be displayed (on a new screen). But I see only the first 4 extras. Here's the layout file for the new activity:

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

    <TextView
        android:id="@+id/address_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/city_entry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28dip" />
        <TextView
            android:id="@+id/zip_entry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28dip" />
        <TextView
            android:id="@+id/state_entry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28dip" />
    </LinearLayout>    

    <TextView
        android:id="@+id/name_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <TextView
        android:id="@+id/number_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <TextView 
        android:id="@+id/store_id_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip"/>
</LinearLayout>

然后是侦听器代码:

lv_custom.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //Log.v("ITEM",lv_custom.getItemAtPosition(position).toString());
        c.moveToPosition(position);
        String adr = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ADDRESS));
        String city = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_CITY));
        String name = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
        String store_id = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STORE_ID));
        String phone = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_PHONE));
        String zip = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ZIP));
        String state = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STATE));
        Intent intent=new Intent(DisplayActivity.this,DisplayInfoActivity.class);
        intent.putExtra("Address: ",adr);
        intent.putExtra("City: ", city);
        intent.putExtra("Zip: ", " " + zip + ", ");
        intent.putExtra("State: ", state);
        intent.putExtra("Name: ", name);
        intent.putExtra("Store ID: ", "Store ID " + store_id);
        intent.putExtra("Phone: ", phone);
        startActivity(intent);
    }
});

这是新的活动代码:

public class  DisplayInfoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_entry);

        TextView address = (TextView) findViewById(R.id.address_entry);
        TextView city = (TextView) findViewById(R.id.city_entry);
        TextView zip = (TextView) findViewById(R.id.zip_entry);
        TextView state = (TextView) findViewById(R.id.state_entry);
        TextView name = (TextView) findViewById(R.id.name_entry);
        TextView store_id = (TextView) findViewById(R.id.store_id_entry);
        TextView phone = (TextView) findViewById(R.id.number_entry);
        Intent intent = getIntent();
        address.setText(intent.getStringExtra("Address: "));
        city.setText(intent.getStringExtra("City: "));
        zip.setText(intent.getStringExtra("Zip: "));
        state.setText(intent.getStringExtra("State: "));
        name.setText(intent.getStringExtra("Name: "));
        store_id.setText(intent.getStringExtra("Store ID: "));
        phone.setText(intent.getStringExtra("Phone: "));
    }
}

问题在于它仅显示地址,城市,邮政编码和州.它不显示名称,商店ID和电话号码.他们会怎样?为什么它们没有在新屏幕上显示/看到?实际上,在我添加更多附加功能之前,我可以看到所有附加功能.临时演员的数量有问题吗?感谢您的帮助,如果您对我有答案!

The problem is that it shows only the address, the city, the zip code, and the state. It does not show the name, store id and the phone number. What happens with them? Why they are not shown/seen on the new screen? Actually, before I added more extras, I could see all the extras in my intent. Is there a problem with the number of extras? Thanks for help, if you have an answer for me!

推荐答案

您可以简化附加内容的添加-无需使用其他字符作为键:

You can simplify adding extras - no need to use extra characters as key:

intent.putExtra("Address",adr);
intent.putExtra("City", city);
intent.putExtra("Zip", " " + zip + ", ");
intent.putExtra("State", state);
intent.putExtra("Name", name);
intent.putExtra("StoreID", "Store ID " + store_id);
intent.putExtra("Phone", phone);

以及相应地,在您的DisplayInfoActivity中:

and correspondingly, in your DisplayInfoActivity:

address.setText(intent.getStringExtra("Address"));
city.setText(intent.getStringExtra("City"));
zip.setText(intent.getStringExtra("Zip"));
state.setText(intent.getStringExtra("State"));
name.setText(intent.getStringExtra("Name"));
store_id.setText(intent.getStringExtra("StoreID"));
phone.setText(intent.getStringExtra("Phone"));

接下来,如果布局不正确,您是否尝试过将额外内容打印到logcat?将此添加到您检索额外内容的行下面:

Next, have you tried printing the extras to logcat, in case your layout is incorrect? Add this below the lines where you retrieve extras:

Log.d("MYTAG", "Name: " + intent.getStringExtra("Name"));

,看看是否可以在logcat中看到它.如果是,则您的布局可能不会显示所有这些TextView.

and see if you can see it in logcat. If yes, maybe your layout is not showing all those TextViews.

顺便说一句,尝试在布局文件中移动最后3个TextView(名称,store_id和编号),在第二个LinearLayout(以及所有其他textview)内部,以查看是否显示它们.

Btw, try moving the last 3 TextViews (name, store_id and number) in your layout file INSIDE the second LinearLayout (together with all the other textviews), just to see if they are displayed.

这篇关于使用带有Extra的Intent启动活动不会显示所有Extras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆