如何根据点击的项目更改列表视图的内容?

时间:2013-03-07 07:41:26

标签: java android

拥有一个列表视图,如果我点击列表视图中的一个项目,它必须转到相同的活动,在列表视图中点击所有项目,我需要的是同一个活动必须根据他们的内容显示不同的内容各自的点击项目。 我在该活动中怀疑如何为相应的点击创建那个并获取他们的数据

 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);     
                                startActivity(newActivity);
                    /*switch(obj_itemDetails.getImageNumber())
                    {
                        case 1:
                             Intent newActivity = new Intent(SouthIndianvegrecipes1.this, 
                            SouthIndianvegrecipes.class);     
                            startActivity(newActivity);
                            break;

                        case 2:
                            Intent new1Activity = new Intent(SouthIndianvegrecipes1.this, 
                                    SouthIndiannonvegrecipes.class);     
                            startActivity(new1Activity);
                            break;

                        case 3:
                            Intent new2Activity = new Intent(SouthIndianvegrecipes1.this, 
                                    SouthIndianpickelsrecipes.class);     
                            startActivity(new2Activity);
                            break; 

                        case 4:
                            Intent new3Activity = new Intent(SouthIndianvegrecipes1.this, 
                                    SouthIndiansoupsrecipes.class);     
                            startActivity(new3Activity);
                            break;      

                       default:
                           Toast.makeText(SouthIndianvegrecipes1.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }*/
                     /*if(obj_itemDetails.getName().equals("Vegeterain"))
                     {

                        Intent newActivity = new Intent(SouthIndian.this, 
                                                     SouthIndianvegrecipes.class);     
                        startActivity(newActivity);
                     }

                     else
                       {
                        Intent new1Activity = new Intent(SouthIndian.this, 
                                SouthIndiannonvegrecipes.class);     
                        startActivity(new1Activity);

                     //same for other Activity
                  } */
                } 
            });
        }

        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("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 {

         private Object getIntent;

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

                ArrayList<Recipedetails2> image_details = GetSearchResults();

                final ListView lv1 = (ListView) findViewById(R.id.listV_main);
                lv1.setAdapter(new ItemListBaseAdapter2(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);     
                                    startActivity(newActivity);
                        /*switch(obj_itemDetails.getImageNumber())
                        {
                            case 1:
                                 Intent newActivity = new Intent(SouthIndianvegrecipes1.this, 
                               SouthIndianvegrecipes.class);     
                               startActivity(newActivity);
                                break;

                            case 2:
                                Intent new1Activity = new Intent(SouthIndianvegrecipes1.this, 
                                        SouthIndiannonvegrecipes.class);     
                                startActivity(new1Activity);
                                break;

                            case 3:
                                Intent new2Activity = new Intent(SouthIndianvegrecipes1.this, 
                                        SouthIndianpickelsrecipes.class);     
                                startActivity(new2Activity);
                                break; 

                            case 4:
                                Intent new3Activity = new Intent(SouthIndianvegrecipes1.this, 
                                        SouthIndiansoupsrecipes.class);     
                                startActivity(new3Activity);
                                break;      

                           default:
                               Toast.makeText(SouthIndianvegrecipes1.this, "Wrong Input", Toast.LENGTH_LONG).show();
                        }*/
                         /*if(obj_itemDetails.getName().equals("Vegeterain"))
                         {

                            Intent newActivity = new Intent(SouthIndian.this, 
                                                         SouthIndianvegrecipes.class);     
                            startActivity(newActivity);
                         }

                         else
                           {
                            Intent new1Activity = new Intent(SouthIndian.this, 
                                    SouthIndiannonvegrecipes.class);     
                            startActivity(new1Activity);

                         //same for other Activity
                      } */
                    //} 
                //});
            }
        Bundle extras = getIntent().getExtras();
        int[] obj_itemDetails.getImageNumber() = extras.getIntArray("numbers");

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

                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.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(12);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Soyamore koozh");
                item_details.setIngredients("Tasty to have this as main dish");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(13);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Tomato kulambu");
                item_details.setIngredients("Good side dish for rice and dinner");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(14);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Tomato upma");
                item_details.setIngredients("Evening time  dinner for kids");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(15);
                results.add(item_details);


                item_details = new Recipedetails2();
                item_details.setName("Tomato upma");
                item_details.setIngredients("Evening time  dinner for kids");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(15);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Navratri special vada");
                item_details.setIngredients("Good snacks at the function");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(16);
                results.add(item_details);


                item_details = new Recipedetails2();
                item_details.setName("Egg kurma");
                item_details.setIngredients("Wonderful dish made by egg");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(17);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Milagu kuzhambu");
                item_details.setIngredients("Good for digestion purpose");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(18);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Rasam");
                item_details.setIngredients("Last and famous dish in lunch,good for health also");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(19);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Vegetable kootu");
                item_details.setIngredients("Nice and healthy one");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(20);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("Avial");
                item_details.setIngredients("Easy to prepare and lots of vegetable");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(21);
                results.add(item_details);

                item_details = new Recipedetails2();
                item_details.setName("kothavarangaicurry");
                item_details.setIngredients("Tamilnadu special and healthy too");
                item_details.setMethods("This is very good for dibets people");
                //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
                item_details.setImageNumber(22);
                results.add(item_details);


                return results;
            }

    }

这里我将如何编写用于获取数据的代码 我的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/itemphoto"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="top"
    android:padding="20dp"
    android:scaleType="center"
  ></ImageView>

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

您可以根据ListView item.Suppose

的位置或名称更改日期
String name[]={"a","b","c","d"};
lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                   String current=name[position];

//now put condition here to start different activity

if(current.equals("a"))
{
//do operation what u have to do for a
}else if(current.equals("b"))
{
//do operation what u have to do for b
}
similarly put here condition for all
        });

祝你好运