在ListAdapter上单击项目时更改TextView?

时间:2013-04-18 13:05:53

标签: android class android-manifest

我有两个类和一个XML。第一个创建一个ListApter,其中包含血腥玛丽,玛格丽塔等鸡尾酒名称。设置第二个类,以便我可以更改TextView和XML中显示的图像。我很难尝试将我在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);

    };
}

CocktailDetail类

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>

2 个答案:

答案 0 :(得分:0)

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

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

将其更改为从ListView检索的值:

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

同样在鸡尾酒班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);

答案 1 :(得分: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(); 
---

}

查看herehere