1. Download our Official Android App: Forums for Android!

Apps Clickable ListView / ArrayAdapter items / redirect to activity

Discussion in 'Android Development' started by ivanlmj, Jun 17, 2016.

?

How to start new Activies for ArrayAdapter items, passing item values to Activity (TextView widget)

  1. Android Activities

    0 vote(s)
    0.0%
  2. Android ArrayAdapter

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. ivanlmj

    ivanlmj Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jun 17, 2016

    Jun 17, 2016
    2
    0
    5
    Male
    Hi everyone! First post here!

    I want to make a clickable ListView that, when I click in one of the items, it will be started a new Activity, which will receive the item data from the ListView. e.g.: if I click on a item called Android from the ListView, it will be started a new activity with a TextView widget, showing the word Android, that came from the ArrayAdapter of the ListView?

    Anyone here knows how to do this?

    I know that I have to use Intents in order to start the activities, but how can I pass each item from the ArrayAdapter to a TextView widget from another Activity?

    Here are the codes which I am using.

    DisplayList.java
    Code (Text):
    1.  
    2. package com.redtjs.simplelistdisplay;
    3.  
    4. import android.os.Bundle;
    5. import android.app.Activity;
    6. import android.widget.ArrayAdapter;
    7. import android.widget.ListView;
    8.  
    9.  
    10. public class DisplayList extends Activity {
    11.  
    12.     String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry"};
    13.  
    14.     @Override
    15.     protected void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.activity_display_list);
    18.  
    19.         ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_row_layout, mobileArray);
    20.         ListView listView = (ListView) findViewById(R.id.select_dialog_listview);
    21.         listView.setAdapter(adapter);
    22.  
    23.     }
    24.  
    25. }
    26.  
    activity_display_list.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     android:paddingBottom="@dimen/activity_vertical_margin"
    8.     android:paddingLeft="@dimen/activity_horizontal_margin"
    9.     android:paddingRight="@dimen/activity_horizontal_margin"
    10.     android:paddingTop="@dimen/activity_vertical_margin"
    11.     tools:context="com.redtjs.simplelistdisplay.DisplayList">
    12.  
    13.     <ListView
    14.         android:id="@+id/select_dialog_listview"
    15.         android:layout_width="wrap_content"
    16.         android:layout_height="wrap_content"
    17.         android:text="Hello World!" />
    18.  
    19. </LinearLayout>
    20.  
    activity_row_layout.xml
    Code (Text):
    1. [/COLOR][/COLOR]
    2. [COLOR=#00b300][COLOR=#000000]<?xml version="1.0" encoding="utf-8"?>
    3.  
    4. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    5.     android:id="@+id/list_item"
    6.     android:layout_width="fill_parent"
    7.     android:layout_height="fill_parent"
    8.     android:padding="10dip"
    9.     android:textSize="16sp"
    10.     android:textStyle="bold" >
    11. </TextView>
    12.  

    Here is the activity which I want to be instantiated when one of the items of the ListView is clicked.

    DisplayRow.java
    Code (Text):
    1.  
    2. package com.redtjs.simplelistdisplay;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6.  
    7. public class DisplayRow extends Activity {
    8.  
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.activity_display_row);
    13.  
    14.     }
    15.  
    16. }
    17.  

    activity_display_row.xml

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical" android:layout_width="match_parent"
    4.     android:layout_height="match_parent">
    5.  
    6.     <TextView
    7.         android:layout_width="wrap_content"
    8.         android:layout_height="wrap_content" />
    9.  
    10. </LinearLayout>
    11.  


    Thanks in advance!
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #16
    Points:
    1,488
    Posts:
    6,701
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,701
    9,403
    1,488
    Male
    Software developer
    South West of England
  3. ivanlmj

    ivanlmj Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jun 17, 2016

    Jun 17, 2016
    2
    0
    5
    Male
    Awesome! Took me sometime to understand the logic but it worked!

    Thanks soldier!
     

Share This Page

Loading...