我正在尝试为ListView中的特定项目创建onItemClickListener。 使用此代码,Listener会对每个项目做出反应,但我只希望它对列表中的一个项目做出反应(字体大小)。
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();
}
});
我如何实现这一目标?
提前致谢
答案 0 :(得分:1)
试试这个:
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();
}
});