gpt4 book ai didi

java - ListView 中特定项目的 OnClick

转载 作者:行者123 更新时间:2023-12-01 13:59:03 26 4
gpt4 key购买 nike

我正在尝试为 ListView 中的特定项目创建一个 onItemClickListener。使用此代码,监听器会对每个项目使用react,但我只希望它对列表中的一项(字体大小)使用react。

String[] listItems = {"Colour", "Font Size",};
ListView lv = (ListView) findViewById(R.id.settings_list);

lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));

lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Context context = getApplicationContext();
CharSequence text = "Clicked";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});

我如何实现这一目标?

提前致谢

最佳答案

试试这个:

 String[] listItems = {"Colour", "Font Size",};
ListView lv = (ListView) findViewById(R.id.settings_list);

lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));

lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(listItems[position].equlas("Font Size"))
{
Context context = getApplicationContext();
CharSequence text = "Clicked";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});

关于java - ListView 中特定项目的 OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454495/

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