• Have Any Question?
  • contact@eitworld.com

write an application for push notification and title and message should be entered by the user.





In this application, we have to select one item from the list and this selected item will be displayed on the screen using text view. The xml layout file is as follows:

 

<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:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” ><TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:text=”ListView Example” />

<ListView
android:id=”@+id/listView1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignTop=”@+id/textView1″ >
</ListView>

</RelativeLayout>

 

Here we have used the relative layout and we have used the <ListView> tag to define the list view in the xml layout file. Here we have used some attributes to define the width, height, margin of the list view, and we have used a text view to display the message of selected item from the list and when application will launch then one message will display on the screen “ListView Example”, when we select any item from list then it will changed into the selected item dynamically.

The java file is as follows:

package com.example.listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

ListView lv;
String[] countNum = new String[] {“One”,”Two”,”Three”,”Four”,”Five”};
TextView message;

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

lv = (ListView) findViewById(R.id.listView1);
message = (TextView) findViewById(R.id.textView1);
ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countNum);
lv.setAdapter(adp);
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub

String item = lv.getItemAtPosition(arg2).toString();
message.setText(“selected item : “+item);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

Here we have used the following packages:

android

Here android.widget.ListView provides the methods related to the list by which we can use the list in the entire layout. After that we have to declare the ListView type variable so that we can use the list view which is defined in the xml layout file as follows:

android

android

Now we have to declare a string array that contains some values named countNum as follows:

android

Now we have to register the list view inside the onCreate() method as follows:

android

Here we have registered the list view using the id which we have provided in the xml layout file. Here we have created an ArrayAdapter object that is of the string type; takes the three parameters; first parameter is the context means current activity or current context, second parameter is the type of the layout of the list view, it is default provided by the android system, and the last parameter is the array name which we have created, here just pass the name of the array.

After that we have to call the setAdapter() method using the list view object and this method takes one parameter that is object of the ArrayAdapter means “adp” in this example.

Here we have used the setOnItemSelected() method to get the selected item. Inside this, onItemClick() method has the four parameters; first parameter is used for adapter view, 2nd parameter is used for view, 3rd parameter is used for the position of the list view and 4th parameter is used for the id of the list items. After that we have declared a string type variable. Here we have used the getItemAtPosition() method which takes the position as integer and used the toString() method to convert into the string format and assign to the item variable. After this, we have set the item variable using the getText() message for the text view message that will displayed on the screen when we will select the item from the list.

The output is as follows:

android

When launched

android

when item selected



Real Time Web Analytics