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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3273834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:06:19  来源:igfitidea点击:

Trying to get value out of text view in list view click event in android

javaandroideventslistviewlistviewitem

提问by Dale Marshall

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.

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

回答by Damon Skelhorn

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.

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

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();