Lab Manual
Lab (5): List View
Visual Programming for Smart Devices lab
1 Instructor: Oraib M Alrashdan
Objectives:
create an android application using List View.
Task 1: Try IT An application using List View .java
Write this code then show the output.
package com.example.oraibpc.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String []
array_technology={"Android","Java","PHP","Python","C++","C#","Ruby","JavaFX","V
B","Ajax",".Net","Rails","Perl","HTML"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//called when the activity is first created.
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array_technology)
);
}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have
selected"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}
Note: Click an item. A message containing the item selected is displayed.
2 Instructor: Oraib M Alrashdan
3 Instructor: Oraib M Alrashdan
Task 2: Try It who to allow multiple items in the List view to be selected
and how to enable filtering support.
package com.example.oraibpc.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String []
array_technology={"Android","Java","PHP","Python","C++","C#","Ruby","JavaFX","V
B","Ajax",".Net","Rails","Perl","HTML"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item
_checked,array_technology));
}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have selected
"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}
4 Instructor: Oraib M Alrashdan
5 Instructor: Oraib M Alrashdan
Task 3: using your application created earlier, add the following
Strings.xml file located in the res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"> Hello World ,List View Activity ! </string>
<string name="app_name">List View </string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>PHP</item>
<item>Python</item>
<item>C++</item>
<item>C#</item>
<item>Ruby</item>
<item>JavaFX</item>
<item>VB</item>
<item>Ajax</item>
<item>.Net</item>
<item>Rails</item>
<item>Perl</item>
<item>HTML</item>
</string-array>
</resources>
Modify the main Avtivity.java file as shown below:
package com.example.oraibpc.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String [] array_technology;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
array_technology =
getResources().getStringArray(R.array.array_technology);
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_check
ed,array_technology));
}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have selected
"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}
6 Instructor: Oraib M Alrashdan