ANDROID LISTVIEW

WHAT IS LISTVIEW

ListView is a view group that displays a list of scrollable items.

The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.

LISTVIEW WITH DEFAULT ARRAYADAPTER

public class ListViewAndroidExample extends Activity {
ListView listView ;

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

// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);

// Defined Array values to show in ListView
String[] values = new String[] { “Android List View”,
“Adapter implementation”,
“Simple List View In Android”,
“Create List View Android”,
“Android Example”,
“List View Source Code”,
“List View Array Adapter”,
“Android Example List View”
};

// Define a new Adapter
// First parameter – Context
// Second parameter – Layout for the row
// Third parameter – ID of the TextView to which the data is written
// Forth – the Array of data

ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);

// Assign adapter to ListView
listView.setAdapter(adapter);

// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {

// ListView Clicked item index
int itemPosition = position;

// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);

// Show Alert
Toast.makeText(getApplicationContext(),
“Position :”+itemPosition+” ListItem : ” +itemValue , Toast.LENGTH_LONG)
.show();

}

});
}

}

LISTVIEW WITH CUSTOM ADAPTER

In the above default adpater, we can show only one simple textview. What if we want to customize the layout of each row in the listview? For that, we would need to develop a custom adapter which extends ArrayAdapter and point it to our custom layout. In the below example, we are going to develop a listview with custom adapter which shows some movie names along with its director and the movie poster. Let’s get started.

STEP-1

1. Create a new Android Project and open the MainActivity class. Define 3 instance variables for getting the movie names, director names and poster images. We’ll get movie names and director names from the strings.xml and poster images from drawable folder. Add the below xml for the layout of MainActivity.

STEP-2

Then, define a CustomListViewAdapter like this:

CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, movie, images, director );

STEP-3

Android Studio will ask you to create this class. Click on create inner class and make CustomListViewAdapter extends ArrayAdapter.

You’ll prompted to create a constructor for CustomListViewAdapter. Create a constructor which takes the context, movie array, images array and director array as its parameters.

STEP-4

Now, we need to modify the adapter to point it to our custom layout. Override getView(int position, View convertView, ViewGroup parent) method and inflate the customlayout which you want to use for the row in the listview like this:

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.singlerow, parent, false);

getView() method is used for creating each row inside the listview and returning the row created to the listview.

LAYOUT FOR SINGLEROW

STEP-5

We need to initialize the items from the custom layout (singlerow.xml) and set the resources from the arrays.

ImageView myImage;
TextView myFilm;
TextView myDirector;

myImage= (ImageView) row.findViewById(R.id.imageView1);
myImage.setImageResource(images[position]);

myFilm=(TextView) row.findViewById(R.id.textView1);
myFilm.setText(filmArray[position]);

myDirector=(TextView) row.findViewById(R.id.textView2);
myDirector.setText(directorArray[position]);

FULL CODE OF MAINACTIVITY CLASS

STEP-6

That’s it. Now, Run the app and see you custom listview in action.

DOWNLOAD FULL SOURCE CODE

admin Author

Leave a Reply

Your email address will not be published. Required fields are marked *