在我的應用程序中,我有一個MainActivity包含許多可以單擊的項目(ListView)。如果點擊某個項目,我希望它打開下一個活動(例如NextActivity),該活動顯示與用戶在前一個屏幕上單擊了哪個項目相關的xml佈局。 這是我的完整MainActivity代碼:單擊ListView項目,根據點擊的項目打開顯示佈局的活動?
public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"Temaki Sushi", "Oyakodon", "Okonomiyaki", "Tofu Dango", "Oden", //5
"Nikujaga", "Yellowtail Teriyaki", "Tendon", "Tonjiru", "Sukiyaki", //10
};
private Integer[] releaseid = {
R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, //5
R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, //10
};
static final String[] description = new String[] {
"Variation of Sushis from fresh vegetables and seafoods! Good for family occassions!",
"Oyakodon is a Japanese Rice Bowl dish with Chicken, Eggs and various sorts of healthy and delicious Veggetables!",
"Japanese assorted Pancake that is made from many different ingredients. The taste is so delicious!",
"Japanese Dumplings made of Rice Flour. This one of the healthiest sweets in Japan!",
"Japanese assorted stews. Made from many different kind of veggetables. Popular in Winter!", //5
"Stew made of Beef and Veggetables. Very delicious and delightful! Yummy!",
"new era",
"new era",
"new era",
"new era", //10
};
private Integer[] difficultyid = {
R.drawable.medium, R.drawable.medium, R.drawable.medium, R.drawable.easy, R.drawable.hard, //5
R.drawable.medium, R.drawable.hard, R.drawable.medium, R.drawable.hard, R.drawable.hard, //10
};
private Integer[] timeid = {
R.drawable.fifteen, R.drawable.thirty, R.drawable.fifteen, R.drawable.fifteen, R.drawable.ninety, //5
R.drawable.fourtyfive, R.drawable.sixty, R.drawable.fourtyfive, R.drawable.fourtyfive, R.drawable.fifteen, //10
};
private Integer[] servesid = {
R.drawable.three,R.drawable.one, R.drawable.three, R.drawable.four,R.drawable.four, //5
R.drawable.one,R.drawable.two, R.drawable.one,R.drawable.one, R.drawable.two, //10
};
private Integer[] imgid = {
R.drawable.thumbtemaki, R.drawable.thumboyakodon, R.drawable.thumbokonomiyaki, R.drawable.thumbdango, R.drawable.thumboden, //5
R.drawable.thumbnikujaga, R.drawable.thumbyellowtail, R.drawable.thumbtendon, R.drawable.thumbtonjiru, R.drawable.thumbsukiyaki, //10
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.aboutapp:
startActivity(new Intent(this, AboutActivity.class));
return true;
case R.id.contact:
startActivity(new Intent(this, ContactActivity.class));
return true;
case R.id.screeninfo:
startActivity(new Intent(this, ScreenInfoActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
overridePendingTransition(R.anim.enter, R.anim.leave);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],i,description[i],i,i,i);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list, android.R.id.list, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
protected void onListItemClick(ListView listView, View view, int position, long id) {
switch(position)
{
case 0: Intent newActivity0 = new Intent(this, TemakiActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(this, OyakodonActivity.class);
startActivity(newActivity1);
break;
case 2: Intent newActivity2 = new Intent(this, OkonomiyakiActivity.class);
startActivity(newActivity2);
break;
case 3: Intent newActivity3 = new Intent(this, DangoActivity.class);
startActivity(newActivity3);
break;
case 4: Intent newActivity4 = new Intent(this, OdenActivity.class);
startActivity(newActivity4);
break;
case 5: Intent newActivity5 = new Intent(this, NikujagaActivity.class);
startActivity(newActivity5);
break;
case 6: Intent newActivity6 = new Intent(this, YellowtailActivity.class);
startActivity(newActivity6);
break;
case 7: Intent newActivity7 = new Intent(this, TendonActivity.class);
startActivity(newActivity7);
break;
case 8: Intent newActivity8 = new Intent(this, TonjiruActivity.class);
startActivity(newActivity8);
break;
case 9: Intent newActivity9 = new Intent(this, SukiyakiActivity.class);
startActivity(newActivity9);
break;
}
}
private class RowData {
protected int mId;
protected String mTitle;
protected int mRelease;
protected String mDescription;
protected int mDifficulty;
protected int mServes;
protected int mTime;
RowData(int id, String title, int release, String description, int difficulty, int serves, int time){
mId=id;
mTitle = title;
mRelease = release;
mDescription = description;
mDifficulty = difficulty;
mServes = serves;
mTime = time;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mRelease+" "+mDescription+" "+mDifficulty+" "+mServes+" "+mTime;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
ImageView release = null;
TextView description = null;
ImageView difficulty = null;
ImageView serves = null;
ImageView time = null;
ImageView thumbnail = null;
RowData rowData = getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
release = holder.getrelease();
release.setImageResource(releaseid[rowData.mRelease]);
description = holder.getdescription();
description.setText(rowData.mDescription);
difficulty = holder.getdifficulty();
difficulty.setImageResource(difficultyid[rowData.mDifficulty]);
serves = holder.getserves();
serves.setImageResource(servesid[rowData.mServes]);
time = holder.gettime();
time.setImageResource(timeid[rowData.mTime]);
thumbnail = holder.getImage();
thumbnail.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private ImageView release = null;
private TextView description = null;
private ImageView difficulty = null;
private ImageView serves = null;
private ImageView time = null;
private ImageView thumbnail = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.titleviewer);
}
return title;
}
public ImageView getrelease() {
if(null == release){
release = (ImageView) mRow.findViewById(R.id.releaseviewer);
}
return release;
}
public TextView getdescription() {
if(null == description){
description = (TextView) mRow.findViewById(R.id.descriptionviewer);
}
return description;
}
public ImageView getdifficulty() {
if(null == difficulty){
difficulty = (ImageView) mRow.findViewById(R.id.difficultyviewer);
}
return difficulty;
}
public ImageView getserves() {
if(null == serves){
serves = (ImageView) mRow.findViewById(R.id.servesviewer);
}
return serves;
}
public ImageView gettime() {
if(null == time){
time = (ImageView) mRow.findViewById(R.id.timeviewer);
}
return time;
}
public ImageView getImage() {
if(null == thumbnail){
thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
}
return thumbnail;
}
}
}
}
如何編寫代碼NextActivity顯示基於項目的佈局點擊?例如,如果項目0被點擊,setContentView(R.layout.item0),如果項目1被點擊,setContentView(R.layout.item1)等等?
看一看本作如何傳遞活動之間的數據 - HTTP:// developer.android.com/reference/android/app/Activity.html#StartingActivities。 – 2012-01-09 17:37:11
@PaulGrime對不起,我真的不明白如何從給出的鏈接實現代碼,你能幫我把代碼合併到我的代碼中嗎?對不起,仍然是小菜一碟! :( – borislemke 2012-01-09 17:42:41