我设法实现了一个很棒的列表视图,我在这里找到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();
}
});
}
答案 0 :(得分:52)
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();
}
});
答案 1 :(得分:8)
如果您的活动扩展了ListActivity,您可以简单地覆盖OnListItemClick()
方法,如下所示:
/** {@inheritDoc} */
@Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
答案 2 :(得分:1)
list.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 3 :(得分:0)
prestListView.getItemAtPosition(position);返回UI小部件:Text,Icon,...
请改为尝试:
Object o = prestationAdapterEco.getItemAtPosition(position);
或
Object o = arg0.getItemAtPosition(position);
从适配器获取对象。不是从列表视图。
2。 对象o是prestationEco对象。不是字符串。
答案 4 :(得分:0)
你在做什么
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
您获得的o
不是字符串,而是prestationEco
,因此您在执行(String)o
答案 5 :(得分:0)
试试这个:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});