0

我有两个类和一个 XML。第一个创建了一个 ListApter,其中充满了鸡尾酒名称,如血腥玛丽、玛格丽塔等。设置了第二个类,因此我可以更改 XML 中显示的 TextView 和 Image。我在尝试将我在 ListAdapter 中按下的项目传递给 CocktailDetail 类以更改 TextView 和 Image 的值时遇到了困难。有人可以帮忙吗?它会运行,但会为列表中选择的每个项目打开血腥玛丽。

菜单类

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","bloodymary");
        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);
            ImageView imageView1 = (ImageView)findViewById(R.id.imageCocktail);
            imageView1.setImageDrawable(getResources().getDrawable(R.drawable.bloodymary));
            Button b1 = (Button) findViewById(R.id.buttonYoutube);
            TextView t1 = (TextView)findViewById(R.id.textCocktailName);
            String cocktailName = "Bloody Mary";
            t1.setText(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:orientation="vertical"
    android:background="@drawable/beer"
    android:id="@+id/cocktailDetail" >

    <TextView
        android:id="@+id/textCocktailName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Cocktail Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageCocktail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/bloodymary" />

    <TextView
        android:textStyle="bold"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Ingredients"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textIngredients"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Insert txt here"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textStyle="bold" />

    <TextView
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="2dp"
        android:text="Preparation"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textPrepration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Insert txt here"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textStyle="bold" />

    <Button
        android:id="@+id/buttonYoutube"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="YouTube Clip"
        android:textSize="20dp" />

</LinearLayout>
4

2 回答 2

0

在 Menu 类中,您传递的是硬编码字符串:

   ourIntent.putExtra("cocktail_name",**"bloodymary"**);

将此更改为从 ListView 检索到的值:

    String selectedFromList =(String) (l.getItemAtPosition(position));
    ourIntent.putExtra("cocktail_name",selectedFromList);

同样在 Cocktail Class 中OnCreate,您再次将文本设置为硬编码字符串。使用Bundle,检索值,然后 setText;

  Bundle bundle = getIntent().getExtras();

 //Extract the data…
 String name = bundle.getString(“cocktail_name”); 
 TextView t1 = (TextView)findViewById(R.id.textCocktailName);
 t1.setText(name);
于 2013-04-18T13:13:08.693 回答
0

在菜单活动中试试这个:

// React to user clicks on item

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                         long id) {


     // here you get the Item selected using position

   Intent i = new Intent(MenuActivity.this, CocktailDetails.class);

   i.putExtra("title", your_title_cocktail);

   startActivity(i);
 }

});

在 DetailActivity

protected void onCreate(Bundle savedInstanceState) { 
 Intent i = getIntent(); 
---

}

看看这里这里

于 2013-04-18T13:14:57.920 回答