DEVELOPMENT

Passing of data from one activity to another can be performed using intents. Here, i've created two activity. The first activity is filled with a listview. There are three option in list, when a user select any one of the option he is redirect to another activity which display a toast message saying the option clicked by the user.

listview android intent android

build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.tut.tut_7_7"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //compile 'com.android.support:appcompat-v7:22.2.0'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.tut.tut_7_7" >
 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <activity
 android:name=".ViewItem"
 android:label="@string/title_activity_view_item" >
 </activity>
 </application>
</manifest>    
MainActivity.java
package com.example.tut.tut_7_7;

 import android.app.ListActivity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;


public class MainActivity extends ListActivity{


 String[] arr = {"a","b","c"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,arr));
 }

@Override
 protected void onListItemClick (ListView l, View v, int position, long id){
 Intent myIntent = new Intent(this, ViewItem.class);
 myIntent.putExtra("id", arr[position]);
 startActivity(myIntent);
 }
}
ViewItem.java
package com.example.tut.tut_7_7;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ViewItem extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_item);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = extras.getString("id");
            Toast.makeText(getBaseContext(), value,
                    Toast.LENGTH_SHORT).show();
        }

    }
}
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/txtQuery"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <TextView
 android:id="@+id/nf"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleSmall"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
 <ListView
 android:id="@+id/list1"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 </ListView>

</LinearLayout>
res/layout/activity_view_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin"
 tools:context="com.example.tut.tut_7_7.ViewItem">

</RelativeLayout>

Leave a Comment  
Download Code(27)





All rights reserved, the content is copyrighted to the author.

RELATED POST