添加一个onclicklistener到列表视图(android)
我设法实现了一个伟大的列表视图,我发现这里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(); } });
如果您的Activity扩展了ListActivity,那么您可以简单地重写OnListItemClick()
方法,如下所示:
/** {@inheritDoc} */ @Override protected void onListItemClick(ListView l, View v, int pos, long id) { super.onListItemClick(l, v, pos, id); // TODO : Logic }
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 } });
prestListView.getItemAtPosition(position); 返回UI小部件:Text,Icon,…
试试这个:
Object o = prestationAdapterEco.getItemAtPosition(position);
要么
Object o = arg0.getItemAtPosition(position);
从适配器获取对象。 不是从列表视图。
2.对象o是prestationEco对象。 不是一个string。
你在做
Object o = prestListView.getItemAtPosition(position); String str=(String)o;//As you are using Default String Adapter
你得到的o
不是一个string,而是一个prestationEco
所以你在做(String)o
时候得到一个CCE
尝试这个:
list.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) } @Override public void onNothingSelected(AdapterView<?> arg0) { } });