gpt4 book ai didi

android - 将 onclicklistener 添加到 listview (android)

转载 作者:IT老高 更新时间:2023-10-28 22:01:20 25 4
gpt4 key购买 nike

我已经成功实现了一个很棒的 ListView ,我在这里找到了 http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/但我似乎无法添加 onclicklistener我只想在单击该行时能够执行操作,当然该行包含的数据有任何想法吗?谢谢

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);

prestListView.setAdapter(prestationAdapterEco);

// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}

最佳答案

listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});

关于android - 将 onclicklistener 添加到 listview (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097723/

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