单击列表视图时在 activity 中显示不同的信息
Display different information in activity when listview is clicked
我希望列表视图打开一个新的 activity 并在列表视图的特定行中单击时显示特定信息。
到目前为止,我只能在点击每一行时显示相同的信息。我不知道如何为特定行设置这些信息,例如,当我点击第 1 行时会显示该地址,当我点击第 2 行时会显示另一个地址......我的代码如下
第一个activity
// storing string resources into Array
String []Buildings_halls = getResources().getStringArray(R.array.halls);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.hallstudent, R.id.listhall, Buildings_halls));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
Bundle bundle = new Bundle();
bundle.putInt("position", position);
// Or / And
intent.putExtra("id", String.valueOf(id));
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription));
intent.putExtras(bundle);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
秒activity
Bundle intent = getIntent().getExtras();
if (intent !=null){
int position = intent.getInt("position", 0);
String[] halldetails = getResources().getStringArray(R.array.hallsdescription);
txt = (TextView) findViewById (R.id.select_details);
txt.setText(halldetails[1]);
txt2=(TextView)findViewById(R.id.details_select);
txt2.setText(halldetails[0]);
// Here we turn your string.xml in an array
String[] myKeys = getResources().getStringArray(R.array.halls);
TextView myTextView = (TextView) findViewById(R.id.selecthalllist);
myTextView.setText(myKeys[position]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
即我要显示的第 1 行的信息
<string-array name="descriptions">
<item>Address:</item>
<item> Contact: </item>
<item>OpeningHours:</item>
</string-array>
即我要显示的第 1 行的信息
<string-array name="hallsdescription">
<item >Address:, Telephone No:,Opening Times:</item>
<item >Address:Catherine Street, Telephone No:657263912739,Opening Times:23:00</item>
谢谢
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
switch(position){
case 0:
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[0]);
break;
case 1:
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[1]);
break;
}
startActivity(intent);
}
在你的第二个 activity,
txt.setText(getIntent().getExtras().getString("description"));
我希望列表视图打开一个新的 activity 并在列表视图的特定行中单击时显示特定信息。 到目前为止,我只能在点击每一行时显示相同的信息。我不知道如何为特定行设置这些信息,例如,当我点击第 1 行时会显示该地址,当我点击第 2 行时会显示另一个地址......我的代码如下
第一个activity
// storing string resources into Array
String []Buildings_halls = getResources().getStringArray(R.array.halls);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.hallstudent, R.id.listhall, Buildings_halls));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
Bundle bundle = new Bundle();
bundle.putInt("position", position);
// Or / And
intent.putExtra("id", String.valueOf(id));
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription));
intent.putExtras(bundle);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
秒activity
Bundle intent = getIntent().getExtras();
if (intent !=null){
int position = intent.getInt("position", 0);
String[] halldetails = getResources().getStringArray(R.array.hallsdescription);
txt = (TextView) findViewById (R.id.select_details);
txt.setText(halldetails[1]);
txt2=(TextView)findViewById(R.id.details_select);
txt2.setText(halldetails[0]);
// Here we turn your string.xml in an array
String[] myKeys = getResources().getStringArray(R.array.halls);
TextView myTextView = (TextView) findViewById(R.id.selecthalllist);
myTextView.setText(myKeys[position]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
即我要显示的第 1 行的信息
<string-array name="descriptions">
<item>Address:</item>
<item> Contact: </item>
<item>OpeningHours:</item>
</string-array>
即我要显示的第 1 行的信息
<string-array name="hallsdescription">
<item >Address:, Telephone No:,Opening Times:</item>
<item >Address:Catherine Street, Telephone No:657263912739,Opening Times:23:00</item>
谢谢
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
switch(position){
case 0:
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[0]);
break;
case 1:
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[1]);
break;
}
startActivity(intent);
}
在你的第二个 activity,
txt.setText(getIntent().getExtras().getString("description"));