尝试在 onclick 函数中实现 listview

发布于 2024-12-17 19:47:14 字数 1265 浏览 0 评论 0原文

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);
listview.setOnItemLongClickListener(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2024-12-24 19:47:14

如果您希望使用父活动 onClick 方法,您的活动必须实现 OnItemClickListener 和 OnItemLongClickListener

public class CompanySearchActivity extends RathbonesActivity implements OnItemClickListener, OnItemLongClickListener 
{
     @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub

        }

}

请注意代码“implements OnItemClickListener, OnItemLongClickListener
这对于以这种方式实施至关重要。

然后你可以调用:

listview.setOnItemClickListener(CompanySearchActivity.this);
listview.setOnItemLongClickListener(CompanySearchActivity.this);

If you wish to use the parent activities onClick method your activity must implement OnItemClickListener and OnItemLongClickListener

public class CompanySearchActivity extends RathbonesActivity implements OnItemClickListener, OnItemLongClickListener 
{
     @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub

        }

}

Note the code "implements OnItemClickListener, OnItemLongClickListener"
This is vital for implementing it this way.

Then you can call:

listview.setOnItemClickListener(CompanySearchActivity.this);
listview.setOnItemLongClickListener(CompanySearchActivity.this);
很快妥协 2024-12-24 19:47:14

this 关键字是对拥有当前正在执行的方法的对象的引用。在本例中,this 指的是您定义的匿名View.OnClickListener 对象。尝试将 this 替换为 CompanySearchActivity.this

The this keyword is a reference to the object that owns the currently executing method. In this case this refers to the anonymous View.OnClickListener object that you are defining. Try replacing this with CompanySearchActivity.this

甜尕妞 2024-12-24 19:47:14

您的 listview onClick 定义应该类似于您的搜索侦听器。

    search.setOnClickListener(new View.OnClickListener() {






       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.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
<do somthing when its clicked>
        }
    });

还要确保您是否有多个布局,这些视图项目(列表视图和搜索)
位于 companysearch_layout.xml 中

your listview onClick definition should look like your search listener.

    search.setOnClickListener(new View.OnClickListener() {






       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.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
<do somthing when its clicked>
        }
    });

also ensure if you have multiple layouts that these view items (listview and search)
are in companysearch_layout.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文