使用intent with extra启动活动并不显示所有额外内容

时间:2013-12-01 22:30:55

标签: android android-intent extras

在setOnItemClickListener中,我使用intent来启动一个新活动。为了这个意图,我把7个字符串作为额外内容。当我点击(ListView的)项目时,我想要显示所有这些额外内容(在新屏幕上)。但我只看到前4个额外的东西。这是新活动的布局文件:

<?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和电话号码。他们会怎么样?为什么他们没有在新屏幕上显示/看到?实际上,在我添加更多额外内容之前,我可以在我的意图中看到所有额外内容。额外人数有问题吗?如果你有答案,谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

您可以简化添加额外内容 - 无需使用额外字符作为键:

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中:

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,以防您的布局不正确?在您检索附加内容的行下方添加此项:

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

看看你是否可以在logcat中看到它。如果是,可能您的布局没有显示所有这些TextView。

顺便说一句,尝试在布局文件中移动最后3个TextViews(name,store_id和number)INSIDE第二个LinearLayout(以及所有其他textview),只是为了看它们是否显示。

答案 1 :(得分:1)

您的内部水平LinearLayout的高度设置为match_parent

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

因此,它会延伸到屏幕的底部,并且不会为其他三个TextView留下任何空间。因此,应将其设置为wrap_content

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >  

答案 2 :(得分:1)

您的布局错误

<?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" >

...

你应该

<?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="wrap_content"
    android:orientation="horizontal" >

...

因为您的第二个LinearLayout正在拍摄所有剩余的屏幕