所以我的问题是我目前正在尝试在从列表视图中选择项目时动态更改 ImageView。它给了我一个"cocktailName cannot be resolved or is not a field"
错误
CocktailMenu 类
package com.drunktxtapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.app.ListActivity;
public class CocktailMenu extends Activity {
String classes[] = {"Bloody_Mary", "Capirinha", "Cosmopolitan", "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita", "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada", "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour", "White_Russian"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cocktail_menu);
Button b1 = (Button) findViewById(R.id.bByList);
Button b2 = (Button) findViewById(R.id.bRandomCocktail);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent ourIntent = new Intent(CocktailMenu.this, Menu.class);
ourIntent.putExtra("cocktail_name", classes);
startActivity(ourIntent);
}
}
);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int random = (int) (Math.random() * classes.length);
Intent i = new Intent(CocktailMenu.this, CocktailDetail.class);
i.putExtra("cocktail_name", classes[random]);
startActivity(i);
}
}
);
}
}
菜单类
package com.drunktxtapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[] = { "Bloody_Mary", "Capirinha", "Cosmopolitan",
"Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita",
"Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada",
"Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour",
"White_Russian" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,
android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
;
Intent ourIntent = new Intent(Menu.this, CocktailDetail.class);
ourIntent.putExtra("cocktail_name",classes);
startActivity(ourIntent);
};
}
鸡尾酒细节课
package com.drunktxtapp;
import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class CocktailDetail extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cocktaildetail);
Button b1 = (Button) findViewById(R.id.buttonYoutube);
String cocktailName = getIntent().getStringExtra("cocktail_name");
TextView t1 = (TextView)findViewById(R.id.textCocktailName);
t1.setText(cocktailName);
ImageView imageView1 = (ImageView)findViewById(R.id.imageCocktail);
//imageView1.setImageResource(R.drawable.cocktailName);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=Alt-ehDc3fc")));
}
});
}
}
鸡尾酒菜单 XML
<?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:background="@drawable/beer"
android:orientation="vertical" >
<Button
android:id="@+id/bByList"
android:layout_width="250dp"
android:layout_height="125dp"
android:layout_gravity="center"
android:layout_marginBottom="100dp"
android:layout_marginTop="25dp"
android:padding="25dp"
android:text="Cocktail List"
android:textSize="20dp" />
<Button
android:id="@+id/bRandomCocktail"
android:layout_width="250dp"
android:layout_height="125dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:padding="2dp"
android:text="Random Cocktail"
android:textSize="20dp" />
</LinearLayout>