0

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>

Then here's the listener code:

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

Here's the new activity code:

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

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!

4

3 回答 3

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 个 TextView(名称、store_id 和编号)移动到第二个 LinearLayout(连同所有其他 textview)中,以查看它们是否显示。

于 2013-12-01T22:53:29.287 回答
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" >  
于 2013-12-01T23:06:59.830 回答
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 占用了所有剩余的屏幕

于 2013-12-01T23:08:47.600 回答