问题
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!
回答1:
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);
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"));
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"));
and see if you can see it in logcat. If yes, maybe your layout is not showing all those TextViews.
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.
回答2:
Your inner horizontal LinearLayout's height is set to match_parent
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
Thus, it stretches to the bottom of the screen and leaves no place for your other three TextViews. Therefore it should be set to wrap_content
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
回答3:
Your layout is wrong
<?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" >
...
You should have
<?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" >
...
Because your second LinearLayout is taking all the remaining screen
来源:https://stackoverflow.com/questions/20318089/start-activity-using-intent-with-extra-does-not-show-all-the-extras