Android ListView is Viewgroup of that create list of scrollable item.A view that show item in a vertically  scrolling list.

Now create project with Android Simple Listview.
create row.xml file in res->layout folder of your project.

row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:textSize="15dp"
</TextView>

now open your MainActivity.java file in your project src folder. and 

public class MainActivity extends ListActivity

see here when we extending ListActivity to MainActivity then thet layout file must have ListView with id android:id=”@android:id/list”

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
</RelativeLayout>

Now using ArrayAdapter you can bind data with listview.ArrayAdapter is class that works to bind data to listview.You pass data in Constructor of this class.

private String[] INDIAN_STATE = { "Gujarat", "Andra Pradesh","Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa","Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala", "Madya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Tripura", "Uttaranchal", "Uttar Pradesh", "West Bengal" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.row, INDIAN_STATE);

now you just call setListAdapter to set Adapter to listview.

setListAdapter(adapter);

its display the indian state in listview and you can change your color of state name and background using row.xml file.they have textview,change its color or background.

Now,when listview is displayed its more important how to check which item clicked in listview and get selected or clicked value.there are OnItemClickListener for ListView.

but when you extends ListActivity then just ovveride onListItemClick  method.and there first parameter is listview.second is view which is row or item view.third is position of item.and last is id of row or item.

we can retrieve value of item.


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {    
   super.onListItemClick(l, v, position, id);

   String str =l.getItemAtPosition(position).toString();
   Toast.makeText(MainActivity.this, str,Toast.LENGTH_SHORT).show();

}

now we discuss how to display listview without extending ListActivity. When we extends Activity there is no need to(not compulsory) your ListView have id android:id=”@android:id/list”. You can use your own list nameandroid:id=”@+id/mylist”

 you can get Listview using,

 ListView listView = (ListView)findViewById(R.id.mylist);

And set Adapter and OnItemClickListener to listView.

listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position,long id) {
String str = l.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});

DownLoad Full Source Code From Here
DownLoad Source Code

Lets start How spinner works in android.

Spinner is widget similar to dropdown list for selecting items.

res->layout->main.xml

 <Spinner android:id="@+id/spinner" android:layout_width="match_parent"
  android:layout_height="wrap_content" />

now open your java class and insert following  code in OnCreate() method.

Spinner  spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INDIAN_STATE);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter); 

Listining spinner using OnItemSelectedListener.Mind that never use OnItemClickListener for spinner.when you use spinner then set OnItemSelectedListener for get selected item from spinner.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
      String str =(String)parent.getSelectedItem(); 
}

public void onNothingSelected(AdapterView<?> parent) {

 }
});

You can get Selected value from spinner using getSelectedItem in onItemSelected method.

And onNothingSelected method is called when you choose nothing from spinner.

When you  setOnItemSelectedListener to spinner it called when your Activity start, and its called onItemSelected method.So you can get default selected value from spinner.

Using setSelection(position) method you can set position of spinner.

Now when you working  with more than one spinner and you have one setOnItemSelectedListener for all spinners then get selected value using spinner’s name.


String str1= (String) spinner1.getSelectedItem();
String str2= (String) spinner2.getSelectedItem();

Download Full Source Code From Here

Let’s start to how to parse Json Objects in Android.
First of all when you start parse Json Objects Check your Json String is valid or not.
Here You can Check Your Json String

here is my Json String.

 {
 "student": [
 {
 "id": 1,
 "name": "Anil",
 "city": "Surat",
 "Gender": "M",
 "age": 10,
 "birthdate": "23/05/2002"
 },
 {
 "id": 2,
 "name": "Kapil",
 "city": "Baroda",
 "Gender": "M",
 "age": 12,
 "birthdate": "08/01/2000"
 },
 {
 "id": 3,
 "name": "Priya",
 "city": "Ahmedabad",
 "Gender": "F",
 "age": 14,
 "birthdate": "01/03/1998"
 }
 ]
 }

Now above Json String have as Student array.that So first,check your Json String is Starting with `{` then its start with JSONObject.and if its start with `[` then its start with JSONArray.

1.JSONObject

so here we have JSONObject,

JSONObject mainJson = new JSONObject(jsonString);


Now we have array student so we can get JSONArray from JSONObject.and in getJSONArray method have String paramter now add your JSONArray name.

JSONArray jsonArray = mainJson.getJSONArray("student");


Getting JSONArray now we have JSONObject of as in JSONArray.So using for loop we can get one by one JSONObject.

 for (int i = 0; i < jsonArray.length(); i++) {
 JSONObject objJson = jsonArray.getJSONObject(i);
 // here you can get id,name,city...
 int id = objJson.getInt("id");
 String name =objJson.getString("name");
 String gender=objJson.getString("Gender");
 }

2.JSONArray

if your Json String Start with JSONArray then you just,

JSONArray jsonArray = new JSONArray(jsonString);

now as per above jsonArray ,retrieve value.

3.How to check JSONString have Array or any Fileld And those are null or not?
Using has(String) you can check JSONObject have array or filed

 if(objJson.has("student")){ }
 if(objJson.has("id")){ }

There are one more thing is check null for jsonObject.

if(objJson.isNull("name")){}

4.How to Remove JSONObject from JSONArray?

There are no any method for removing jsonobject from JSONArray.So we have option List<T>.so List have remove method and you can remove your JSONObject.

5.How many Data type we can Retrieve from JSONObject?
Using JSONObject you can retrieve Strings, Booleans, Integers, Longs, Doubles

 
DownLoad Full Source Code From Here.
DownLoad Source Code

Android Listview with image and Text::

Here i populated Listview with one ImageView and Three TextView using below layout file.

here when we have listview  with images then we download image one by one asyncronously in android.Then create bitmap from url and set it to imageview .so when you work with image then first read data from url byte by byte  and image have more bites that text so its take time to load in listview.

So when you need display image in listview in each row .then you have to use custom adapter for that ,and using your row layout you can inflate row and fill it in listview.

So First of all you have to create Thread or AsyncTask for read rss feeds from server.Then you can get image Url from rss feeds.now using image url you can download each image in seperate thread or asynctask.after completion of download you can set image to imageview.

R.layout.row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_bg" >

<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#444"
android:padding="3dp" />

<TextView
android:id="@+id/tvtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toLeftOf="@+id/tvdate"
android:layout_toRightOf="@+id/image"
android:maxLines="1"
android:text="title"
android:textColor="@android:color/black"
android:textSize="15dip"
android:textStyle="bold" />

<TextView
android:id="@+id/tvdesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvtitle"
android:layout_toRightOf="@+id/image"
android:ellipsize="end"
android:maxLines="3"
android:text="desc"
android:textColor="@android:color/black"
android:textSize="13dip" />

<TextView
android:id="@+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dip"
android:text="date"
android:textColor="@android:color/black"
android:textSize="12dip" />

</RelativeLayout>

here we have row and there are three Textview and imageview.so in Adapter Class we inflate this row in getView() method.

LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row, null);

Now we use Viewholder in adapter, it makes listview for smooth/reusable row view in listview android.
public static class ViewHolder {
public TextView tvTitle, tvDesc, tvDate;
private ImageView imgView;
}

now create new object of ViewHolder for every row while view is null.otherwise set as a tag using setTag(ViewHolder).


ViewHolder holder;
if(view==null){ 
 holder = new ViewHolder();
 view.setTag(holder);
} else {
 holder = (ViewHolder) view.getTag();
}

now using viewHolder object you can refrenced your view from row.xml.

holder.tvTitle = (TextView) view.findViewById(R.id.tvtitle);
holder.tvDesc = (TextView) view.findViewById(R.id.tvdesc);
holder.tvDate = (TextView) view.findViewById(R.id.tvdate);
holder.imgView = (ImageView) view.findViewById(R.id.image);

we can set here value of each view of row.

holder.tvTitle.setText("title");
holder.tvDesc.setText("title");
holder.tvDate.setText("title");
holder.imgView.setImageResource(R.drawable.icon);

then return statement of getView method is view.

return view;

Download Full Source Code From here
Download Source Code.

how to work with AsyncTask in android ?

For Example ,i have AsyncTask which name is MyTask.

MyTask objMyTask = new MyTask();

To start AsyncTask call 

objMyTask.execute();

  • The AsyncTask instance must be created in UI thread.
  • .execute must be invoked on the UI thread.
  • Never call  objMyTask.onPreExecute(), objMyTask.doInBackground(), 
    objMyTask.onProgressUpdate(),  objMyTask.onPostExecute manually.
  • The AsyncTask can be executed only once (an exception will be thrown if a second execution is attempted.)


AsyncTask have Four Main Method…

  1. onPreExecute()
  2. doInBackground()
  3. onProgressUpdate()
  4. onPostExecute()

  • onPreExecute-This method is called first when you start AsyncTask using objAsync.execute().And mostly this method is use for initializing dialog(ProgressDialog,CustomDialog) and showing.
  • doInBackground-The main purpose of AsyncTask is accomplished by this method.Any non-UI thread process is running in this method.Such as Rss Feed Reader,Image and video Uploading and Downloading.You cant handle your View in this method.Because this method is non-UI thread.While any background process is running if you want to handle UI therea are  onProgressUpdate method. after completion of process this method send result to OnPostExecute.
  • onProgressUpdate-While backgrounding task is running ,you can handle your UI using this method .Such as status of downloading or uploading task.and this method is called from  doInBackground.Using publishProgress() you can call onProgressUpdate method to update UI while process is running.
  • onPostExecute –This method is called after the background computation finishes.The result of background process in passed in this method as parameters.And now you can dismiss progress dialog ,to indicate that background task is completed.
You can cancel AsyncTask using 

objAsyncTask.cancel();


then you just check in doInBackground,

if (isCancelled()) {
break;
} else {
   //continue…
}