尝试在 onclick 函数中实现 listview
public class CompanySearchActivity extends RathbonesActivity {
private CompanySearchAdapter mStockListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companysearch_layout);
final EditText keywordET = (EditText)findViewById(R.id.codeET);
final Button search = (Button)findViewById(R.id.button_stock_add);
final Activity a= CompanySearchActivity.this;
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
}
});
}
行 listview.setOnItemClickListener(this); listview.setOnItemLongClickListener(this);
由于此关键字而给出错误,我也将其替换为“a”,但它不起作用。实现这一目标的可能方法是什么?
public class CompanySearchActivity extends RathbonesActivity {
private CompanySearchAdapter mStockListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companysearch_layout);
final EditText keywordET = (EditText)findViewById(R.id.codeET);
final Button search = (Button)findViewById(R.id.button_stock_add);
final Activity a= CompanySearchActivity.this;
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
}
});
}
The lines listview.setOnItemClickListener(this);
are giving errors because of this keyword, i replaced it with 'a' too but it doesnt work. What can be the possible way to achieve this?
listview.setOnItemLongClickListener(this);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望使用父活动 onClick 方法,您的活动必须实现 OnItemClickListener 和 OnItemLongClickListener
请注意代码“
implements OnItemClickListener, OnItemLongClickListener
”这对于以这种方式实施至关重要。
然后你可以调用:
If you wish to use the parent activities onClick method your activity must implement OnItemClickListener and OnItemLongClickListener
Note the code "
implements OnItemClickListener, OnItemLongClickListener
"This is vital for implementing it this way.
Then you can call:
this
关键字是对拥有当前正在执行的方法的对象的引用。在本例中,this
指的是您定义的匿名View.OnClickListener
对象。尝试将this
替换为CompanySearchActivity.this
The
this
keyword is a reference to the object that owns the currently executing method. In this casethis
refers to the anonymousView.OnClickListener
object that you are defining. Try replacingthis
withCompanySearchActivity.this
您的
listview
onClick 定义应该类似于您的搜索侦听器。还要确保您是否有多个布局,这些视图项目(列表视图和搜索)
位于 companysearch_layout.xml 中
your
listview
onClick definition should look like your search listener.also ensure if you have multiple layouts that these view items (listview and search)
are in companysearch_layout.xml