尝试从android中列表视图点击事件中的文本视图中获取值

发布于 2024-09-10 05:01:37 字数 1296 浏览 4 评论 0原文

我有一个点击事件连接到我的列表视图,如图所示。

int[] GenusListIDs = { R.id.txt_ID, R.id.txt_Genus, R.id.txt_Count };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.genusitem, cursor_genuslist, service.GenusListColumns, GenusListIDs);

        ListView list_Genus = (ListView)findViewById(R.id.list_Genus);
        list_Genus.setAdapter(adapter);
        list_Genus.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id)
            {
                try
                {
                    Log.println(1, "ItemClick", view.toString());
                    TextView tv = (TextView)view;
                    String genus = (String) tv.getText();
                    Intent i = new Intent(getBaseContext(), Cat_Species.class);//new Intent(this, Total.class);
                    /*view
                    i.putExtra("id", id);*/
                    startActivity(i);
                }
                catch(Exception ex)
                {
                    Log.println(1, "item-click-event", ex.getMessage());
                }
            }
        });

我需要根据他们单击的列表项将字符串参数传递给新意图。我想要传递的值位于名为 txt_Genus 的列表项中。如何从列表项中获取该值以传递给意图?请不要关注我的实验,哈哈。

I have a click event hooked up to my listview as shown.

int[] GenusListIDs = { R.id.txt_ID, R.id.txt_Genus, R.id.txt_Count };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.genusitem, cursor_genuslist, service.GenusListColumns, GenusListIDs);

        ListView list_Genus = (ListView)findViewById(R.id.list_Genus);
        list_Genus.setAdapter(adapter);
        list_Genus.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id)
            {
                try
                {
                    Log.println(1, "ItemClick", view.toString());
                    TextView tv = (TextView)view;
                    String genus = (String) tv.getText();
                    Intent i = new Intent(getBaseContext(), Cat_Species.class);//new Intent(this, Total.class);
                    /*view
                    i.putExtra("id", id);*/
                    startActivity(i);
                }
                catch(Exception ex)
                {
                    Log.println(1, "item-click-event", ex.getMessage());
                }
            }
        });

I need to pass a string param to the new intent based on which listitem they clicked on. The value I want to pass is in the listitem called txt_Genus. How do I get that value out of the listitem to pass to the intent? Don't pay attention to my experiments please haha.

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

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

发布评论

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

评论(1

混浊又暗下来 2024-09-17 05:01:37

这应该可以做到。

TextView tv = (TextView)view.findViewById(R.id.txt_Genius);
String genus = tv.getText().toString();

然后把它放到intent extras里,我想你已经知道这一点了。

i.putExtra("genius", genius);

编辑;

在您的新活动中,您将访问意图并获得额外的捆绑包。然后,您可以访问您在先前活动的意图中放置的任何内容,如下所示;

Bundle extras = getIntent().getExtras();
String genius = extras.getString("genius");
// pop some toast to show the value of genius - not required but shows it works :) 
Toast.makeText(this, "Genius value: " + genius, Toast.LENGTH_SHORT).show();

This should do it.

TextView tv = (TextView)view.findViewById(R.id.txt_Genius);
String genus = tv.getText().toString();

Then put it into the intent extras, I think you already know this bit.

i.putExtra("genius", genius);

Edit;

In your new activity you would access the intent and get the extras bundle. You can then access anything you placed in the intent on your previous activity like below;

Bundle extras = getIntent().getExtras();
String genius = extras.getString("genius");
// pop some toast to show the value of genius - not required but shows it works :) 
Toast.makeText(this, "Genius value: " + genius, Toast.LENGTH_SHORT).show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文