Android Customized ListView

















 

Summary::


Main Activity :: NewsMainActivity.java
Package Name :: com.samir

Uses  Permission::
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Features::

Android Provides Simple Listview just Display one TextView in Row.But Sometimes in Applications we need more than one TextView and also other Views in Row.

This is Customized Android ListView Using  AsyncTask,Xml Parsing and ArrayAdapter.

The AsyncTask is Painless Threading in Android.User can download remote data and use it in App.Its provide Painless and Simple threading in Android.

After you getting data in XML response we have to parse data.And  add data in dynamic array in Android.

After we have XML data in Dynamic Array(List<T>)  user can bind listview using ArrayAdapter.


This tutorial start with NewsMainActivity.java.it is Android Launcher Activity.And in oncreate method i call AsyncTask for get data from rss feed and display progress dialog while getting data from rss feed.

Using NewsParser we can parse data from rss feed.I used DOM parser.and add data in List<Items>.this is one of the dynamic array to add items.And i set Adapter for listview using dynamic array.

Here i introduced most useful way of handle data in ArrayList.

The items class is Bean class. Which is custom class and user add element in class.Suppose i am parsing rss feed which have title,description,link,

class Item{
  private String title;
  private String desc;
  private String link;

 //here generate getters and setters method for all variables.

}

Now,when you parsing data then set value of them,Creating object of Item you can set value of title,desc and link.Then when you iterate ArrayList you get Item Object and you can easily retrieve you value.For more download source code.

Listview has Custom row(R.layout.row) with two textview as a Title,Description and one Button.
Using NewsRowAdapter(ArrayAdapter) inflating row in getView(...) method and add row in Listview.

Sometimes we need different row color in one ListView.So we have to put condition in Adapter in getView(...) method. And set background color of row.

in getView() method,

if(position==0){//position start with zero
   row.setBackgroundColor(Color.RED);
}else if(position==1){
  row.setBackgroundColor(Color.BLUE);
}

in this Example i put backgroundcolor of row for odd and even number row.

Now onItemClick i have AlertDialog with title as a position and message as row title.Now clicking on OK Button i pass title,description,date to next View.We have DetailView.

in row ,when you Click on Button0,Button1...also DetailView is opened as per row clicked button.

And in DetailView.java,

when you click on any row of ListView it gives details description  of that row in DetailView.java.And clicking on button i open URL in browser using Intent.ACTION_VIEW.

I also put checking of  internet connectivity and also How to format Date in Android.

i hope its very useful for Many Android Developers.