gpt4 book ai didi

java - 如何在 ListView 中创建按钮,如果我单击它必须打开一个 Activity ?

转载 作者:行者123 更新时间:2023-12-01 14:47:34 27 4
gpt4 key购买 nike

我还有一个带有图像和链接功能的 ListView ,现在我需要的是我想在 ListView 中的每个项目中创建一个按钮,如果我单击按钮,它必须进入该 Activity 页面中的另一个 Activity 它必须分别单击按钮打开一个 xml 文件,我将预先在 ListView java 文件中硬编码 xml 文件的详细信息,如何做到这一点。

public class SouthIndianvegrecipes  extends Activity {

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

ArrayList<Recipedetails1> image_details = GetSearchResults();

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

lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
Recipedetails1 obj_itemDetails = (Recipedetails1)o;
Toast.makeText(SouthIndianvegrecipes.this, "You have chosen : " + " " + obj_itemDetails.getName1(), Toast.LENGTH_LONG).show();


}
});
}

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

Recipedetails1 item_details = new Recipedetails1();
item_details.setName1("Vegterian");
item_details.setItemDescription1("Recipes made by raw materials");
item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
item_details.setImageNumber1(1);
results.add(item_details);

item_details = new Recipedetails1();
item_details.setName1("Non-Vegterian");
item_details.setItemDescription1("Flesh of sweet animals");
item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
item_details.setImageNumber1(2);
results.add(item_details);

item_details = new Recipedetails1();
item_details.setName1("Pickels");
item_details.setItemDescription1("Touchable dish by Homemade");
item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
item_details.setImageNumber1(3);
results.add(item_details);

item_details = new Recipedetails1();
item_details.setName1("Soups");
item_details.setItemDescription1("Startup for our food");
item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
item_details.setImageNumber1(4);
results.add(item_details);




return results;
}

}

最佳答案

您只需在 ItemListBaseAdapter1 的 getView() 方法中为按钮设置 OnClickListener 即可,如下所示。

    class ItemListBaseAdapter1 extends BaseAdapter
{

private Activity mActivity;
public ItemListBaseAdapter1(Activity activity, ...)
{
mActivity = activity;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO: get view here
final Recipedetails1 item = (Recipedetails1) getItem(position);
theButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
Intent intent = new Intent(mActivity, [your activity class]);
intent.putExtra("The Link Key", item.getUrlWiki());
mActivity.startActivity(intent);
}
});
return view;
}

}

在目标 Activity 上,您可以通过以下方式获得链接:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String link = getIntent.getString("The Link Key", null);
}

关于java - 如何在 ListView 中创建按钮,如果我单击它必须打开一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262602/

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