单击列表视图中的项目后如何传递字符串并获取其数据,它必须根据点击项目获取各自的数据

时间:2013-03-08 08:38:45

标签: java android

我有一个列表视图,在列表视图中,如果单击一个项目,它必须选择各自的数据并显示它,如何将数据作为一个标记传递以及如何获取该数据?对我来说,我传递了name参数,它必须比较名称并获取数据以显示它。

如何在FirstActivity.java中传递字符串?

public class SouthIndianvegrecipes1  extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<Recipedetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                Recipedetails obj_itemDetails = (Recipedetails)o;
                Toast.makeText(SouthIndianvegrecipes1.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
                 Intent newActivity = new Intent(SouthIndianvegrecipes1.this, 
                            SouthIndianvegmanualrecipes.class );  
                newActivity.putExtra("name", obj_itemDetails.getName());
                            startActivity(newActivity);

            } 
        });
    }

    private ArrayList<Recipedetails> GetSearchResults(){
        ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();

        Recipedetails item_details = new Recipedetails();
        item_details.setName("Godhumai veg adai");
        item_details.setItemDescription("This is very good for dibets people");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(12);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Soyamore koozh");
        item_details.setItemDescription("Tasty to have this as main dish");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(13);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Tomato kulambu");
        item_details.setItemDescription("Good side dish for rice and dinner");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(14);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Tomato upma");
        item_details.setItemDescription("Evening time  dinner for kids");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(15);
        results.add(item_details);




        item_details = new Recipedetails();
        item_details.setName("Navratri special vada");
        item_details.setItemDescription("Good snacks at the function");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(16);

        results.add(item_details);


        item_details = new Recipedetails();
        item_details.setName("Egg kurma");
        item_details.setItemDescription("Wonderful dish made by egg");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(17);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Milagu kuzhambu");
        item_details.setItemDescription("Good for digestion purpose");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(18);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Rasam");
        item_details.setItemDescription("Last and famous dish in lunch,good for health also");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(19);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Vegetable kootu");
        item_details.setItemDescription("Nice and healthy one");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(20);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Avial");
        item_details.setItemDescription("Easy to prepare and lots of vegetable");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(21);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("kothavarangaicurry");
        item_details.setItemDescription("Tamilnadu special and healthy too");
        //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber(22);
        results.add(item_details);


        return results;
    }

}

第二项活动

public class SouthIndianvegmanualrecipes  extends Activity {

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recipe_manual);
            Intent intent = getIntent();
            String value = intent.getStringExtra("name");


           if(value.equals("Godhumai veg adai"))
        {
            Recipedetails2 item_details = new Recipedetails2();
            item_details.setName("Godhumai veg adai");
            item_details.setIngredients("This is very good for dibets people");
            item_details.setMethods("This is very good for dibets people");

        }
        else if(value.equals("Soyamore koozh"))
        {
            Recipedetails2 item_detail1 = new Recipedetails2();
            item_detail1.setName("Soyamore koozh");
            item_detail1.setIngredients("Tasty to have this as main dish");
            item_detail1.setMethods("This is very good for dibets people");
        }
        else if(value.equals("Tomato kulambu"))
        {
            Recipedetails2 item_detail3 = new Recipedetails2();
            item_detail3.setName("Tomato kulambu");
            item_detail3.setIngredients("Good side dish for rice and dinner");
            item_detail3.setMethods("This is very good for dibets people");

        }
        else if(value.equals("Navratri special vada"))
        {
            Recipedetails2 item_detail5 = new Recipedetails2();
            item_detail5.setName("Navratri special vada");
            item_detail5.setIngredients("Good snacks at the function");
            item_detail5.setMethods("This is very good for dibets people");

        }
        else if(value.equals("Egg kurma"))
        {
            Recipedetails2 item_detail6 = new Recipedetails2();
            item_detail6.setName("Egg kurma");
            item_detail6.setIngredients("Wonderful dish made by egg");
            item_detail6.setMethods("This is very good for dibets people");

        }
        else if(value.equals("Milagu kuzhambu"))
        {
            Recipedetails2 item_detail7 = new Recipedetails2();
            item_detail7.setName("Milagu kuzhambu");
            item_detail7.setIngredients("Good for digestion purpose");
            item_detail7.setMethods("This is very good for dibets people");

        }

        else if(value.equals("Rasam"))
        {
            Recipedetails2 item_detail8 = new Recipedetails2();
            item_detail8.setName("Rasam");
            item_detail8.setIngredients("Last and famous dish in lunch,good for health also");
            item_detail8.setMethods("This is very good for dibets people");

        }

        else if(value.equals("Vegetable kootu"))
        {
            Recipedetails2 item_detail9 = new Recipedetails2();
            item_detail9.setName("Vegetable kootu");
            item_detail9.setIngredients("Nice and healthy one");
            item_detail9.setMethods("This is very good for dibets people");

        }

        else if(value.equals("Avial"))
        {
            Recipedetails2 item_detail10 = new Recipedetails2();
            item_detail10.setName("Avial");
            item_detail10.setIngredients("Easy to prepare and lots of vegetable");
            item_detail10.setMethods("This is very good for dibets people");

        }

        else 
        {
            Recipedetails2 item_detail11 = new Recipedetails2();
            item_detail11.setName("kothavarangaicurry");
            item_detail11.setIngredients("Tamilnadu special and healthy too");
            item_detail11.setMethods("This is very good for dibets people");

        }



        }
    }

这里我需要根据我的布局放置或存储字符串功能来放置他们的名字和图像以及成分和方法,在if函数后我输错了代码, layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"

  android:orientation="vertical"

  >

  <ImageView
    android:id="@+id/photo"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="top"
    android:padding="20dp"
    android:scaleType="center"
  ></ImageView>

  <TextView android:id="@+id/name"
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:textColor="#32cd32" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/photo"/>

  <TextView
      android:text="Ingredients:"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="left" />

  <TextView android:id="@+id/itemIngredients"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:scaleType="center" />

   <TextView
      android:text="Method:"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="left" />

  <TextView android:id="@+id/methods"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:scaleType="center" />

</LinearLayout>

如何传递字符串以及如何比较,提取和显示它?或者你能给出一些如何做到这一点的链接吗?

我的代码是否正在进行比较,为什么它不显示数据,只是默认布局

1 个答案:

答案 0 :(得分:0)

朋友这是正确的代码

 public class SouthIndianvegmanualrecipes  extends Activity {
TextView itemName,itemIngredients,methods;
ImageView itemImage;
String value;


private Integer[] imgid = {
        R.drawable.vegeterian,
        R.drawable.nonveg,
        R.drawable.pickels,
        R.drawable.soup,
        R.drawable.sweets,
        R.drawable.cakes,
        R.drawable.icecreams,
        R.drawable.chinesevegdishes,
        R.drawable.chinesenonveg,
        R.drawable.chinesenoodles,
        R.drawable.chinesesoup,
        R.drawable.godhumaivegadai,
        R.drawable.soyamorekoozh,
        R.drawable.tomatokulambu,
        R.drawable.tomatoupma,
        R.drawable.vadanavratrispecial,
        R.drawable.eggkurma,
        R.drawable.milagu,
        R.drawable.rasam,
        R.drawable.vegetablekootu,
        R.drawable.avial,
        R.drawable.kothavarangaicurry,
        };



    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recipe_manual);
            Intent intent = getIntent();
             value = intent.getStringExtra("name");
                itemName = (TextView) findViewById(R.id.name);
                itemIngredients = (TextView) findViewById(R.id.itemIngredients);
                methods = (TextView) findViewById(R.id.methods);
                itemImage = (ImageView) findViewById(R.id.photo);

           if(value.equals("Godhumai veg adai"))
        {
            Recipedetails2 item_details = new Recipedetails2();
            item_details.setName("Godhumai veg adai");
            item_details.setIngredients("This is very good for dibets people");
            item_details.setMethods("This is very good for dibets people");
            item_details.setImageNumber(12);
            setUIScreen(item_details.getName(),item_details.getIngredients(),item_details.getMethods(),item_details.getImageNumber() );
        }
        else if(value.equals("Soyamore koozh"))
        {
            Recipedetails2 item_detail1 = new Recipedetails2();
            item_detail1.setName("Soyamore koozh");
            item_detail1.setIngredients("Tasty to have this as main dish");
            item_detail1.setMethods("This is very good for dibets people");
            item_detail1.setImageNumber(13);
            setUIScreen(item_detail1.getName(),item_detail1.getIngredients(),item_detail1.getMethods(),item_detail1.getImageNumber() );
        }
        else if(value.equals("Tomato kulambu"))
        {
            Recipedetails2 item_detail3 = new Recipedetails2();
            item_detail3.setName("Tomato kulambu");
            item_detail3.setIngredients("Good side dish for rice and dinner");
            item_detail3.setMethods("This is very good for dibets people");
            item_detail3.setImageNumber(14);
            setUIScreen(item_detail3.getName(),item_detail3.getIngredients(),item_detail3.getMethods(),item_detail3.getImageNumber() );

        }
        else if(value.equals("Tomato upma"))
        {
            Recipedetails2 item_detail4 = new Recipedetails2();
            item_detail4.setName("Tomato upma");
            item_detail4.setIngredients("Good side dish for rice and dinner");
            item_detail4.setMethods("This is very good for dibets people");
            item_detail4.setImageNumber(14);
            setUIScreen(item_detail4.getName(),item_detail4.getIngredients(),item_detail4.getMethods(),item_detail4.getImageNumber() );

        }

        else if(value.equals("Navratri special vada"))
        {
            Recipedetails2 item_detail5 = new Recipedetails2();
            item_detail5.setName("Navratri special vada");
            item_detail5.setIngredients("Good snacks at the function");
            item_detail5.setMethods("This is very good for dibets people");
            item_detail5.setImageNumber(15);
            setUIScreen(item_detail5.getName(),item_detail5.getIngredients(),item_detail5.getMethods(),item_detail5.getImageNumber() );
        }
        else if(value.equals("Egg kurma"))
        {
            Recipedetails2 item_detail6 = new Recipedetails2();
            item_detail6.setName("Egg kurma");
            item_detail6.setIngredients("Wonderful dish made by egg");
            item_detail6.setMethods("This is very good for dibets people");
            item_detail6.setImageNumber(16);
            setUIScreen(item_detail6.getName(),item_detail6.getIngredients(),item_detail6.getMethods(),item_detail6.getImageNumber() );
        }
        else if(value.equals("Milagu kuzhambu"))
        {
            Recipedetails2 item_detail7 = new Recipedetails2();
            item_detail7.setName("Milagu kuzhambu");
            item_detail7.setIngredients("Good for digestion purpose");
            item_detail7.setMethods("This is very good for dibets people");
            item_detail7.setImageNumber(17);
            setUIScreen(item_detail7.getName(),item_detail7.getIngredients(),item_detail7.getMethods(),item_detail7.getImageNumber() );
        }

        else if(value.equals("Rasam"))
        {
            Recipedetails2 item_detail8 = new Recipedetails2();
            item_detail8.setName("Rasam");
            item_detail8.setIngredients("Last and famous dish in lunch,good for health also");
            item_detail8.setMethods("This is very good for dibets people");
            item_detail8.setImageNumber(18);
            setUIScreen(item_detail8.getName(),item_detail8.getIngredients(),item_detail8.getMethods(),item_detail8.getImageNumber() );
        }

        else if(value.equals("Vegetable kootu"))
        {
            Recipedetails2 item_detail9 = new Recipedetails2();
            item_detail9.setName("Vegetable kootu");
            item_detail9.setIngredients("Nice and healthy one");
            item_detail9.setMethods("This is very good for dibets people");
            item_detail9.setImageNumber(19);
            setUIScreen(item_detail9.getName(),item_detail9.getIngredients(),item_detail9.getMethods(),item_detail9.getImageNumber() );
        }

        else if(value.equals("Avial"))
        {
            Recipedetails2 item_detail10 = new Recipedetails2();
            item_detail10.setName("Avial");
            item_detail10.setIngredients("Easy to prepare and lots of vegetable");
            item_detail10.setMethods("This is very good for dibets people");
            item_detail10.setImageNumber(20);
            setUIScreen(item_detail10.getName(),item_detail10.getIngredients(),item_detail10.getMethods(),item_detail10.getImageNumber() );
        }

        else
        {
            Recipedetails2 item_detail11 = new Recipedetails2();
            item_detail11.setName("kothavarangaicurry");
            item_detail11.setIngredients("Tamilnadu special and healthy too");
            item_detail11.setMethods("This is very good for dibets people");
            item_detail11.setImageNumber(21);
            setUIScreen(item_detail11.getName(),item_detail11.getIngredients(),item_detail11.getMethods(),item_detail11.getImageNumber() );
        }

        }

public void setUIScreen(String i1, String i2, String i3, int n1){
itemName.setText(i1);
 itemIngredients.setText(i2);
 methods.setText(i3);
 itemImage.setImageResource(imgid[n1 - 1]);
}

}