0

I have a listview with items inside each cell. I am able to get Toast alerts but what I would like it to do is to start a new activity with the content of the cell being displayed with a textview. Is this even possible? Here is my code for reference:

public class Activity extends ListActivity implements OnItemClickListener {

  ArrayList<HashMap<String,String>> gameCollection = new ArrayList<HashMap<String, String>>();

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

    ListView listView=getListView();
    listView.setOnItemClickListener(this);


    //Populate view with data
    gameCollection.add(displayVideoGame("PlayStation","FIFA 14"));
    gameCollection.add(displayVideoGame("PlayStation","Thief"));
    gameCollection.add(displayVideoGame("PlayStation","Watch Dogs"));
    gameCollection.add(displayVideoGame("PlayStation","Battlefield 4"));
    gameCollection.add(displayVideoGame("PlayStation","Second Sun"));
    gameCollection.add(displayVideoGame("PlayStation","Mario Bros"));




    //Calling the Simple Adapter calling gameCollection List and setting the predefined layout
    SimpleAdapter adapter = new SimpleAdapter(this,this.gameCollection, android.R.layout.simple_list_item_1, new String[] {"PlayStation"},new int[] {android.R.id.text1});
    //set adapter to collection
    System.out.println(gameCollection);
    setListAdapter(adapter);
}

//Create Private Method - Returns HashMap with key-value pairs
private HashMap<String, String> displayVideoGame(String key, String value)
{
    HashMap<String, String> videoGameHashMap = new HashMap<String, String>();
    videoGameHashMap.put(key,  value);
    return videoGameHashMap;

}

public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3)
{
    String item=adapter.getItemAtPosition(position).toString();
    Toast.makeText(Activity.this, "You Click on:"+item, Toast.LENGTH_SHORT).show();
}
user3078406
  • 511
  • 1
  • 9
  • 26

1 Answers1

0

In the body of your onItemClick, you can add the logic to start a new Activity:

Intent intent = new Intent(this, MyOtherActivity.class);
intent.putStringExtra(MyOtherActivity.TEXT_TO_DISPLAY, item);
startActivity(intent);

The first line creates a new intent that will start the "MyOtherActivity" activity (obviously you want to insert your own activity's name instead).

The second line sticks your item text you want to display into the intent's bundle so that your new activity can use it later.

The third line starts the activity described in your intent.

Then, in "MyOtherAcitivy.onCreate()":

String text = getIntent().getExtras().getString(TEXT_TO_DISPLAY);
TextView tv = (TextView) findViewById(R.id.id_of_textview);
tv.setText(text);

Here, the first line gets your string out of the bundle, the second line finds your TextView you want to fill, and the third line sets the text of your TextView to the string you sent from your first activity.

You will also need to add TEXT_TO_DISPLAY as a static final string in MyOtherActivity. This is the key that lets you find the string in the Extras bundle.

public static final string TEXT_TO_DISPLAY = "PickSomeTextToUseThatIsClear";

These links will give you some more information on how this all works.

Community
  • 1
  • 1
emerssso
  • 2,376
  • 18
  • 24
  • Hello. Thanks for your help. So I get an error that says"TEXT_TO_DISPLAY cannot be resolved or is not a field" Thanks – user3078406 Oct 16 '14 at 07:25
  • I just fixed that error. now the only error I get is on line 2 of the first portion of your code: intent.putStringExtra(MyOtherActivity.TEXT_TO_DISPLAY, item); The error says "The method putStringExtra(String, String) is undefined for the type Intent" Thank you! – user3078406 Oct 16 '14 at 08:02
  • Anyway your answer helped a whole lot. I am marking it as Answered. Thanks again. – user3078406 Oct 16 '14 at 08:09
  • All Errors fixed. Worked like a charm! Marked as Answered. Thank you!! – user3078406 Oct 16 '14 at 08:30