gpt4 book ai didi

java - 如何根据点击的item改变listview的内容?

转载 作者:行者123 更新时间:2023-11-30 11:35:09 24 4
gpt4 key购买 nike

有一个 ListView ,如果我单击 ListView 中的一个项目,它必须为在 ListView 中单击的所有项目转到相同的 Activity ,我需要的是相同的 Activity 必须根据他们的不同显示内容相应的点击项目。我对该 Activity 有疑问,如何为各自的点击创建那个 Activity 并获取他们的数据

 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;
}

}这是代码,如果我在 ListView 中单击该项目,它必须转到下一个 Activity 以及我将为项目写入数据的位置

 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>

最佳答案

您可以根据 ListView 项目的位置或名称更改日期。假设

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
});

祝你好运

关于java - 如何根据点击的item改变listview的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15265646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com