如何在不扩展ListActivity的情况下处理ListView的onClick?

发布于 2024-12-11 21:38:54 字数 609 浏览 0 评论 0原文

我有一个列表视图,填充如下并且它正在工作。我的问题是能够处理 onclick。我没有扩展列表活动,因为我的所有活动都扩展了一个通用活动,我用它来设置所有活动中使用的通用变量/首选项/等。有没有办法在不扩展 listactivity 的情况下处理 onclick ?如果没有,有没有办法导入列表活动(通过类或其他东西)?

    ListView lv = (ListView)findViewById(R.id.listViewGhostCams);
    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_row_view,
            new String[] {"pen","price","color"},
            new int[] {R.id.text1,R.id.text2, R.id.text3}
            );
    populateList();
    lv.setAdapter(adapter);

我是 android / java 编程新手,所以请温柔一点! :-)

谢谢, 香农

I have a listview populated as follows and it's working. My issue is with being able to handle the onclick. I'm not extending the listactivity because all my activities extend a common activity that I use to setup common variables/preferences/etc that are used on all activities. Is there a way to handle the onclick without extending the listactivity? and if not, is there a way to import the listactivity (via a class or something)?

    ListView lv = (ListView)findViewById(R.id.listViewGhostCams);
    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_row_view,
            new String[] {"pen","price","color"},
            new int[] {R.id.text1,R.id.text2, R.id.text3}
            );
    populateList();
    lv.setAdapter(adapter);

I'm new to android / java programming so please be gentle! :-)

Thanks,
Shannon

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

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

发布评论

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

评论(3

獨角戲 2024-12-18 21:38:54

您可以使用 OnItemClickListener 来执行此操作

    lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            //Your code
        }

    });

You can do this with OnItemClickListener

    lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            //Your code
        }

    });
皓月长歌 2024-12-18 21:38:54

我猜你的意思是 onClick on an item?只需使用这一点:

lv.setOnItemClickListener(new OnItemClickListener() {

    @覆盖             
    public void onItemClick(AdapterViewparent, View
    视图,int 位置,长 id) { 
    }
});

I guess you mean onClick on an item? Just use this bit:

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override             
    public void onItemClick(AdapterView<?> parent, View
    view, int position, long id) { 
    }
});
童话 2024-12-18 21:38:54

您可以使用 onItemClickListener

 public class MainActivity extends Activity {
     ListView listView;
     String[] listPlanet={"mercury","Venus","Mars","Saturn","Neptune"};

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

         listView = (ListView)findViewById(R.id.listView));

         ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,         listPlanet);

         listview.setAdapter(adapter);

         listView.setOnItemClickListener(new OnItemClickListener(){

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)      {
                Toast.makeText(getAppicationContext(), listPlanet.get(arg2), Toast.LENGTH_SHORT).show();

       }

    });

    }

 }

you can user onItemClickListener

 public class MainActivity extends Activity {
     ListView listView;
     String[] listPlanet={"mercury","Venus","Mars","Saturn","Neptune"};

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

         listView = (ListView)findViewById(R.id.listView));

         ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,         listPlanet);

         listview.setAdapter(adapter);

         listView.setOnItemClickListener(new OnItemClickListener(){

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)      {
                Toast.makeText(getAppicationContext(), listPlanet.get(arg2), Toast.LENGTH_SHORT).show();

       }

    });

    }

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