Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...